Feat v0.8.5 extension composability (#72)
All checks were successful
CI/CD / detect-changes (push) Successful in 4s
CI/CD / test-runners (push) Has been skipped
CI/CD / e2e-smoke (push) Has been skipped
CI/CD / test-frontend (push) Successful in 6s
CI/CD / test-go-pg (push) Successful in 2m44s
CI/CD / test-sqlite (push) Successful in 2m54s
CI/CD / build-and-deploy (push) Successful in 52s
All checks were successful
CI/CD / detect-changes (push) Successful in 4s
CI/CD / test-runners (push) Has been skipped
CI/CD / e2e-smoke (push) Has been skipped
CI/CD / test-frontend (push) Successful in 6s
CI/CD / test-go-pg (push) Successful in 2m44s
CI/CD / test-sqlite (push) Successful in 2m54s
CI/CD / build-and-deploy (push) Successful in 52s
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com> Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
This commit was merged in pull request #72.
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
export function createSlots(emitFn) {
|
||||
/** @type {Map<string, Array<{id:string, component:Function, priority:number}>>} */
|
||||
const _registry = new Map();
|
||||
/** @type {Map<string, string>|undefined} */
|
||||
let _declarations;
|
||||
|
||||
function _sort(arr) {
|
||||
arr.sort((a, b) => a.priority - b.priority);
|
||||
@@ -73,5 +75,43 @@ export function createSlots(emitFn) {
|
||||
names() {
|
||||
return [..._registry.keys()];
|
||||
},
|
||||
|
||||
/**
|
||||
* Render all components registered in a slot.
|
||||
* Each component is called with the context object. Errors are
|
||||
* caught so one broken contributor doesn't break the host surface.
|
||||
* @param {string} name — slot name
|
||||
* @param {object} context — props passed to each component
|
||||
* @returns {Array} — array of rendered vnodes (nulls filtered)
|
||||
*/
|
||||
renderAll(name, context = {}) {
|
||||
return this.get(name).map(e => {
|
||||
try {
|
||||
return e.component(context);
|
||||
} catch (err) {
|
||||
console.error(`[sw.slots] Error rendering "${e.id}" in slot "${name}":`, err);
|
||||
return null;
|
||||
}
|
||||
}).filter(Boolean);
|
||||
},
|
||||
|
||||
/**
|
||||
* Declare a slot for discoverability. Runtime-only metadata —
|
||||
* does not affect registration or rendering.
|
||||
* @param {string} name — slot name
|
||||
* @param {string} description — human-readable purpose
|
||||
*/
|
||||
declare(name, description) {
|
||||
if (!_declarations) _declarations = new Map();
|
||||
_declarations.set(name, description);
|
||||
},
|
||||
|
||||
/**
|
||||
* List declared slot metadata (from declare()).
|
||||
* @returns {Map<string, string>}
|
||||
*/
|
||||
declarations() {
|
||||
return _declarations ? new Map(_declarations) : new Map();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user