@inweb/eventemitter2 25.3.19 → 25.3.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/eventemitter2.js
CHANGED
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
*/
|
|
32
32
|
class EventEmitter2 {
|
|
33
33
|
constructor() {
|
|
34
|
-
this._listeners =
|
|
34
|
+
this._listeners = {};
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
37
|
* Registers a new listener for an event type.
|
|
@@ -40,8 +40,6 @@
|
|
|
40
40
|
* @param listener - The function that gets called when the event is fired.
|
|
41
41
|
*/
|
|
42
42
|
addEventListener(type, listener) {
|
|
43
|
-
if (this._listeners === undefined)
|
|
44
|
-
this._listeners = {};
|
|
45
43
|
if (this._listeners[type] === undefined)
|
|
46
44
|
this._listeners[type] = [];
|
|
47
45
|
this._listeners[type].push(listener);
|
|
@@ -54,12 +52,13 @@
|
|
|
54
52
|
* @param listener - The listener function that gets removed.
|
|
55
53
|
*/
|
|
56
54
|
removeEventListener(type, listener) {
|
|
57
|
-
if (this._listeners === undefined)
|
|
58
|
-
return this;
|
|
59
55
|
if (this._listeners[type] === undefined)
|
|
60
56
|
return this;
|
|
61
57
|
const listeners = this._listeners[type].filter((x) => x !== listener);
|
|
62
|
-
|
|
58
|
+
if (listeners.length !== 0)
|
|
59
|
+
this._listeners[type] = listeners;
|
|
60
|
+
else
|
|
61
|
+
delete this._listeners[type];
|
|
63
62
|
return this;
|
|
64
63
|
}
|
|
65
64
|
/**
|
|
@@ -70,9 +69,9 @@
|
|
|
70
69
|
*/
|
|
71
70
|
removeAllListeners(type) {
|
|
72
71
|
if (type)
|
|
73
|
-
this._listeners[type]
|
|
72
|
+
delete this._listeners[type];
|
|
74
73
|
else
|
|
75
|
-
this._listeners =
|
|
74
|
+
this._listeners = {};
|
|
76
75
|
return this;
|
|
77
76
|
}
|
|
78
77
|
/**
|
|
@@ -82,8 +81,6 @@
|
|
|
82
81
|
* @param event - The event that gets fired.
|
|
83
82
|
*/
|
|
84
83
|
emitEvent(event) {
|
|
85
|
-
if (this._listeners === undefined)
|
|
86
|
-
return false;
|
|
87
84
|
if (this._listeners[event.type] === undefined)
|
|
88
85
|
return false;
|
|
89
86
|
const invoke = this._listeners[event.type].slice();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventemitter2.js","sources":["../src/index.ts"],"sourcesContent":["///////////////////////////////////////////////////////////////////////////////\n// Copyright (C) 2002-2021, Open Design Alliance (the \"Alliance\").\n// All rights reserved.\n//\n// This software and its documentation and related materials are owned by\n// the Alliance. The software may only be incorporated into application\n// programs owned by members of the Alliance, subject to a signed\n// Membership Agreement and Supplemental Software License Agreement with the\n// Alliance. The structure and organization of this software are the valuable\n// trade secrets of the Alliance and its suppliers. The software is also\n// protected by copyright law and international treaty provisions. Application\n// programs incorporating this software must include the following statement\n// with their copyright notices:\n//\n// This application incorporates Open Design Alliance software pursuant to a\n// license agreement with Open Design Alliance.\n// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.\n// All rights reserved.\n//\n// By use of this software, its documentation or related materials, you\n// acknowledge and accept the above terms.\n///////////////////////////////////////////////////////////////////////////////\n\nexport interface IEvent {\n type: string;\n}\n\nexport interface IEventEmitter {\n addEventListener(type: string, listener: (event: IEvent) => void): this;\n removeEventListener(type: string, listener: (event: IEvent) => void): this;\n removeAllListeners(type?: string): this;\n emitEvent(event: IEvent): boolean;\n on(type: string, listener: (event: any) => void): this;\n off(type: string, listener: (event: any) => void): this;\n emit(event: string | object, ...args: any[]): boolean;\n}\n\n/**\n * The minimal basic Event that can be emitted by a {@link EventEmitter2}.\n */\nexport interface Event<T extends string = string> extends IEvent {\n type: T;\n}\n\n/**\n * Event emitter for custom objects.\n */\nexport class EventEmitter2<EventMap extends Record<string, any> = Record<string, any>> implements IEventEmitter {\n private _listeners =
|
|
1
|
+
{"version":3,"file":"eventemitter2.js","sources":["../src/index.ts"],"sourcesContent":["///////////////////////////////////////////////////////////////////////////////\n// Copyright (C) 2002-2021, Open Design Alliance (the \"Alliance\").\n// All rights reserved.\n//\n// This software and its documentation and related materials are owned by\n// the Alliance. The software may only be incorporated into application\n// programs owned by members of the Alliance, subject to a signed\n// Membership Agreement and Supplemental Software License Agreement with the\n// Alliance. The structure and organization of this software are the valuable\n// trade secrets of the Alliance and its suppliers. The software is also\n// protected by copyright law and international treaty provisions. Application\n// programs incorporating this software must include the following statement\n// with their copyright notices:\n//\n// This application incorporates Open Design Alliance software pursuant to a\n// license agreement with Open Design Alliance.\n// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.\n// All rights reserved.\n//\n// By use of this software, its documentation or related materials, you\n// acknowledge and accept the above terms.\n///////////////////////////////////////////////////////////////////////////////\n\nexport interface IEvent {\n type: string;\n}\n\nexport interface IEventEmitter {\n addEventListener(type: string, listener: (event: IEvent) => void): this;\n removeEventListener(type: string, listener: (event: IEvent) => void): this;\n removeAllListeners(type?: string): this;\n emitEvent(event: IEvent): boolean;\n on(type: string, listener: (event: any) => void): this;\n off(type: string, listener: (event: any) => void): this;\n emit(event: string | object, ...args: any[]): boolean;\n}\n\n/**\n * The minimal basic Event that can be emitted by a {@link EventEmitter2}.\n */\nexport interface Event<T extends string = string> extends IEvent {\n type: T;\n}\n\n/**\n * Event emitter for custom objects.\n */\nexport class EventEmitter2<EventMap extends Record<string, any> = Record<string, any>> implements IEventEmitter {\n private _listeners: Record<any, any[]> = {};\n\n /**\n * Registers a new listener for an event type.\n *\n * @param type - The type of event to listen to.\n * @param listener - The function that gets called when the event is fired.\n */\n addEventListener<T extends keyof EventMap>(type: T, listener: (event: EventMap[T]) => void): this {\n if (this._listeners[type] === undefined) this._listeners[type] = [];\n this._listeners[type].push(listener);\n return this;\n }\n\n /**\n * Removes the listener from an event type.\n *\n * @param type - The type of the listener that gets removed.\n * @param listener - The listener function that gets removed.\n */\n removeEventListener<T extends keyof EventMap>(type: T, listener: (event: EventMap[T]) => void): this {\n if (this._listeners[type] === undefined) return this;\n const listeners = this._listeners[type].filter((x: any) => x !== listener);\n if (listeners.length !== 0) this._listeners[type] = listeners;\n else delete this._listeners[type];\n return this;\n }\n\n /**\n * If `type` is specified, removes all registered listeners for type, otherwise removes all\n * registered listeners.\n *\n * @param type - The type of the listener that gets removed.\n */\n removeAllListeners<T extends keyof EventMap>(type?: T): this {\n if (type) delete this._listeners[type];\n else this._listeners = {};\n return this;\n }\n\n /**\n * Fires the event. Calls each of the listeners registered for the event type `event.type`,\n * in the order they were registered.\n *\n * @param event - The event that gets fired.\n */\n emitEvent<T extends keyof EventMap>(event: Event<Extract<T, string>> & EventMap[T]): boolean {\n if (this._listeners[event.type] === undefined) return false;\n const invoke = this._listeners[event.type].slice();\n invoke.forEach((listener: any) => listener.call(this, event));\n return true;\n }\n\n // Node.js style, to emit custom events\n\n /**\n * Alias to {@link EventEmitter2.addEventListener()}\n */\n on(type: string, listener: (event: any) => void): this {\n return this.addEventListener(type as any, listener as any);\n }\n\n /**\n * Alias to {@link EventEmitter2.removeEventListener()}.\n */\n off(type: string, listener: (event: any) => void): this {\n return this.removeEventListener(type as any, listener as any);\n }\n\n /**\n * Alias to {@link EventEmitter2.emitEvent()}.\n */\n emit(event: string | object, ...args: any[]): boolean {\n if (typeof event === \"string\") return this.emitEvent({ type: event, args } as any);\n else if (typeof event === \"object\") return this.emitEvent(event as any);\n else return false;\n }\n}\n"],"names":[],"mappings":";;;;;;IAAA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IAuBA;;IAEG;UACU,aAAa,CAAA;IAA1B,IAAA,WAAA,GAAA;YACU,IAAU,CAAA,UAAA,GAAuB,EAAE,CAAC;SA6E7C;IA3EC;;;;;IAKG;QACH,gBAAgB,CAA2B,IAAO,EAAE,QAAsC,EAAA;IACxF,QAAA,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,SAAS;IAAE,YAAA,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;YACpE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACrC,QAAA,OAAO,IAAI,CAAC;SACb;IAED;;;;;IAKG;QACH,mBAAmB,CAA2B,IAAO,EAAE,QAAsC,EAAA;IAC3F,QAAA,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,SAAS;IAAE,YAAA,OAAO,IAAI,CAAC;YACrD,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAM,KAAK,CAAC,KAAK,QAAQ,CAAC,CAAC;IAC3E,QAAA,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;IAAE,YAAA,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;;IACzD,YAAA,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAClC,QAAA,OAAO,IAAI,CAAC;SACb;IAED;;;;;IAKG;IACH,IAAA,kBAAkB,CAA2B,IAAQ,EAAA;IACnD,QAAA,IAAI,IAAI;IAAE,YAAA,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;;IAClC,YAAA,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;IAC1B,QAAA,OAAO,IAAI,CAAC;SACb;IAED;;;;;IAKG;IACH,IAAA,SAAS,CAA2B,KAA8C,EAAA;YAChF,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,SAAS;IAAE,YAAA,OAAO,KAAK,CAAC;IAC5D,QAAA,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC;IACnD,QAAA,MAAM,CAAC,OAAO,CAAC,CAAC,QAAa,KAAK,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;IAC9D,QAAA,OAAO,IAAI,CAAC;SACb;;IAID;;IAEG;QACH,EAAE,CAAC,IAAY,EAAE,QAA8B,EAAA;YAC7C,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAW,EAAE,QAAe,CAAC,CAAC;SAC5D;IAED;;IAEG;QACH,GAAG,CAAC,IAAY,EAAE,QAA8B,EAAA;YAC9C,OAAO,IAAI,CAAC,mBAAmB,CAAC,IAAW,EAAE,QAAe,CAAC,CAAC;SAC/D;IAED;;IAEG;IACH,IAAA,IAAI,CAAC,KAAsB,EAAE,GAAG,IAAW,EAAA;YACzC,IAAI,OAAO,KAAK,KAAK,QAAQ;IAAE,YAAA,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAS,CAAC,CAAC;iBAC9E,IAAI,OAAO,KAAK,KAAK,QAAQ;IAAE,YAAA,OAAO,IAAI,CAAC,SAAS,CAAC,KAAY,CAAC,CAAC;;IACnE,YAAA,OAAO,KAAK,CAAC;SACnB;IACF;;;;;;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ODA=e.ODA||{})}(this,(function(e){"use strict";e.EventEmitter2=class{constructor(){this._listeners=
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ODA=e.ODA||{})}(this,(function(e){"use strict";e.EventEmitter2=class{constructor(){this._listeners={}}addEventListener(e,t){return void 0===this._listeners[e]&&(this._listeners[e]=[]),this._listeners[e].push(t),this}removeEventListener(e,t){if(void 0===this._listeners[e])return this;const s=this._listeners[e].filter((e=>e!==t));return 0!==s.length?this._listeners[e]=s:delete this._listeners[e],this}removeAllListeners(e){return e?delete this._listeners[e]:this._listeners={},this}emitEvent(e){if(void 0===this._listeners[e.type])return!1;return this._listeners[e.type].slice().forEach((t=>t.call(this,e))),!0}on(e,t){return this.addEventListener(e,t)}off(e,t){return this.removeEventListener(e,t)}emit(e,...t){return"string"==typeof e?this.emitEvent({type:e,args:t}):"object"==typeof e&&this.emitEvent(e)}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
class EventEmitter2 {
|
|
2
2
|
constructor() {
|
|
3
|
-
this._listeners =
|
|
3
|
+
this._listeners = {};
|
|
4
4
|
}
|
|
5
5
|
addEventListener(type, listener) {
|
|
6
|
-
if (this._listeners === undefined) this._listeners = {};
|
|
7
6
|
if (this._listeners[type] === undefined) this._listeners[type] = [];
|
|
8
7
|
this._listeners[type].push(listener);
|
|
9
8
|
return this;
|
|
10
9
|
}
|
|
11
10
|
removeEventListener(type, listener) {
|
|
12
|
-
if (this._listeners === undefined) return this;
|
|
13
11
|
if (this._listeners[type] === undefined) return this;
|
|
14
12
|
const listeners = this._listeners[type].filter((x => x !== listener));
|
|
15
|
-
this._listeners[type] = listeners
|
|
13
|
+
if (listeners.length !== 0) this._listeners[type] = listeners; else delete this._listeners[type];
|
|
16
14
|
return this;
|
|
17
15
|
}
|
|
18
16
|
removeAllListeners(type) {
|
|
19
|
-
if (type) this._listeners[type]
|
|
17
|
+
if (type) delete this._listeners[type]; else this._listeners = {};
|
|
20
18
|
return this;
|
|
21
19
|
}
|
|
22
20
|
emitEvent(event) {
|
|
23
|
-
if (this._listeners === undefined) return false;
|
|
24
21
|
if (this._listeners[event.type] === undefined) return false;
|
|
25
22
|
const invoke = this._listeners[event.type].slice();
|
|
26
23
|
invoke.forEach((listener => listener.call(this, event)));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventemitter2.module.js","sources":["../src/index.ts"],"sourcesContent":["///////////////////////////////////////////////////////////////////////////////\n// Copyright (C) 2002-2021, Open Design Alliance (the \"Alliance\").\n// All rights reserved.\n//\n// This software and its documentation and related materials are owned by\n// the Alliance. The software may only be incorporated into application\n// programs owned by members of the Alliance, subject to a signed\n// Membership Agreement and Supplemental Software License Agreement with the\n// Alliance. The structure and organization of this software are the valuable\n// trade secrets of the Alliance and its suppliers. The software is also\n// protected by copyright law and international treaty provisions. Application\n// programs incorporating this software must include the following statement\n// with their copyright notices:\n//\n// This application incorporates Open Design Alliance software pursuant to a\n// license agreement with Open Design Alliance.\n// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.\n// All rights reserved.\n//\n// By use of this software, its documentation or related materials, you\n// acknowledge and accept the above terms.\n///////////////////////////////////////////////////////////////////////////////\n\nexport interface IEvent {\n type: string;\n}\n\nexport interface IEventEmitter {\n addEventListener(type: string, listener: (event: IEvent) => void): this;\n removeEventListener(type: string, listener: (event: IEvent) => void): this;\n removeAllListeners(type?: string): this;\n emitEvent(event: IEvent): boolean;\n on(type: string, listener: (event: any) => void): this;\n off(type: string, listener: (event: any) => void): this;\n emit(event: string | object, ...args: any[]): boolean;\n}\n\n/**\n * The minimal basic Event that can be emitted by a {@link EventEmitter2}.\n */\nexport interface Event<T extends string = string> extends IEvent {\n type: T;\n}\n\n/**\n * Event emitter for custom objects.\n */\nexport class EventEmitter2<EventMap extends Record<string, any> = Record<string, any>> implements IEventEmitter {\n private _listeners =
|
|
1
|
+
{"version":3,"file":"eventemitter2.module.js","sources":["../src/index.ts"],"sourcesContent":["///////////////////////////////////////////////////////////////////////////////\n// Copyright (C) 2002-2021, Open Design Alliance (the \"Alliance\").\n// All rights reserved.\n//\n// This software and its documentation and related materials are owned by\n// the Alliance. The software may only be incorporated into application\n// programs owned by members of the Alliance, subject to a signed\n// Membership Agreement and Supplemental Software License Agreement with the\n// Alliance. The structure and organization of this software are the valuable\n// trade secrets of the Alliance and its suppliers. The software is also\n// protected by copyright law and international treaty provisions. Application\n// programs incorporating this software must include the following statement\n// with their copyright notices:\n//\n// This application incorporates Open Design Alliance software pursuant to a\n// license agreement with Open Design Alliance.\n// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.\n// All rights reserved.\n//\n// By use of this software, its documentation or related materials, you\n// acknowledge and accept the above terms.\n///////////////////////////////////////////////////////////////////////////////\n\nexport interface IEvent {\n type: string;\n}\n\nexport interface IEventEmitter {\n addEventListener(type: string, listener: (event: IEvent) => void): this;\n removeEventListener(type: string, listener: (event: IEvent) => void): this;\n removeAllListeners(type?: string): this;\n emitEvent(event: IEvent): boolean;\n on(type: string, listener: (event: any) => void): this;\n off(type: string, listener: (event: any) => void): this;\n emit(event: string | object, ...args: any[]): boolean;\n}\n\n/**\n * The minimal basic Event that can be emitted by a {@link EventEmitter2}.\n */\nexport interface Event<T extends string = string> extends IEvent {\n type: T;\n}\n\n/**\n * Event emitter for custom objects.\n */\nexport class EventEmitter2<EventMap extends Record<string, any> = Record<string, any>> implements IEventEmitter {\n private _listeners: Record<any, any[]> = {};\n\n /**\n * Registers a new listener for an event type.\n *\n * @param type - The type of event to listen to.\n * @param listener - The function that gets called when the event is fired.\n */\n addEventListener<T extends keyof EventMap>(type: T, listener: (event: EventMap[T]) => void): this {\n if (this._listeners[type] === undefined) this._listeners[type] = [];\n this._listeners[type].push(listener);\n return this;\n }\n\n /**\n * Removes the listener from an event type.\n *\n * @param type - The type of the listener that gets removed.\n * @param listener - The listener function that gets removed.\n */\n removeEventListener<T extends keyof EventMap>(type: T, listener: (event: EventMap[T]) => void): this {\n if (this._listeners[type] === undefined) return this;\n const listeners = this._listeners[type].filter((x: any) => x !== listener);\n if (listeners.length !== 0) this._listeners[type] = listeners;\n else delete this._listeners[type];\n return this;\n }\n\n /**\n * If `type` is specified, removes all registered listeners for type, otherwise removes all\n * registered listeners.\n *\n * @param type - The type of the listener that gets removed.\n */\n removeAllListeners<T extends keyof EventMap>(type?: T): this {\n if (type) delete this._listeners[type];\n else this._listeners = {};\n return this;\n }\n\n /**\n * Fires the event. Calls each of the listeners registered for the event type `event.type`,\n * in the order they were registered.\n *\n * @param event - The event that gets fired.\n */\n emitEvent<T extends keyof EventMap>(event: Event<Extract<T, string>> & EventMap[T]): boolean {\n if (this._listeners[event.type] === undefined) return false;\n const invoke = this._listeners[event.type].slice();\n invoke.forEach((listener: any) => listener.call(this, event));\n return true;\n }\n\n // Node.js style, to emit custom events\n\n /**\n * Alias to {@link EventEmitter2.addEventListener()}\n */\n on(type: string, listener: (event: any) => void): this {\n return this.addEventListener(type as any, listener as any);\n }\n\n /**\n * Alias to {@link EventEmitter2.removeEventListener()}.\n */\n off(type: string, listener: (event: any) => void): this {\n return this.removeEventListener(type as any, listener as any);\n }\n\n /**\n * Alias to {@link EventEmitter2.emitEvent()}.\n */\n emit(event: string | object, ...args: any[]): boolean {\n if (typeof event === \"string\") return this.emitEvent({ type: event, args } as any);\n else if (typeof event === \"object\") return this.emitEvent(event as any);\n else return false;\n }\n}\n"],"names":["EventEmitter2","constructor","this","_listeners","addEventListener","type","listener","undefined","push","removeEventListener","listeners","filter","x","length","removeAllListeners","emitEvent","event","invoke","slice","forEach","call","on","off","emit","args"],"mappings":"MA+CaA;IAAb,WAAAC;QACUC,KAAUC,aAAuB;AA6E1C;IArEC,gBAAAC,CAA2CC,MAASC;QAClD,IAAIJ,KAAKC,WAAWE,UAAUE,WAAWL,KAAKC,WAAWE,QAAQ;QACjEH,KAAKC,WAAWE,MAAMG,KAAKF;QAC3B,OAAOJ;AACR;IAQD,mBAAAO,CAA8CJ,MAASC;QACrD,IAAIJ,KAAKC,WAAWE,UAAUE,WAAW,OAAOL;QAChD,MAAMQ,YAAYR,KAAKC,WAAWE,MAAMM,QAAQC,KAAWA,MAAMN;QACjE,IAAII,UAAUG,WAAW,GAAGX,KAAKC,WAAWE,QAAQK,uBACxCR,KAAKC,WAAWE;QAC5B,OAAOH;AACR;IAQD,kBAAAY,CAA6CT;QAC3C,IAAIA,aAAaH,KAAKC,WAAWE,YAC5BH,KAAKC,aAAa;QACvB,OAAOD;AACR;IAQD,SAAAa,CAAoCC;QAClC,IAAId,KAAKC,WAAWa,MAAMX,UAAUE,WAAW,OAAO;QACtD,MAAMU,SAASf,KAAKC,WAAWa,MAAMX,MAAMa;QAC3CD,OAAOE,SAASb,YAAkBA,SAASc,KAAKlB,MAAMc;QACtD,OAAO;AACR;IAOD,EAAAK,CAAGhB,MAAcC;QACf,OAAOJ,KAAKE,iBAAiBC,MAAaC;AAC3C;IAKD,GAAAgB,CAAIjB,MAAcC;QAChB,OAAOJ,KAAKO,oBAAoBJ,MAAaC;AAC9C;IAKD,IAAAiB,CAAKP,UAA2BQ;QAC9B,WAAWR,UAAU,UAAU,OAAOd,KAAKa,UAAU;YAAEV,MAAMW;YAAOQ;iBAC/D,WAAWR,UAAU,UAAU,OAAOd,KAAKa,UAAUC,aACrD,OAAO;AACb;;;"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -46,7 +46,7 @@ export interface Event<T extends string = string> extends IEvent {
|
|
|
46
46
|
* Event emitter for custom objects.
|
|
47
47
|
*/
|
|
48
48
|
export class EventEmitter2<EventMap extends Record<string, any> = Record<string, any>> implements IEventEmitter {
|
|
49
|
-
private _listeners =
|
|
49
|
+
private _listeners: Record<any, any[]> = {};
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* Registers a new listener for an event type.
|
|
@@ -55,7 +55,6 @@ export class EventEmitter2<EventMap extends Record<string, any> = Record<string,
|
|
|
55
55
|
* @param listener - The function that gets called when the event is fired.
|
|
56
56
|
*/
|
|
57
57
|
addEventListener<T extends keyof EventMap>(type: T, listener: (event: EventMap[T]) => void): this {
|
|
58
|
-
if (this._listeners === undefined) this._listeners = {};
|
|
59
58
|
if (this._listeners[type] === undefined) this._listeners[type] = [];
|
|
60
59
|
this._listeners[type].push(listener);
|
|
61
60
|
return this;
|
|
@@ -68,10 +67,10 @@ export class EventEmitter2<EventMap extends Record<string, any> = Record<string,
|
|
|
68
67
|
* @param listener - The listener function that gets removed.
|
|
69
68
|
*/
|
|
70
69
|
removeEventListener<T extends keyof EventMap>(type: T, listener: (event: EventMap[T]) => void): this {
|
|
71
|
-
if (this._listeners === undefined) return this;
|
|
72
70
|
if (this._listeners[type] === undefined) return this;
|
|
73
|
-
const listeners = this._listeners[type].filter((x) => x !== listener);
|
|
74
|
-
|
|
71
|
+
const listeners = this._listeners[type].filter((x: any) => x !== listener);
|
|
72
|
+
if (listeners.length !== 0) this._listeners[type] = listeners;
|
|
73
|
+
else delete this._listeners[type];
|
|
75
74
|
return this;
|
|
76
75
|
}
|
|
77
76
|
|
|
@@ -82,8 +81,8 @@ export class EventEmitter2<EventMap extends Record<string, any> = Record<string,
|
|
|
82
81
|
* @param type - The type of the listener that gets removed.
|
|
83
82
|
*/
|
|
84
83
|
removeAllListeners<T extends keyof EventMap>(type?: T): this {
|
|
85
|
-
if (type) this._listeners[type]
|
|
86
|
-
else this._listeners =
|
|
84
|
+
if (type) delete this._listeners[type];
|
|
85
|
+
else this._listeners = {};
|
|
87
86
|
return this;
|
|
88
87
|
}
|
|
89
88
|
|
|
@@ -94,10 +93,9 @@ export class EventEmitter2<EventMap extends Record<string, any> = Record<string,
|
|
|
94
93
|
* @param event - The event that gets fired.
|
|
95
94
|
*/
|
|
96
95
|
emitEvent<T extends keyof EventMap>(event: Event<Extract<T, string>> & EventMap[T]): boolean {
|
|
97
|
-
if (this._listeners === undefined) return false;
|
|
98
96
|
if (this._listeners[event.type] === undefined) return false;
|
|
99
97
|
const invoke = this._listeners[event.type].slice();
|
|
100
|
-
invoke.forEach((listener) => listener.call(this, event));
|
|
98
|
+
invoke.forEach((listener: any) => listener.call(this, event));
|
|
101
99
|
return true;
|
|
102
100
|
}
|
|
103
101
|
|