@irrelon/emitter 5.0.2 → 5.0.4
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/.eslintrc.json +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/src/Emitter.d.ts +202 -0
- package/{src → dist/cjs/src}/Emitter.js +50 -129
- package/dist/cjs/tests/Emitter.test.js +357 -0
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/src/Emitter.d.ts +202 -0
- package/dist/mjs/src/Emitter.js +483 -0
- package/dist/mjs/tests/Emitter.test.d.ts +1 -0
- package/dist/mjs/tests/Emitter.test.js +336 -0
- package/dist/mjs/tsconfig.tsbuildinfo +1 -0
- package/fixup.sh +11 -0
- package/license.md +11 -0
- package/package.json +10 -4
- package/readme.md +19 -19
- package/src/Emitter.ts +133 -113
- package/tests/Emitter.test.ts +132 -32
- package/tsconfig-base.json +36 -0
- package/tsconfig-cjs.json +8 -0
- package/tsconfig.json +5 -47
- package/src/Emitter.d.ts +0 -190
- package/tests/Emitter.test.js +0 -277
- /package/{tests → dist/cjs/tests}/Emitter.test.d.ts +0 -0
package/.eslintrc.json
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"@typescript-eslint/no-empty-arrow-function": "off",
|
|
17
17
|
"@typescript-eslint/no-empty-function": "off",
|
|
18
18
|
"@typescript-eslint/no-inferrable-types": "off",
|
|
19
|
+
"@typescript-eslint/no-non-null-assertion": "off",
|
|
19
20
|
"@typescript-eslint/strict-boolean-expressions": "off",
|
|
20
21
|
"@typescript-eslint/ban-ts-comment": "off",
|
|
21
22
|
"@typescript-eslint/no-this-alias": "off",
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
export declare type AnyFunction<ReturnType = any> = (...args: any[]) => ReturnType;
|
|
2
|
+
export declare type AnyConstructor = new (...args: any[]) => any;
|
|
3
|
+
export declare enum EventReturnFlag {
|
|
4
|
+
none = 0,
|
|
5
|
+
cancel = 1
|
|
6
|
+
}
|
|
7
|
+
export declare type EventListenerCallback<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> = (...args: Parameters<EventCallbackFunctionSignature>) => ReturnType<EventCallbackFunctionSignature>;
|
|
8
|
+
export interface EventStaticEmitterObject<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> {
|
|
9
|
+
id: string;
|
|
10
|
+
args: Parameters<EventCallbackFunctionSignature>;
|
|
11
|
+
}
|
|
12
|
+
export interface EmitterEventsInterface {
|
|
13
|
+
[key: string]: AnyFunction;
|
|
14
|
+
}
|
|
15
|
+
export declare type ConvertInterfaceToDict<T> = {
|
|
16
|
+
[K in keyof T]: T[K];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new class with the capability to emit events.
|
|
20
|
+
*/
|
|
21
|
+
export declare class Emitter<EventsInterface extends EmitterEventsInterface = EmitterEventsInterface> {
|
|
22
|
+
_eventsEmitting: boolean;
|
|
23
|
+
_eventRemovalQueue: any[];
|
|
24
|
+
_eventListeners?: {
|
|
25
|
+
[EventName in (keyof EventsInterface)]?: Record<string, EventListenerCallback<EventsInterface[EventName]>[]>;
|
|
26
|
+
};
|
|
27
|
+
_eventStaticEmitters: {
|
|
28
|
+
[EventName in (keyof EventsInterface)]?: EventStaticEmitterObject<EventsInterface[EventName]>[];
|
|
29
|
+
};
|
|
30
|
+
_eventsAllowDefer: boolean;
|
|
31
|
+
_eventsDeferTimeouts: Record<any, number>;
|
|
32
|
+
/**
|
|
33
|
+
* Attach an event listener to the passed event only if the passed
|
|
34
|
+
* id matches the id for the event being fired.
|
|
35
|
+
* @param eventName The name of the event to listen for.
|
|
36
|
+
* @param id The id to match against.
|
|
37
|
+
* @param listener The method to call when the event is fired.
|
|
38
|
+
* @returns The emitter instance.
|
|
39
|
+
*/
|
|
40
|
+
_on<EventName extends keyof EventsInterface>(eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
41
|
+
/**
|
|
42
|
+
* Attach an event listener to the passed event only if the passed
|
|
43
|
+
* id matches the document id for the event being fired.
|
|
44
|
+
* @param eventName The name of the event to listen for.
|
|
45
|
+
* @param id The id to match against.
|
|
46
|
+
* @param listener The method to call when the event is fired.
|
|
47
|
+
* @returns The emitter instance.
|
|
48
|
+
*/
|
|
49
|
+
_once<EventName extends keyof EventsInterface>(eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
50
|
+
/**
|
|
51
|
+
* Cancels an event listener based on an event name, id and listener function.
|
|
52
|
+
* @param eventName The event to cancel listener for.
|
|
53
|
+
* @param id The ID of the event to cancel listening for.
|
|
54
|
+
* @param listener The event listener function used in the on()
|
|
55
|
+
* or once() call to cancel.
|
|
56
|
+
* @returns The emitter instance.
|
|
57
|
+
*/
|
|
58
|
+
_off<EventName extends keyof EventsInterface>(eventName: EventName, id: string, listener?: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
59
|
+
/**
|
|
60
|
+
* Attach an event listener to the passed event only if the passed
|
|
61
|
+
* id matches the document id for the event being fired.
|
|
62
|
+
* @param eventName The name of the event to listen for.
|
|
63
|
+
* @param id The id to match against.
|
|
64
|
+
* @param listener The method to call when the event is fired.
|
|
65
|
+
* @returns The emitter instance.
|
|
66
|
+
*/
|
|
67
|
+
on<EventName extends keyof EventsInterface>(eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
68
|
+
on<EventName extends keyof EventsInterface>(eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
69
|
+
/**
|
|
70
|
+
* Attach an event listener to the passed event only if the passed
|
|
71
|
+
* id matches the document id for the event being fired.
|
|
72
|
+
* @param eventName The name of the event to listen for.
|
|
73
|
+
* @param id The id to match against.
|
|
74
|
+
* @param listener
|
|
75
|
+
* @returns The emitter instance.
|
|
76
|
+
*/
|
|
77
|
+
once<EventName extends keyof EventsInterface>(eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
78
|
+
once<EventName extends keyof EventsInterface>(eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
79
|
+
/**
|
|
80
|
+
* Overwrites any previous event listeners so that if the event fires, only
|
|
81
|
+
* the listener you pass will be called. If you pass an id along with the
|
|
82
|
+
* listener, only listeners for the id will be overwritten. This is similar
|
|
83
|
+
* to calling `.off(eventName)` then `on(eventName, listener)` since
|
|
84
|
+
* `off(eventName)` will cancel any previous event listeners for the passed
|
|
85
|
+
* event name.
|
|
86
|
+
* @param eventName The name of the event to listen for.
|
|
87
|
+
* @param id The id to match against.
|
|
88
|
+
* @param listener The method to call when the event is fired.
|
|
89
|
+
* @returns The emitter instance.
|
|
90
|
+
*/
|
|
91
|
+
overwrite<EventName extends keyof EventsInterface>(eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
92
|
+
overwrite<EventName extends keyof EventsInterface>(eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
93
|
+
/**
|
|
94
|
+
* Cancels an event listener based on an event name, id and listener function.
|
|
95
|
+
* @param eventName The event to cancel listener for.
|
|
96
|
+
* @param id The ID of the event to cancel listening for.
|
|
97
|
+
* @param listener The event listener function used in the on()
|
|
98
|
+
* or once() call to cancel.
|
|
99
|
+
* @returns The emitter instance.
|
|
100
|
+
*/
|
|
101
|
+
off<EventName extends keyof EventsInterface>(eventName: EventName, id: string, listener?: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
102
|
+
off<EventName extends keyof EventsInterface>(eventName: EventName, listener?: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
103
|
+
off<EventName extends keyof EventsInterface>(eventName: EventName): this;
|
|
104
|
+
/**
|
|
105
|
+
* Emit an event by name.
|
|
106
|
+
* @param eventName The name of the event to emit.
|
|
107
|
+
* @param {...any} data The arguments to send to any listening methods.
|
|
108
|
+
* If you are sending multiple arguments, separate them with a comma so
|
|
109
|
+
* that they are received by the function as separate arguments.
|
|
110
|
+
* @return {number}
|
|
111
|
+
* @example #Emit an Event
|
|
112
|
+
* // Emit the event named "hello"
|
|
113
|
+
* myEntity.emit('hello');
|
|
114
|
+
* @example #Emit an Event With Data Object
|
|
115
|
+
* // Emit the event named "hello"
|
|
116
|
+
* myEntity.emit('hello', {moo: true});
|
|
117
|
+
* @example #Emit an Event With Multiple Data Values
|
|
118
|
+
* // Emit the event named "hello"
|
|
119
|
+
* myEntity.emit('hello', {moo: true}, 'someString');
|
|
120
|
+
* @example #Listen for Event Data
|
|
121
|
+
* // Set a listener to listen for the data (multiple values emitted
|
|
122
|
+
* // from an event are passed as function arguments)
|
|
123
|
+
* myEntity.on('hello', function (arg1, arg2) {
|
|
124
|
+
* console.log(arg1, arg2);
|
|
125
|
+
* }
|
|
126
|
+
*
|
|
127
|
+
* // Emit the event named "hello"
|
|
128
|
+
* myEntity.emit('hello', 'data1', 'data2');
|
|
129
|
+
*
|
|
130
|
+
* // The console output is:
|
|
131
|
+
* // data1, data2
|
|
132
|
+
*/
|
|
133
|
+
emit<EventName extends keyof EventsInterface>(eventName: EventName, ...data: Parameters<EventsInterface[EventName]>): Promise<EventReturnFlag>;
|
|
134
|
+
emitId<EventName extends keyof EventsInterface>(eventName: EventName, id: string, ...data: Parameters<EventsInterface[EventName]>): EventReturnFlag;
|
|
135
|
+
/**
|
|
136
|
+
* Creates a persistent emitter record that will fire a listener if
|
|
137
|
+
* one is added for this event after the emitStatic() call has been
|
|
138
|
+
* made.
|
|
139
|
+
* @param eventName The name of the event to emit.
|
|
140
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
141
|
+
* @returns The emitter instance.
|
|
142
|
+
* @private
|
|
143
|
+
*/
|
|
144
|
+
emitStatic<EventName extends keyof EventsInterface>(eventName: EventName, ...data: Parameters<EventsInterface[EventName]>): this;
|
|
145
|
+
/**
|
|
146
|
+
* Creates a persistent emitter record that will fire a listener if
|
|
147
|
+
* one is added for this event after the emitStatic() call has been
|
|
148
|
+
* made.
|
|
149
|
+
* @param eventName The name of the event to emit.
|
|
150
|
+
* @param id The id of the event to emit.
|
|
151
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
152
|
+
* @returns The emitter instance.
|
|
153
|
+
* @private
|
|
154
|
+
*/
|
|
155
|
+
emitStaticId<EventName extends keyof EventsInterface>(eventName: EventName, id: string, ...data: Parameters<EventsInterface[EventName]>): this;
|
|
156
|
+
/**
|
|
157
|
+
* Handles removing emitters, is an internal method not called directly.
|
|
158
|
+
* @param eventName The event to remove static emitter for.
|
|
159
|
+
* @returns The emitter instance.
|
|
160
|
+
* @private
|
|
161
|
+
*/
|
|
162
|
+
cancelStatic<EventName extends keyof EventsInterface>(eventName: EventName): this;
|
|
163
|
+
/**
|
|
164
|
+
* Checks if an event has any event listeners or not.
|
|
165
|
+
* @param eventName The name of the event to check for.
|
|
166
|
+
* @returns True if one or more event listeners are registered for
|
|
167
|
+
* the event. False if none are found.
|
|
168
|
+
*/
|
|
169
|
+
willEmit<EventName extends keyof EventsInterface>(eventName: EventName): boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Checks if an event has any event listeners or not based on the passed id.
|
|
172
|
+
* @param eventName The name of the event to check for.
|
|
173
|
+
* @param id The event ID to check for.
|
|
174
|
+
* @returns True if one or more event listeners are registered for
|
|
175
|
+
* the event. False if none are found.
|
|
176
|
+
*/
|
|
177
|
+
willEmitId<EventName extends keyof EventsInterface>(eventName: EventName, id: string): boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Queues an event to be fired. This has automatic de-bouncing so that any
|
|
180
|
+
* events of the same type that occur within 100 milliseconds of a previous
|
|
181
|
+
* one will all be wrapped into a single emit rather than emitting tons of
|
|
182
|
+
* events for lots of chained inserts etc. Only the data from the last
|
|
183
|
+
* de-bounced event will be emitted.
|
|
184
|
+
* @param eventName The name of the event to emit.
|
|
185
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
186
|
+
* @returns The emitter instance.
|
|
187
|
+
*/
|
|
188
|
+
deferEmit<EventName extends keyof EventsInterface>(eventName: EventName, ...data: Parameters<EventsInterface[EventName]>): this;
|
|
189
|
+
/**
|
|
190
|
+
* If events are cleared with the off() method while the event emitter is
|
|
191
|
+
* actively processing any events then the off() calls get added to a
|
|
192
|
+
* queue to be executed after the event emitter is finished. This stops
|
|
193
|
+
* errors that might occur by potentially modifying the event queue while
|
|
194
|
+
* the emitter is running through them. This method is called after the
|
|
195
|
+
* event emitter is finished processing.
|
|
196
|
+
* @private
|
|
197
|
+
*/
|
|
198
|
+
_processRemovalQueue(): void;
|
|
199
|
+
}
|
|
200
|
+
export declare function makeEmitter(obj: AnyConstructor, prototypeMode: boolean): Emitter;
|
|
201
|
+
export declare function makeEmitter(obj: boolean): Emitter;
|
|
202
|
+
export declare function makeEmitter(obj: Record<string, unknown>, prototypeMode: boolean): Emitter;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -7,104 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
9
|
});
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
Copyright (c) 2014 Irrelon Software Limited
|
|
14
|
-
https://www.irrelon.com
|
|
15
|
-
|
|
16
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
17
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
18
|
-
in the Software without restriction, including without limitation the rights
|
|
19
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
20
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
21
|
-
furnished to do so, subject to the following conditions:
|
|
22
|
-
|
|
23
|
-
The above copyright notice, url and this permission notice shall be included in
|
|
24
|
-
all copies or substantial portions of the Software.
|
|
25
|
-
|
|
26
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
27
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
28
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
29
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
30
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
31
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
32
|
-
THE SOFTWARE.
|
|
33
|
-
|
|
34
|
-
Source: https://github.com/irrelon/emitter
|
|
35
|
-
|
|
36
|
-
Changelog:
|
|
37
|
-
Version 5.0.2:
|
|
38
|
-
Removed some unnecessary code, updated to be in step with version
|
|
39
|
-
that is being used in the new version of Isogenic Game Engine.
|
|
40
|
-
Version 5.0.1:
|
|
41
|
-
Further TypeScript updates.
|
|
42
|
-
Version 5.0.0:
|
|
43
|
-
Added TypeScript and prettier, removed babel etc
|
|
44
|
-
Version 4.0.1:
|
|
45
|
-
Updated library to use new ES6 functionality making Overload()
|
|
46
|
-
less useful so it can be removed as a dependency.
|
|
47
|
-
Version 4.0.0:
|
|
48
|
-
Breaking change. Library now has named exports `Emitter` and
|
|
49
|
-
`makeEmitter`. `Emitter` is an ES6 class that can be extended
|
|
50
|
-
and `makeEmitter` is a function that takes an object or class
|
|
51
|
-
and does what `Emitter(someObject)` used to do. This change is
|
|
52
|
-
to support being able to extend `Emitter` as a base class.
|
|
53
|
-
Version 3.1.0:
|
|
54
|
-
Changed order of execution so that listeners that are listening
|
|
55
|
-
against a specific ID get called before the general catch-all
|
|
56
|
-
listeners. Renamed package to @irrelon/emitter.
|
|
57
|
-
Version 2.0.11:
|
|
58
|
-
Added cancelStatic method to allow cancelling a static event
|
|
59
|
-
Version 2.0.7:
|
|
60
|
-
Fixed UMD module support
|
|
61
|
-
Version 2.0.6:
|
|
62
|
-
Added UMD module support
|
|
63
|
-
Version 2.0.5:
|
|
64
|
-
Added bower version number
|
|
65
|
-
Version 2.0.4:
|
|
66
|
-
Allow instantiation as independent instance, updated unit tests
|
|
67
|
-
Version 2.0.3:
|
|
68
|
-
Documentation updates, published to bower
|
|
69
|
-
Version 2.0.2:
|
|
70
|
-
Documentation updates
|
|
71
|
-
Version 2.0.1:
|
|
72
|
-
Bug fix in this._emitters usage
|
|
73
|
-
Version 2.0.0:
|
|
74
|
-
Big update to bring in line with latest developments in other projects. Event emitter can
|
|
75
|
-
now use deferEmit(), emitId(), emitStatic(), emitStaticId(), willEmit(), willEmitId().
|
|
76
|
-
Version 1.1.9:
|
|
77
|
-
Updated changelog correctly
|
|
78
|
-
Version 1.1.8:
|
|
79
|
-
Removed tons of dependencies wrongly included in main dependencies, have moved to devDependencies section of package.json
|
|
80
|
-
Version 1.1.0:
|
|
81
|
-
Added support for overloaded methods
|
|
82
|
-
Added support for events with ids
|
|
83
|
-
Version 1.0.2:
|
|
84
|
-
Removed AMD support, added browserify support
|
|
85
|
-
Added package.json
|
|
86
|
-
Added once() method
|
|
87
|
-
Added hasListener() method
|
|
88
|
-
Published to NPM as irrelon-emitter
|
|
89
|
-
Version 1.0.1:
|
|
90
|
-
Added ability to extend any object with eventing capability
|
|
91
|
-
Added AMD / Require.js support
|
|
92
|
-
Added Node.js support
|
|
93
|
-
Version 1.0.0:
|
|
94
|
-
First commit
|
|
95
|
-
*/
|
|
96
|
-
export var EventReturnFlag;
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.makeEmitter = exports.Emitter = exports.EventReturnFlag = void 0;
|
|
13
|
+
var EventReturnFlag;
|
|
97
14
|
(function (EventReturnFlag) {
|
|
98
15
|
EventReturnFlag[EventReturnFlag["none"] = 0] = "none";
|
|
99
16
|
EventReturnFlag[EventReturnFlag["cancel"] = 1] = "cancel";
|
|
100
|
-
})(EventReturnFlag || (EventReturnFlag = {}));
|
|
17
|
+
})(EventReturnFlag = exports.EventReturnFlag || (exports.EventReturnFlag = {}));
|
|
101
18
|
/**
|
|
102
19
|
* Creates a new class with the capability to emit events.
|
|
103
20
|
*/
|
|
104
|
-
|
|
21
|
+
class Emitter {
|
|
105
22
|
constructor() {
|
|
106
23
|
this._eventsEmitting = false;
|
|
107
24
|
this._eventRemovalQueue = [];
|
|
25
|
+
this._eventListeners = {};
|
|
108
26
|
this._eventStaticEmitters = {};
|
|
109
27
|
this._eventsAllowDefer = false;
|
|
110
28
|
this._eventsDeferTimeouts = {};
|
|
@@ -112,10 +30,10 @@ export class Emitter {
|
|
|
112
30
|
/**
|
|
113
31
|
* Attach an event listener to the passed event only if the passed
|
|
114
32
|
* id matches the id for the event being fired.
|
|
115
|
-
* @param
|
|
116
|
-
* @param
|
|
117
|
-
* @param
|
|
118
|
-
* @returns
|
|
33
|
+
* @param eventName The name of the event to listen for.
|
|
34
|
+
* @param id The id to match against.
|
|
35
|
+
* @param listener The method to call when the event is fired.
|
|
36
|
+
* @returns The emitter instance.
|
|
119
37
|
*/
|
|
120
38
|
_on(eventName, id, listener) {
|
|
121
39
|
const generateTimeout = (emitter) => {
|
|
@@ -124,9 +42,9 @@ export class Emitter {
|
|
|
124
42
|
}, 1);
|
|
125
43
|
};
|
|
126
44
|
this._eventListeners = this._eventListeners || {};
|
|
127
|
-
this._eventListeners[eventName] = this._eventListeners[eventName] || {};
|
|
128
|
-
|
|
129
|
-
|
|
45
|
+
const nonNullableEventListeners = this._eventListeners[eventName] = this._eventListeners[eventName] || {};
|
|
46
|
+
nonNullableEventListeners[id] = nonNullableEventListeners[id] || [];
|
|
47
|
+
nonNullableEventListeners[id].push(listener);
|
|
130
48
|
// Check for any static emitters, and fire the event if any exist
|
|
131
49
|
if (!this._eventStaticEmitters || !this._eventStaticEmitters[eventName] || !this._eventStaticEmitters[eventName].length)
|
|
132
50
|
return this;
|
|
@@ -145,29 +63,31 @@ export class Emitter {
|
|
|
145
63
|
/**
|
|
146
64
|
* Attach an event listener to the passed event only if the passed
|
|
147
65
|
* id matches the document id for the event being fired.
|
|
148
|
-
* @param
|
|
149
|
-
* @param
|
|
150
|
-
* @param
|
|
151
|
-
* @returns
|
|
66
|
+
* @param eventName The name of the event to listen for.
|
|
67
|
+
* @param id The id to match against.
|
|
68
|
+
* @param listener The method to call when the event is fired.
|
|
69
|
+
* @returns The emitter instance.
|
|
152
70
|
*/
|
|
153
71
|
_once(eventName, id, listener) {
|
|
154
72
|
let fired = false;
|
|
73
|
+
let originalReturnData;
|
|
155
74
|
const internalCallback = (...args) => {
|
|
156
75
|
if (fired)
|
|
157
|
-
return;
|
|
76
|
+
return originalReturnData;
|
|
158
77
|
fired = true;
|
|
159
78
|
this.off(eventName, id, internalCallback);
|
|
160
|
-
listener(...args);
|
|
79
|
+
originalReturnData = listener(...args);
|
|
80
|
+
return originalReturnData;
|
|
161
81
|
};
|
|
162
82
|
return this.on(eventName, id, internalCallback);
|
|
163
83
|
}
|
|
164
84
|
/**
|
|
165
85
|
* Cancels an event listener based on an event name, id and listener function.
|
|
166
|
-
* @param
|
|
167
|
-
* @param
|
|
168
|
-
* @param
|
|
86
|
+
* @param eventName The event to cancel listener for.
|
|
87
|
+
* @param id The ID of the event to cancel listening for.
|
|
88
|
+
* @param listener The event listener function used in the on()
|
|
169
89
|
* or once() call to cancel.
|
|
170
|
-
* @returns
|
|
90
|
+
* @returns The emitter instance.
|
|
171
91
|
*/
|
|
172
92
|
_off(eventName, id, listener) {
|
|
173
93
|
// If the event name doesn't have any listeners, exit early
|
|
@@ -202,12 +122,11 @@ export class Emitter {
|
|
|
202
122
|
}
|
|
203
123
|
return this;
|
|
204
124
|
}
|
|
205
|
-
on(eventName,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
return this._on(eventName, "*", rest[0]);
|
|
125
|
+
on(eventName, idOrListener, listener) {
|
|
126
|
+
if (typeof idOrListener === "string") {
|
|
127
|
+
return this._on(eventName, idOrListener, listener);
|
|
209
128
|
}
|
|
210
|
-
return this._on(eventName,
|
|
129
|
+
return this._on(eventName, "*", idOrListener);
|
|
211
130
|
}
|
|
212
131
|
once(eventName, ...rest) {
|
|
213
132
|
const restTypes = rest.map((arg) => typeof arg);
|
|
@@ -242,7 +161,7 @@ export class Emitter {
|
|
|
242
161
|
}
|
|
243
162
|
/**
|
|
244
163
|
* Emit an event by name.
|
|
245
|
-
* @param
|
|
164
|
+
* @param eventName The name of the event to emit.
|
|
246
165
|
* @param {...any} data The arguments to send to any listening methods.
|
|
247
166
|
* If you are sending multiple arguments, separate them with a comma so
|
|
248
167
|
* that they are received by the function as separate arguments.
|
|
@@ -338,9 +257,9 @@ export class Emitter {
|
|
|
338
257
|
* Creates a persistent emitter record that will fire a listener if
|
|
339
258
|
* one is added for this event after the emitStatic() call has been
|
|
340
259
|
* made.
|
|
341
|
-
* @param
|
|
260
|
+
* @param eventName The name of the event to emit.
|
|
342
261
|
* @param {...any} data Optional arguments to emit with the event.
|
|
343
|
-
* @returns
|
|
262
|
+
* @returns The emitter instance.
|
|
344
263
|
* @private
|
|
345
264
|
*/
|
|
346
265
|
emitStatic(eventName, ...data) {
|
|
@@ -373,10 +292,10 @@ export class Emitter {
|
|
|
373
292
|
* Creates a persistent emitter record that will fire a listener if
|
|
374
293
|
* one is added for this event after the emitStatic() call has been
|
|
375
294
|
* made.
|
|
376
|
-
* @param
|
|
377
|
-
* @param
|
|
295
|
+
* @param eventName The name of the event to emit.
|
|
296
|
+
* @param id The id of the event to emit.
|
|
378
297
|
* @param {...any} data Optional arguments to emit with the event.
|
|
379
|
-
* @returns
|
|
298
|
+
* @returns The emitter instance.
|
|
380
299
|
* @private
|
|
381
300
|
*/
|
|
382
301
|
emitStaticId(eventName, id, ...data) {
|
|
@@ -422,8 +341,8 @@ export class Emitter {
|
|
|
422
341
|
}
|
|
423
342
|
/**
|
|
424
343
|
* Handles removing emitters, is an internal method not called directly.
|
|
425
|
-
* @param
|
|
426
|
-
* @returns
|
|
344
|
+
* @param eventName The event to remove static emitter for.
|
|
345
|
+
* @returns The emitter instance.
|
|
427
346
|
* @private
|
|
428
347
|
*/
|
|
429
348
|
cancelStatic(eventName) {
|
|
@@ -433,8 +352,8 @@ export class Emitter {
|
|
|
433
352
|
}
|
|
434
353
|
/**
|
|
435
354
|
* Checks if an event has any event listeners or not.
|
|
436
|
-
* @param
|
|
437
|
-
* @returns
|
|
355
|
+
* @param eventName The name of the event to check for.
|
|
356
|
+
* @returns True if one or more event listeners are registered for
|
|
438
357
|
* the event. False if none are found.
|
|
439
358
|
*/
|
|
440
359
|
willEmit(eventName) {
|
|
@@ -455,9 +374,9 @@ export class Emitter {
|
|
|
455
374
|
}
|
|
456
375
|
/**
|
|
457
376
|
* Checks if an event has any event listeners or not based on the passed id.
|
|
458
|
-
* @param
|
|
459
|
-
* @param
|
|
460
|
-
* @returns
|
|
377
|
+
* @param eventName The name of the event to check for.
|
|
378
|
+
* @param id The event ID to check for.
|
|
379
|
+
* @returns True if one or more event listeners are registered for
|
|
461
380
|
* the event. False if none are found.
|
|
462
381
|
*/
|
|
463
382
|
willEmitId(eventName, id) {
|
|
@@ -496,9 +415,9 @@ export class Emitter {
|
|
|
496
415
|
* one will all be wrapped into a single emit rather than emitting tons of
|
|
497
416
|
* events for lots of chained inserts etc. Only the data from the last
|
|
498
417
|
* de-bounced event will be emitted.
|
|
499
|
-
* @param
|
|
418
|
+
* @param eventName The name of the event to emit.
|
|
500
419
|
* @param {...any} data Optional arguments to emit with the event.
|
|
501
|
-
* @returns
|
|
420
|
+
* @returns The emitter instance.
|
|
502
421
|
*/
|
|
503
422
|
deferEmit(eventName, ...data) {
|
|
504
423
|
if (!this._eventsAllowDefer) {
|
|
@@ -509,11 +428,11 @@ export class Emitter {
|
|
|
509
428
|
}
|
|
510
429
|
// Set a timeout
|
|
511
430
|
this._eventsDeferTimeouts[eventName] = setTimeout(() => {
|
|
512
|
-
this.emit.call(this, eventName, ...data);
|
|
431
|
+
void this.emit.call(this, eventName, ...data);
|
|
513
432
|
}, 1);
|
|
514
433
|
}
|
|
515
434
|
else {
|
|
516
|
-
this.emit.call(this, eventName, ...data);
|
|
435
|
+
void this.emit.call(this, eventName, ...data);
|
|
517
436
|
}
|
|
518
437
|
return this;
|
|
519
438
|
}
|
|
@@ -538,7 +457,8 @@ export class Emitter {
|
|
|
538
457
|
this._eventRemovalQueue = [];
|
|
539
458
|
}
|
|
540
459
|
}
|
|
541
|
-
|
|
460
|
+
exports.Emitter = Emitter;
|
|
461
|
+
function makeEmitter(obj, prototypeMode) {
|
|
542
462
|
let operateOnObject;
|
|
543
463
|
if (obj === undefined && prototypeMode === undefined) {
|
|
544
464
|
obj = {};
|
|
@@ -578,3 +498,4 @@ export function makeEmitter(obj, prototypeMode) {
|
|
|
578
498
|
operateOnObject._processRemovalQueue = Emitter.prototype._processRemovalQueue;
|
|
579
499
|
return obj;
|
|
580
500
|
}
|
|
501
|
+
exports.makeEmitter = makeEmitter;
|