@feflow/cli
Version:
A front-end flow tool.
85 lines • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var constant_1 = require("../../shared/constant");
var Hook = /** @class */ (function () {
function Hook() {
this.listeners = {};
this.maxListener = 100;
}
Hook.prototype.on = function (type, listener) {
var listeners = this.listeners;
if (listeners[type] && listeners[type].length >= this.maxListener) {
throw new Error("Listener's maxCount is ".concat(this.maxListener, ", has exceed"));
}
if (Array.isArray(listeners[type])) {
if (listeners[type].indexOf(listener) === -1) {
listeners[type].push(listener);
}
}
else {
listeners[type] = [listener];
}
};
Hook.prototype.emit = function (type) {
var _this = this;
var _a;
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
switch (type) {
case constant_1.HOOK_TYPE_BEFORE:
this.hook(constant_1.HOOK_TYPE_BEFORE, function () {
_this.emit(constant_1.EVENT_COMMAND_BEGIN);
});
break;
case constant_1.HOOK_TYPE_AFTER:
this.hook(constant_1.HOOK_TYPE_AFTER, function () {
_this.emit(constant_1.EVENT_DONE);
});
break;
default: {
(_a = this.listeners[type]) === null || _a === void 0 ? void 0 : _a.forEach(function (listener) {
listener(args);
});
break;
}
}
};
/**
* Run hook `type` callbacks and then invoke `fn()`.
*
* @private
* @param {string} type
* @param {Function} fn
*/
Hook.prototype.hook = function (type, fn) {
var hooks = this.listeners[type];
var next = function (i) {
var hook = hooks[i];
if (!hook) {
return fn();
}
var result = hook();
if (result && typeof result.then === 'function') {
result.then(function () {
next(i + 1);
}, function () {
throw new Error('Promise rejected with no or falsy reason');
});
}
else {
next(i + 1);
}
};
process.nextTick(function () {
if (!hooks) {
return fn();
}
next(0);
});
};
return Hook;
}());
exports.default = Hook;
//# sourceMappingURL=index.js.map