all files / lib/ hooks.js

100% Statements 21/21
89.47% Branches 17/19
100% Functions 4/4
100% Lines 19/19
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              13×               11×   11×     10×      
"use strict";
 
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.add = add;
exports.run = run;
 
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
 
var hooks = {};
 
function add(name, callback) {
    if (typeof name !== "string" || name.length < 1) {
        throw new Error("Name must be string of length > 1");
    }
 
    if (typeof callback !== "function") {
        throw new Error("hooks.add expects a function to be passed as a callback but " + (typeof callback === "undefined" ? "undefined" : _typeof(callback)) + ":" + callback + " given");
    }
 
    if (!hooks[name]) hooks[name] = [];
 
    hooks[name].push(callback);
}
 
function run(name, env) {
    var callbacks = hooks[name];
 
    if (!callbacks || !callbacks.length) {
        return;
    }
 
    callbacks.forEach(function (callback) {
        return callback(env);
    });
}
 
exports.default = { add: add, run: run };