All files / utils/services/module module.service.ts

21.62% Statements 8/37
0% Branches 0/6
10% Functions 1/10
17.14% Lines 6/35

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74  8x     8x 8x   8x                                                                                         8x 7x                                        
import { GraphQLObjectType, GraphQLNonNull } from 'graphql';
import { Service } from '../../../utils/container/index';
import { Subject } from 'rxjs/Subject';
import { GapiModuleArguments } from '../../../decorators/gapi-module/gapi-module.decorator.interface';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Container } from '../../container';
 
export class ModuleMapping {
    _module_name: string;
    _injectables: GapiModuleArguments;
    _handlers: BehaviorSubject<Array<any>> = new BehaviorSubject([]);
 
    constructor(name: string, injectables: GapiModuleArguments) {
        this._module_name = name;
        this._injectables = injectables;
    }
 
    async registerDependencyHandler(module): Promise<boolean> {
        const originalFactory = module.useFactory;
        module.useFactory = function () {
            return originalFactory(...module.deps);
        };
        Container.set(module.provide, module.useFactory());
        this._handlers.next([...this._handlers.getValue(), module]);
 
        return await true;
    }
 
    async resolveDependencyHandlers(): Promise<any> {
        this._handlers.getValue().forEach(handler => {
            const injectables = [...handler.deps];
            let resolvedInjectables = [];
            injectables.forEach(i => {
                if (i.constructor === Function) {
                    // resolvedInjectables = [...resolvedInjectables, Container.get(i)];
                } else {
                    resolvedInjectables = [...resolvedInjectables, i];
                }
            });
            const originalFactory = handler.useFactory;
            handler.useFactory = function () {
                console.log(resolvedInjectables);
                return originalFactory(...resolvedInjectables);
            };
            Container.set(handler.provide, handler.useFactory());
            console.log(Container.get(handler.provide));
        });
        return await true;
    }
}
 
@Service()
export class ModuleContainerService {
    modules: Map<string, ModuleMapping> = new Map();
    getModule(name: string): ModuleMapping {
        if (this.modules.has(name)) {
            return this.modules.get(name);
        } else {
            throw new Error('Missing module internal Gapi Error!');
        }
    }
 
    createModule(name: string, injectables: GapiModuleArguments): ModuleMapping {
        if (this.modules.has(name)) {
            return this.modules.get(name);
        } else {
            this.modules.set(name, new ModuleMapping(name, injectables));
            return this.modules.get(name);
        }
 
    }
 
 
}