All files / lib/modifiers/initializers RunAfterModifier.ts

93.33% Statements 14/15
72.72% Branches 8/11
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3312x         12x   45x       59x       59x 59x   59x 2x 2x 2x 2x 2x 2x       59x        
import { Keys } from "../../Keys";
import { IRunAfter } from "../../interfaces/IRunAfter";
import { IResolver } from "../../interfaces/IResolver";
import { IInitializer } from "./IInitializer";
 
export class RunAfterModifier implements IInitializer {
 
    constructor(readonly resolver: IResolver) {
    }
 
    async run(instance: any, definition: any): Promise<any> {
        return this.setRunAfter(instance, definition);
    }
 
    async setRunAfter(resolvedInstance: any, definition: any) {
        const afterMeta = Reflect.getMetadata(Keys.AFTER_METHOD_KEY, resolvedInstance.constructor) || {};
        Iif (!afterMeta) return resolvedInstance;
 
        for (const key in afterMeta) {
            const resolveRunAfter: IRunAfter = typeof afterMeta[key] === "string" ? await this.resolver.resolve(afterMeta[key]) : await this.resolver.resolveByType(afterMeta[key]);
            const descriptorOriginal = Reflect.getMetadata(Keys.METHOD_DESCRIPTOR_KEY, resolvedInstance.constructor) || {};
            resolvedInstance[key]  = function (this: any, ...args: any) {
                const res = descriptorOriginal.value?.apply(this, args);
                resolveRunAfter.run();
                return res;
            };
        }
 
        return resolvedInstance;
    }
 
}