all files / src/module/ module.dependency.js

100% Statements 23/23
92.86% Branches 13/14
100% Functions 9/9
100% Lines 23/23
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                    19× 19× 19×                   19×                         13× 12×     13×                                                                                                            
/**
 * Class representing DependencyManager
 */
class DependencyManager {
 
    /**
     * Creates instance of DependencyManager
     * @constructor
     */
    constructor() {
        this._dependencies = {};
        this._resolvedDependencies = {};
        this._scope = null;
    }
 
    /**
     * checks if dependency is valid
     * @param key
     * @param dependency
     * @return {boolean}
     */
    isValid(key, dependency) {
        return (
            typeof key === 'string' &&
            typeof dependency === 'function'
        );
    }
 
    /**
     * add dependency
     * @param {string} key
     * @param {function} dependency
     * @return {DependencyManager}
     */
    add(key, dependency) {
        if(this.isValid(key, dependency)) {
            this._dependencies[key] = dependency;
        }
 
        return this;
    }
 
    /**
     * resolve dependencies
     * @return {Promise}
     */
    async resolve() {
        const keys = Object.keys(this._dependencies);
        const resolved = await Promise.all(
            keys.map((depKey) => {
                return this._dependencies[depKey]();
            }),
        );
 
        keys.forEach((depKey, idx) => {
            this._resolvedDependencies[depKey] = resolved[idx];
        });
 
        return this;
    }
 
    /**
     * checks if valid
     * dependencies exists
     * @return {boolean}
     */
    hasDependencies() {
        return !!Object.keys(this._dependencies).length;
    }
 
    /**
     * inject dependencies to a given scope
     * and makes it accessible through
     * local properties
     * @return {DependencyManager}
     */
    inject(scope) {
        if(
            scope &&
            this.hasDependencies()
        ) {
            this._scope = scope;
 
            Object.keys(this._resolvedDependencies)
                .forEach((dependencyKey) => {
 
                    const dependency = this._resolvedDependencies[dependencyKey];
 
                    if(
                        typeof dependencyKey === 'string' &&
                        typeof scope[dependencyKey] === 'undefined'
                    ) {
                        scope[dependencyKey] = dependency;
                    } else Eif(typeof dependencyKey === 'string') {
                        throw new Error(`
                            Could not bind dependency '${dependencyKey}' property alread reserved by controller.
                            We recommend to work with a prefix convention like all dependencies have a $ sign prefixed
                            and become accessible by this.$dependencyKey.
                        `);
                    }
                });
        }
        return this;
    }
 
}
 
export {
    DependencyManager,
};