@parkersoftware/whoson-lib
Version:
Useful whoson related library
129 lines (128 loc) • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hooks = void 0;
require("./global.helpers");
var Logging_1 = require("./Logging");
var Hooks = /** @class */ (function () {
function Hooks(_showCalls, _showWarnings) {
if (_showCalls === void 0) { _showCalls = false; }
if (_showWarnings === void 0) { _showWarnings = false; }
this._showCalls = _showCalls;
this._showWarnings = _showWarnings;
this._hooks = new Map();
this._showWarnings = _showWarnings;
this._hookLog = [];
}
Object.defineProperty(Hooks.prototype, "showCalls", {
get: function () {
return this._showCalls;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Hooks.prototype, "showWarnings", {
get: function () {
return this._showWarnings;
},
enumerable: false,
configurable: true
});
/**
* register a hook
* @param name name of the hook to bind to
* @param callback the function to run when it's called
* @param weight weight by default is 0, the higher the weight the earlier it is called in the hook sequence
*/
Hooks.prototype.register = function (name, callback, weight) {
if (weight === void 0) { weight = 0; }
throwIf(isEmpty(name), "name cannot be empty");
throwIf(isNull(callback), "callback cannot be null");
name = name.toLowerCase();
var collection = this._hooks.get(name);
if (isNull(collection)) {
this._hooks.set(name, []);
collection = this._hooks.get(name);
}
var entry = new Hook(callback, weight);
collection === null || collection === void 0 ? void 0 : collection.push(entry);
collection === null || collection === void 0 ? void 0 : collection.sort(function (a, b) {
if (a.Weight > b.Weight)
return -1;
if (a.Weight < b.Weight)
return 1;
return 0;
});
};
/**
* call a hook by name with any args
* @param name the name of the hook to call
* @param args any args you want to pass to the hook
* @returns {number} the amount of hooks called
*/
Hooks.prototype.call = function (name, args) {
if (args === void 0) { args = null; }
throwIf(isEmpty(name), "name cannot be empty");
name = name.toLowerCase();
var counter = 0;
var handled = false;
if (this._showCalls)
Logging_1.Logging.log("Hook " + name + " called");
var collection = this._hooks.get(name);
if (isNotNull(collection)) {
for (var i = 0; i < (collection === null || collection === void 0 ? void 0 : collection.length); i++) {
try {
var returned = void 0;
if (isNotNull(args))
returned = collection[i].Callback.call(this, args);
else
returned = collection[i].Callback.call(this);
handled = true;
counter++;
// If any thing is returned from the hooks we cancel the call useful for overriding hooks
if (isNotNull(returned) && Object.prototype.toString.call(returned) !== "[object Promise]") {
break;
}
}
catch (e) {
Logging_1.Logging.error("Error calling hook - " + name, e);
}
}
}
else {
Logging_1.Logging.warn("No hooks registered for - " + name);
}
this.addHookLog(name, args, handled);
return counter;
};
/**
* add a hook call to the hook log
*/
Hooks.prototype.addHookLog = function (name, args, handled) {
try {
if (this._hookLog.length > 2000) {
this._hookLog = this._hookLog.slice(1000);
}
this._hookLog.push({ Timestamp: new Date(), Name: name, Args: args, Handled: handled });
}
catch (e) {
Logging_1.Logging.error("An error occurred whilst adding a hook", e);
}
};
/**
* get the last 1000 hook events that have occurred
* @returns {HookEvent[]} an array of recent hook events
*/
Hooks.prototype.HookLogs = function () {
return this._hookLog.slice(-1000);
};
return Hooks;
}());
exports.Hooks = Hooks;
var Hook = /** @class */ (function () {
function Hook(Callback, weight) {
this.Callback = Callback;
this.Weight = weight || 0;
}
return Hook;
}());
module.exports.Hooks = Hooks;