All files / app/lib hook.class.js

77.78% Statements 14/18
60% Branches 6/10
100% Functions 4/4
77.78% Lines 14/18
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 451x   1x     61x         1x     1x 1x 1x               2x       2x         2x         5x 5x 4x   1x      
const common = require('./../common');
 
module.exports = class Hook {
 
    constructor(ctx, next) {
        this.hooks = {};
    }
 
    get(name) {
 
        Iif (arguments.length === 0) {
            return this.hooks;
        } else {
            for (let key in this.hooks) {
                Eif (name === key) {
                    return this.hooks[name];
                }
            }
        }
    }
 
    add(name, fn) {
 
        Iif (this.hooks[name]) {
            common.log(`The "${name}" hook already exists. repeat adding will cover before`, 'info');
        }
 
        Iif (typeof fn !== 'function') {
            throw new Error(`The "${name}" hook only support function`);
            return;
        }
 
        this.hooks[name] = fn;
    }
 
    run(name, ...args) {
 
        const fn = this.hooks[name];
        if (!fn) {
            return;
        }
        return fn(...args);
    }
}