import { Startup, ObjectConstructor, Context, isFunction, isString, } from "@halsp/core"; import { HookType } from "@halsp/core"; import { MAP_BAG, SINGLETON_BAG } from "./constant"; import { KeyTargetType, InjectMap } from "./interfaces"; import { InjectDecoratorParser, InjectTarget, isInjectClass, } from "./inject-parser"; import { InjectType } from "./inject-type"; import { IService } from "./interfaces/service"; declare module "@halsp/core" { interface Startup { useInject(): this; inject(key: string, target: T): this; inject( key: string, target: (ctx: Context) => T | Promise, type?: InjectType, ): this; inject( key: string, target: ObjectConstructor, type?: InjectType, ): this; inject( anestor: ObjectConstructor, target: TTarget, ): this; inject( target: ObjectConstructor, type?: InjectType, ): this; inject( anestor: ObjectConstructor, target: (ctx: Context) => TTarget | Promise, type?: InjectType, ): this; inject( anestor: ObjectConstructor, target: ObjectConstructor, type?: InjectType, ): this; } interface Context { getService(key: string): Promise; getService(target: InjectTarget): Promise; getCachedService( target: ObjectConstructor | string, ): T | undefined; } } const usedMap = new WeakMap(); Startup.prototype.useInject = function (): Startup { if (usedMap.get(this)) return this; usedMap.set(this, true); const singletons = []; return this.use(async (ctx, next) => { initContext(ctx); ctx.set(SINGLETON_BAG, singletons); ctx.set(MAP_BAG, []); await next(); }) .hook(HookType.Constructor, async (ctx, mh) => { if (isInjectClass(mh)) { return await ctx.getService(mh); } }) .hook(async (ctx, mh) => { await ctx.getService(mh); }) .inject(Context, (ctx) => ctx); }; Startup.prototype.inject = function (...args: any[]): Startup { let anestor: ObjectConstructor | string; let target: ObjectConstructor | any | ((ctx: Context) => any); let type: InjectType | undefined; if (typeof args[0] == "string") { anestor = args[0]; target = args[1]; type = args[2]; } else if (args[1] == undefined) { anestor = args[0]; target = args[0]; } else { if (typeof args[1] == typeof InjectType.Singleton) { anestor = args[0]; target = args[0]; type = args[1]; } else { anestor = args[0]; target = args[1]; type = args[2]; } } this.use(async (ctx, next) => { const injectType = type ?? InjectType.Scoped; const injectMaps = ctx.get(MAP_BAG); injectMaps.push({ anestor, target, type: injectType, }); try { await next(); } finally { await dispose(ctx, injectType, anestor); } }); return this; }; async function dispose( ctx: Context, injectType: InjectType, target: ObjectConstructor | string, ) { async function disposeObject(instance?: T) { if (!instance) return; if (!instance.dispose || !isFunction(instance.dispose)) return; await instance.dispose(ctx); } if (injectType == InjectType.Scoped) { const instance = ctx.getCachedService(target) as IService; await disposeObject(instance); } else if (injectType == InjectType.Transient) { const parser = new InjectDecoratorParser(ctx); const instances = parser.getTransientInstances(target) as IService[]; for (const instance of instances) { await disposeObject(instance); } } } function initContext(ctx: Context) { ctx.getService = async function ( target: InjectTarget | string, ) { if (isString(target)) { return await new InjectDecoratorParser(ctx).parseKey(target); } else { return await new InjectDecoratorParser(ctx).parseTarget(target); } }; ctx.getCachedService = function ( target: ObjectConstructor | string, ) { return new InjectDecoratorParser(ctx).getCachedService(target); }; }