"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var factories = []; factories[0] = function () { return function dispatcher0() { }; }; factories[1] = function (callback, context) { if (typeof (context) === 'undefined') return callback; return function dispatcher1(payload) { callback(payload, context); }; }; function getFactory(handlerCount) { if (!factories[handlerCount]) factories[handlerCount] = compileFactory(handlerCount); return factories[handlerCount]; } function compileFactory(handlerCount) { var src = 'return function dispatcher' + handlerCount + '(payload) {\n'; var argsHandlers = [], argsContexts = []; for (var i = 0; i < handlerCount; i++) { argsHandlers.push('cb' + i); argsContexts.push('ctx' + i); src += ' cb' + i + '(payload, ctx' + i + ');\n'; } src += '};'; return new (Function.bind.apply(Function, [void 0].concat(argsHandlers.concat(argsContexts), [src])))(); } var Event = /** @class */ (function () { function Event() { this.hasHandlers = false; this._handlers = []; this._contexts = []; this._createDispatcher(); } Event.prototype.addHandler = function (handler, context) { if (!this.isHandlerAttached(handler, context)) { this._handlers.push(handler); this._contexts.push(context); this._createDispatcher(); this._updateHasHandlers(); } return this; }; Event.prototype.removeHandler = function (handler, context) { var idx = this._getHandlerIndex(handler, context); if (typeof (idx) !== 'undefined') { this._handlers.splice(idx, 1); this._contexts.splice(idx, 1); this._createDispatcher(); this._updateHasHandlers(); } return this; }; Event.prototype.isHandlerAttached = function (handler, context) { return typeof (this._getHandlerIndex(handler, context)) !== 'undefined'; }; Event.prototype._updateHasHandlers = function () { this.hasHandlers = !!this._handlers.length; }; Event.prototype._getHandlerIndex = function (handler, context) { var handlerCount = this._handlers.length; var idx; for (idx = 0; idx < handlerCount; idx++) { if (this._handlers[idx] === handler && this._contexts[idx] === context) break; } return idx < handlerCount ? idx : undefined; }; Event.prototype._createDispatcher = function () { this.dispatch = getFactory(this._handlers.length).apply(this, this._handlers.concat(this._contexts)); }; return Event; }()); exports.default = Event;