UNPKG

@wener/console

Version:

Base console UI toolkit

54 lines (53 loc) 1.71 kB
import { createStore } from "zustand"; import { immer } from "zustand/middleware/immer"; import { DynamicStore } from "./DynamicStore.js"; let DynamicModuleContext = class DynamicModuleContext extends DynamicStore { module; constructor(module, store) { super(store), this.module = module; } }; export class ModuleService { loader; store; instances; get modules() { return this.instances.map((v) => v.module); } constructor(loader) { this.loader = loader; this.store = createStore(immer(() => { return {}; })); this.instances = []; } async loadModules(names) { const last = this.instances.map((v) => v.name); names = names.filter((v) => last.indexOf(v) < 0); const loaded = await Promise.all(names.map(async (name) => { const module = await this.loader(name); return { module, name, context: new DynamicModuleContext(module, this.store) }; })); // ensure order await Promise.all(loaded.map(async ({ module, context }) => { if ("onModuleInit" in module) { await module.onModuleInit?.(context); } })); this.instances = [ ...this.instances, ...loaded ]; // wait to next tick, ensure the SetState take effects await Promise.resolve(); } async createRoutes() { const all = await Promise.all(this.instances.map((v) => v.module?.createRoutes?.(v.context))); return all.flat().filter((x) => Boolean(x)); } } //# sourceMappingURL=ModuleService.js.map