import { Container } from "./model/container.js"; import * as meta from './metadata/index.js' // import { Command, CommandProxy } from './model/command'; import { CommandProcessor, type ICommandProcessor } from './model/processor.js'; import { SelfDefinedCommand } from "./model/command.js"; import { isCommand } from "./metadata/index.js"; import { Local } from "./processors/local.js"; // import { configure } from './decorators'; export const ignoredCommands = ['$serve', '$metadata', '$attach'] export function metadata(container: Container, deep?: boolean, extract?: boolean): meta.Container { // console.log(deep); const metacontainer: meta.Container = { name: container.name || 'unnamed', commands: [] }; container.keys().forEach((key) => { if (key === '$injector' || key === '$state' || key === '$container') return; const cmd = container.resolve(key); if (cmd && isCommand(cmd) && ignoredCommands.indexOf(cmd.name) == -1) metacontainer.commands.push(extract ? meta.extractCommandMetadata(cmd) : cmd); else if (cmd instanceof Container && deep) { // console.log(cmd); const subContainer = metadata(cmd as Container, deep); if (extract) metacontainer.commands.push(...subContainer.commands.map(c => ({ ...meta.extractCommandMetadata(c), name: key + '.' + c.name }))); else metacontainer.commands.push(...subContainer.commands); } }); return metacontainer; } export function proxy(metacontainer: meta.Container, processor: CommandProcessor): Container { const container = new Container(metacontainer.name, undefined, processor); container.unregister('$metadata'); container.register(new SelfDefinedCommand(() => metacontainer, '$metadata')); registerCommands(metacontainer.commands, processor, container); return container; } // export function proxyCommand(cmd: meta.Command, processor: CommandProcessor): Command // { // return configure(cmd.config)(new CommandProxy(processor, cmd.name, cmd.inject)) // } export function registerCommands(commands: meta.Command[], processor: ICommandProcessor, container: Container): void { for (const cmd of commands) { if (cmd.name == '$serve' || cmd.name == '$attach' || cmd.name == '$metadata') continue; container.register(cmd.name, Object.assign(cmd, { processor })); } } export function updateCommands(commands: meta.Command[], processor: ICommandProcessor, container: Container): void { container.keys().map<[string, meta.Command]>(c => [c, container.resolve(c)]).filter(x => isCommand(x[1] && x[0] != '$metadata')).forEach(v => container.unregister(v[0])) for (var cmd of commands) { if (cmd.name == '$serve' || cmd.name == '$attach' || cmd.name == '$metadata') return; container.register(cmd.name, { ...cmd, processor }, true); } } export function helper Promise }>(container: Container, metacontainer?: meta.Container): TProxy { const result: TProxy = {} as unknown as TProxy; if (!metacontainer) metacontainer = metadata(container); commandList(metacontainer).forEach(cmd => { result[cmd as keyof TProxy] = function (...args) { return container.dispatch(cmd, ...args); } as unknown as TProxy[keyof TProxy]; }); return result; } export function commandList(container: meta.Container): string[] { return container.commands.map(cmd => cmd.name) } export function fromObject>(o: T, name: string): Container { const container = proxy(metadataFromObject(o, name), Local.fromObject(o)); container.state = o; return container; } export function metadataFromObject(o: T, name: string): meta.Container { return { name, commands: Object.entries(o).filter(e => typeof e[1] === 'function').map(e => ({ name: e[0], config: { "": { inject: e[1].$inject } } })) }; }