UNPKG

@gamestdio/signals

Version:

Minimal Signal implementation for JavaScript.

142 lines (141 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Slot = (function () { function Slot(listener, signal, once, priority) { if (once === void 0) { once = false; } if (priority === void 0) { priority = 0; } this._enabled = true; this._once = false; this._priority = 0; this._listener = listener; this._once = once; this._signal = signal; this._priority = priority; this.verifyListener(listener); } Slot.prototype.execute0 = function () { if (!this._enabled) { return; } if (this._once) { this.remove(); } if (this._params && this._params.length) { this._listener.apply(null, this._params); return; } this._listener(); }; Slot.prototype.execute1 = function (value) { if (!this._enabled) { return; } if (this._once) { this.remove(); } if (this._params && this._params.length) { this._listener.apply(null, [value].concat(this._params)); return; } this._listener(value); }; Slot.prototype.execute = function (valueObjects) { if (!this._enabled) { return; } if (this._once) { this.remove(); } if (this._params && this._params.length) { valueObjects = valueObjects.concat(this._params); } var numValueObjects = valueObjects.length; if (numValueObjects === 0) { this._listener(); } else if (numValueObjects === 1) { this._listener(valueObjects[0]); } else if (numValueObjects === 2) { this._listener(valueObjects[0], valueObjects[1]); } else if (numValueObjects === 3) { this._listener(valueObjects[0], valueObjects[1], valueObjects[2]); } else { this._listener.apply(null, valueObjects); } }; Object.defineProperty(Slot.prototype, "listener", { get: function () { return this._listener; }, set: function (value) { if (null == value) { throw new Error("Given listener is null.\nDid you want to set enabled to false instead?"); } this.verifyListener(value); this._listener = value; }, enumerable: true, configurable: true }); Object.defineProperty(Slot.prototype, "once", { get: function () { return this._once; }, enumerable: true, configurable: true }); Object.defineProperty(Slot.prototype, "priority", { get: function () { return this._priority; }, enumerable: true, configurable: true }); Slot.prototype.toString = function () { return ("[Slot listener: " + this._listener + ", once: " + this._once + ", priority: " + this._priority + ", enabled: " + this._enabled + "]"); }; Object.defineProperty(Slot.prototype, "enabled", { get: function () { return this._enabled; }, set: function (value) { this._enabled = value; }, enumerable: true, configurable: true }); Object.defineProperty(Slot.prototype, "params", { get: function () { return this._params; }, set: function (value) { this._params = value; }, enumerable: true, configurable: true }); Slot.prototype.remove = function () { this._signal.remove(this._listener); }; Slot.prototype.verifyListener = function (listener) { if (null == listener) { throw new Error("Given listener is null."); } if (null == this._signal) { throw new Error("Internal signal reference has not been set yet."); } }; return Slot; }()); exports.Slot = Slot;