@reactway/tiny-emitter
Version:
Small change emitter
34 lines • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var TinyEmitter = /** @class */ (function () {
function TinyEmitter() {
this.registry = [];
}
TinyEmitter.prototype.addListener = function (handler) {
var _this = this;
this.registry.push(handler);
return function () { return _this.removeListener(handler); };
};
TinyEmitter.prototype.removeListener = function (handler) {
if (this.registry.length === 0) {
return;
}
this.registry = this.registry.filter(function (handlerItem) { return handlerItem !== handler; });
};
TinyEmitter.prototype.emit = function () {
var payload = [];
for (var _i = 0; _i < arguments.length; _i++) {
payload[_i] = arguments[_i];
}
for (var _a = 0, _b = this.registry; _a < _b.length; _a++) {
var handler = _b[_a];
handler.apply(void 0, payload);
}
};
TinyEmitter.prototype.getListenersCount = function () {
return this.registry.length;
};
return TinyEmitter;
}());
exports.TinyEmitter = TinyEmitter;
//# sourceMappingURL=tiny-emitter.js.map