UNPKG

typed-event-emitter

Version:

Alternative event emitter for JavaScript and TypeScript.

117 lines (116 loc) 5.28 kB
"use strict"; /****************************************************************************** * The MIT License (MIT) * * * * Copyright (c) 2016 Simon "Tenry" Burchert * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the "Software"), * * to deal in the Software without restriction, including without limitation * * the rights to use, copy, modify, merge, publish, distribute, sublicense, * * and/or sell copies of the Software, and to permit persons to whom the * * Software is furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included in * * all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * * EALINGS IN THE SOFTWARE. * ******************************************************************************/ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Listener = exports.EventEmitter = void 0; var EventEmitter = /** @class */ (function () { function EventEmitter() { this.eventListeners = new Map(); } EventEmitter.prototype.addListener = function (event, callback) { if (!this.eventListeners.has(event)) { this.eventListeners.set(event, [callback]); } else { this.eventListeners.get(event).push(callback); } return new Listener(this, event, callback); }; /** * Alias for `addListener`. */ EventEmitter.prototype.on = function (event, callback) { return this.on(event, callback); }; EventEmitter.prototype.removeListener = function () { if (arguments.length == 0) { // remove all event listeners this.eventListeners.clear(); } else if (arguments.length == 1 && typeof arguments[0] == 'object') { // convert Listener {event, listener} to [event, listener] var listener = arguments[0]; this.removeListener(listener.event, listener.callback); } else if (arguments.length >= 1) { // removeListener(event, listener) var _a = Array.from(arguments), event = _a[0], listener_1 = _a[1]; if (this.eventListeners.has(event)) { var callbacks = this.eventListeners.get(event); callbacks = callbacks.filter(function (otherListener) { return listener_1 != otherListener; }); this.eventListeners.set(event, callbacks); } } }; /** * Emit event. Calls all bound listeners with args. */ EventEmitter.prototype.emit = function (event) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } if (this.eventListeners.has(event)) { // copy array to allow removal of listeners within the callbacks var callbacks = __spreadArray([], this.eventListeners.get(event), true); for (var _a = 0, callbacks_1 = callbacks; _a < callbacks_1.length; _a++) { var callback = callbacks_1[_a]; callback.apply(void 0, args); } } }; /** * @typeparam T The event handler signature. */ EventEmitter.prototype.registerEvent = function () { var _this = this; var eventBinder = function (handler) { return _this.addListener(eventBinder, handler); }; return eventBinder; }; return EventEmitter; }()); exports.EventEmitter = EventEmitter; var Listener = /** @class */ (function () { function Listener(owner, event, callback) { this.owner = owner; this.event = event; this.callback = callback; } Listener.prototype.unbind = function () { this.owner.removeListener(this); }; return Listener; }()); exports.Listener = Listener;