Event.js
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"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;