@robotlegsjs/core
Version:
An architecture-based IoC framework for JavaScript/TypeScript
94 lines • 4.04 kB
JavaScript
;
// ------------------------------------------------------------------------------
// Copyright (c) 2017-present, RobotlegsJS. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageRunner = void 0;
var safelyCallBack_1 = require("./safelyCallBack");
var MessageRunner = /** @class */ (function () {
/*============================================================================*/
/* Constructor */
/*============================================================================*/
/**
* @private
*/
function MessageRunner(message, handlers, callback) {
this._message = message;
this._handlers = handlers;
this._callback = callback;
}
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
/**
* @private
*/
MessageRunner.prototype.run = function () {
this._next();
};
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
MessageRunner.prototype._next = function () {
var _this = this;
// Try to keep things synchronous with a simple loop,
// forcefully breaking out for async handlers and recursing.
// We do this to avoid increasing the stack depth unnecessarily.
var handler;
var _loop_1 = function () {
handler = this_1._handlers.pop();
if (handler.length === 0) {
// sync handler: ()
handler();
}
else if (handler.length === 1) {
// sync handler: (message)
handler(this_1._message);
}
else if (handler.length === 2) {
// sync or async handler: (message, callback)
var handled_1 = false;
handler(this_1._message, function (error, msg) {
if (error === void 0) { error = null; }
if (msg === void 0) { msg = null; }
// handler must not invoke the callback more than once
if (handled_1) {
return;
}
handled_1 = true;
if (error || _this._handlers.length === 0) {
if (_this._callback) {
safelyCallBack_1.safelyCallBack(_this._callback, error, _this._message);
}
}
else {
_this._next();
}
});
return { value: void 0 };
}
else {
// ERROR: this should NEVER happen
throw new Error("Bad handler signature");
}
};
var this_1 = this;
while (this._handlers.length > 0) {
var state_1 = _loop_1();
if (typeof state_1 === "object")
return state_1.value;
}
// If we got here then this loop finished synchronously.
// Nobody broke out, so we are done.
// This relies on the various return statements above. Be careful.
if (this._callback) {
safelyCallBack_1.safelyCallBack(this._callback, null, this._message);
}
};
return MessageRunner;
}());
exports.MessageRunner = MessageRunner;
//# sourceMappingURL=MessageRunner.js.map