events-ex 2.1.0 → 2.2.0
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/.npmrcx +5 -0
- package/CHANGELOG.md +14 -0
- package/README.cn.md +112 -180
- package/README.md +133 -151
- package/docs/README.md +136 -150
- package/docs/all-off/README.md +17 -0
- package/docs/all-off/functions/allOff.md +33 -0
- package/docs/consts/README.md +12 -0
- package/docs/consts/variables/RegExpEventSymbol.md +11 -0
- package/docs/consts/variables/states.md +29 -0
- package/docs/default-methods/README.md +17 -0
- package/docs/default-methods/functions/getEventableMethods.md +301 -0
- package/docs/event/README.md +17 -0
- package/docs/event/classes/Event.md +143 -0
- package/docs/event-emitter/README-1.md +17 -0
- package/docs/event-emitter/README.md +17 -0
- package/docs/event-emitter/classes/EventEmitter-1.md +29 -0
- package/docs/event-emitter/classes/EventEmitter.md +349 -0
- package/docs/eventable/README.md +17 -0
- package/docs/eventable/functions/eventable.md +82 -0
- package/docs/has-listeners/README.md +17 -0
- package/docs/has-listeners/functions/hasListeners.md +38 -0
- package/docs/index/README.md +85 -0
- package/docs/modules.md +24 -23
- package/docs/pipe/README.md +17 -0
- package/docs/pipe/functions/pipe.md +41 -0
- package/docs/pipe-async/README.md +17 -0
- package/docs/pipe-async/functions/pipeAsync.md +41 -0
- package/docs/unify/README.md +17 -0
- package/docs/unify/functions/unify.md +35 -0
- package/docs/util/array-remove/README.md +17 -0
- package/docs/util/array-remove/functions/remove.md +21 -0
- package/docs/util/object-for-each/README.md +17 -0
- package/docs/util/object-for-each/functions/forEach.md +29 -0
- package/docs/util/promise-any/README.md +11 -0
- package/docs/util/promise-any/variables/default.md +9 -0
- package/docs/util/string-pad/README.md +17 -0
- package/docs/util/string-pad/functions/pad.md +25 -0
- package/docs/util/to-int/README.md +17 -0
- package/docs/util/to-int/functions/toInt.md +21 -0
- package/docs/util/valid-callable/README.md +17 -0
- package/docs/util/valid-callable/functions/validCallable.md +21 -0
- package/docs/util/valid-object/README.md +17 -0
- package/docs/util/valid-object/functions/validObject.md +21 -0
- package/docs/wrap-event-emitter/README.md +21 -0
- package/docs/wrap-event-emitter/functions/wrapEventEmitter.md +33 -0
- package/docs/wrap-event-emitter/variables/methods.md +11 -0
- package/lib/all-off.d.ts +1 -1
- package/lib/all-off.js +1 -1
- package/lib/default-methods.d.ts +28 -5
- package/lib/default-methods.js +114 -22
- package/lib/event-emitter.d.ts +17 -0
- package/lib/event.d.ts +9 -3
- package/lib/event.js +6 -0
- package/lib/eventable.js +4 -1
- package/lib/pipe-async.d.ts +4 -1
- package/lib/pipe-async.js +39 -5
- package/lib/pipe.d.ts +1 -1
- package/lib/pipe.js +1 -1
- package/lib/unify.d.ts +1 -1
- package/lib/unify.js +1 -1
- package/lib/util/array-remove.js +1 -1
- package/lib/util/object-for-each.js +1 -1
- package/lib/util/promise-any.d.ts +1 -0
- package/lib/util/promise-any.js +44 -0
- package/lib/util/string-pad.js +1 -1
- package/lib/wrap-event-emitter.d.ts +2 -1
- package/lib/wrap-event-emitter.js +24 -3
- package/package.json +17 -17
- package/src/default-methods.js +112 -15
- package/src/event-emitter.d.ts +17 -0
- package/src/event.js +6 -0
- package/src/eventable.js +3 -0
- package/src/pipe-async.js +64 -30
- package/src/util/promise-any.js +45 -0
- package/src/wrap-event-emitter.js +23 -18
- package/docs/.nojekyll +0 -1
- package/docs/classes/event.Event.md +0 -141
- package/docs/classes/event_emitter-1.EventEmitter.md +0 -29
- package/docs/classes/event_emitter.EventEmitter.md +0 -306
- package/docs/modules/all_off.md +0 -44
- package/docs/modules/consts.md +0 -39
- package/docs/modules/default_methods.md +0 -51
- package/docs/modules/event.md +0 -19
- package/docs/modules/event_emitter-1.md +0 -19
- package/docs/modules/event_emitter.md +0 -19
- package/docs/modules/eventable.md +0 -92
- package/docs/modules/has_listeners.md +0 -49
- package/docs/modules/index.md +0 -99
- package/docs/modules/pipe.md +0 -49
- package/docs/modules/pipe_async.md +0 -49
- package/docs/modules/unify.md +0 -46
- package/docs/modules/util_array_remove.md +0 -39
- package/docs/modules/util_object_for_each.md +0 -41
- package/docs/modules/util_string_pad.md +0 -40
- package/docs/modules/util_to_int.md +0 -39
- package/docs/modules/util_valid_callable.md +0 -39
- package/docs/modules/util_valid_object.md +0 -39
- package/docs/modules/wrap_event_emitter.md +0 -57
package/src/default-methods.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {defineProperty, isArray, isFunction, isNumber, isObject, isRegExp as _isRegExp, isUndefined, isRegExpStr, toRegExp } from 'util-ex'
|
|
2
|
+
import './util/promise-any'
|
|
2
3
|
import {RegExpEventSymbol} from './consts'
|
|
3
4
|
import {Event} from './event';
|
|
4
5
|
|
|
@@ -13,6 +14,50 @@ function isRegExp(value) {
|
|
|
13
14
|
|
|
14
15
|
export function getEventableMethods(aClass) {
|
|
15
16
|
return {
|
|
17
|
+
/**
|
|
18
|
+
* Configures the event emitter with specified options using a Fluent API.
|
|
19
|
+
* @param {Object} options - Configuration options for event emission.
|
|
20
|
+
* @param {string} [options.asyncMode='serial'] - The mode of asynchronous emission ('serial' or 'parallel').
|
|
21
|
+
* @param {string} [options.resultMode='last'] - The strategy for handling multiple return values ('last', 'first', 'collect').
|
|
22
|
+
* @returns {import('./event-emitter').EventEmitter} A proxy object representing the configured EventEmitter.
|
|
23
|
+
*/
|
|
24
|
+
configure(options) {
|
|
25
|
+
const proxy = Object.create(this)
|
|
26
|
+
const mergedOptions = Object.assign({}, this._eeRuntimeOptions, options)
|
|
27
|
+
defineProperty(proxy, '_eeRuntimeOptions', mergedOptions)
|
|
28
|
+
return proxy
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A shortcut for parallel configuration.
|
|
33
|
+
* @param {string} [resultMode='last'] - The strategy for handling multiple return values.
|
|
34
|
+
* @returns {import('./event-emitter').EventEmitter} A proxy object representing the configured EventEmitter.
|
|
35
|
+
*/
|
|
36
|
+
parallel(resultMode) {
|
|
37
|
+
return this.configure({asyncMode: 'parallel', resultMode: resultMode || 'last'})
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Sets the configuration options for the EventEmitter instance.
|
|
42
|
+
* @param {Object} options - Configuration options for the emitter (e.g., asyncMode, resultMode, maxListeners).
|
|
43
|
+
* @returns {import('./event-emitter').EventEmitter} The EventEmitter instance for chaining.
|
|
44
|
+
*/
|
|
45
|
+
setEmitterOptions(options) {
|
|
46
|
+
if (!isObject(options)) {return this}
|
|
47
|
+
let data
|
|
48
|
+
if (!this.hasOwnProperty('_emitterOptions')) {
|
|
49
|
+
data = create(null)
|
|
50
|
+
defineProperty(this, '_emitterOptions', data)
|
|
51
|
+
} else {
|
|
52
|
+
data = this._emitterOptions
|
|
53
|
+
}
|
|
54
|
+
Object.assign(data, options)
|
|
55
|
+
if (!isUndefined(options.maxListeners) && isFunction(this.setMaxListeners)) {
|
|
56
|
+
this.setMaxListeners(options.maxListeners)
|
|
57
|
+
}
|
|
58
|
+
return this
|
|
59
|
+
},
|
|
60
|
+
|
|
16
61
|
/**
|
|
17
62
|
* Adds a listener function to the specified event type.
|
|
18
63
|
* @param {string|RegExp} type - The event type to listen for.
|
|
@@ -143,22 +188,9 @@ export function getEventableMethods(aClass) {
|
|
|
143
188
|
const args = r.args
|
|
144
189
|
const listeners = r.listeners
|
|
145
190
|
const evt = Event(this, r.type)
|
|
146
|
-
const
|
|
191
|
+
const options = Object.assign({}, this._emitterOptions, this._eeRuntimeOptions)
|
|
147
192
|
try {
|
|
148
|
-
|
|
149
|
-
try {
|
|
150
|
-
await _notify(listener, evt, args);
|
|
151
|
-
if (evt.stopped) {break}
|
|
152
|
-
} catch(err) {
|
|
153
|
-
errs.push({err: err, listener: listener})
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (errs.length) {
|
|
157
|
-
for (let i=0;i<errs.length;i++) {
|
|
158
|
-
const it = errs[i]
|
|
159
|
-
this.emit('error', it.err, 'notify', r.type, it.listener, args)
|
|
160
|
-
}
|
|
161
|
-
}
|
|
193
|
+
await _executeAsync.call(this, listeners, evt, args, options)
|
|
162
194
|
} finally {
|
|
163
195
|
// eslint-disable-next-line no-unsafe-finally
|
|
164
196
|
return evt.end()
|
|
@@ -381,3 +413,68 @@ function _notify(listener, evt, args) {
|
|
|
381
413
|
}
|
|
382
414
|
return result
|
|
383
415
|
}
|
|
416
|
+
|
|
417
|
+
async function _executeAsync(listeners, evt, args, options) {
|
|
418
|
+
const asyncMode = options.asyncMode || 'serial'
|
|
419
|
+
const resultMode = options.resultMode || 'last'
|
|
420
|
+
const errs = []
|
|
421
|
+
|
|
422
|
+
if (resultMode === 'collect') {
|
|
423
|
+
evt.result = []
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const notifyListener = async (listener) => {
|
|
427
|
+
try {
|
|
428
|
+
const result = await _notify(listener, evt, args)
|
|
429
|
+
if (result !== undefined) {
|
|
430
|
+
if (resultMode === 'first' && !evt.resolved) {
|
|
431
|
+
evt.result = result
|
|
432
|
+
evt.resolved = true
|
|
433
|
+
} else if (resultMode === 'last') {
|
|
434
|
+
evt.result = result
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return result
|
|
438
|
+
} catch (err) {
|
|
439
|
+
errs.push({err, listener})
|
|
440
|
+
throw err
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (asyncMode === 'parallel') {
|
|
445
|
+
const promises = listeners.map(listener => notifyListener(listener))
|
|
446
|
+
if (resultMode === 'collect') {
|
|
447
|
+
evt.result = await Promise.all(promises.map(p => p.catch(() => undefined)))
|
|
448
|
+
} else if (resultMode === 'first') {
|
|
449
|
+
try {
|
|
450
|
+
await Promise.any(promises.map(p => p.then(res => res === undefined ? Promise.reject() : res)))
|
|
451
|
+
} catch (e) {
|
|
452
|
+
// If all rejected or returned undefined, ignore
|
|
453
|
+
}
|
|
454
|
+
} else {
|
|
455
|
+
await Promise.all(promises.map(p => p.catch(() => undefined)))
|
|
456
|
+
}
|
|
457
|
+
} else {
|
|
458
|
+
// Serial mode (default)
|
|
459
|
+
for (const listener of listeners) {
|
|
460
|
+
try {
|
|
461
|
+
const result = await notifyListener(listener)
|
|
462
|
+
if (resultMode === 'collect') {
|
|
463
|
+
evt.result.push(result)
|
|
464
|
+
}
|
|
465
|
+
if (evt.stopped || (resultMode === 'first' && evt.resolved)) break
|
|
466
|
+
} catch (err) {
|
|
467
|
+
if (resultMode === 'collect') {
|
|
468
|
+
evt.result.push(undefined)
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (errs.length) {
|
|
475
|
+
for (let i = 0; i < errs.length; i++) {
|
|
476
|
+
const it = errs[i]
|
|
477
|
+
this.emit('error', it.err, 'notify', evt.type, it.listener, args)
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
package/src/event-emitter.d.ts
CHANGED
|
@@ -28,8 +28,18 @@ export class EventEmitter {
|
|
|
28
28
|
* @param {Function} listener - The listener function to be removed.
|
|
29
29
|
* @returns {EventEmitter} The EventEmitter instance to allow chaining.
|
|
30
30
|
* @throws {TypeError} If the listener is not a function.
|
|
31
|
+
* @see {@link removeListener}
|
|
31
32
|
*/
|
|
32
33
|
off(eventName: string|RegExp, listener: ListenerCallbackFunc): EventEmitter;
|
|
34
|
+
/**
|
|
35
|
+
* Removes a listener function from the specified event type.
|
|
36
|
+
* @param {string|RegExp} type - The event type to remove the listener from.
|
|
37
|
+
* @param {Function} listener - The listener function to be removed.
|
|
38
|
+
* @returns {EventEmitter} The EventEmitter instance to allow chaining.
|
|
39
|
+
* @throws {TypeError} If the listener is not a function.
|
|
40
|
+
* @see {@link off}
|
|
41
|
+
*/
|
|
42
|
+
removeListener(eventName: string|RegExp, listener: ListenerCallbackFunc): EventEmitter;
|
|
33
43
|
/**
|
|
34
44
|
* Emits the specified event type with the given arguments.
|
|
35
45
|
* @param {...*} args - The event type followed by any number of arguments to be passed to the listener functions.
|
|
@@ -49,6 +59,13 @@ export class EventEmitter {
|
|
|
49
59
|
* @returns {EventEmitter} - The event emitter with all listeners removed.
|
|
50
60
|
*/
|
|
51
61
|
removeAllListeners(eventName?: string|RegExp): EventEmitter;
|
|
62
|
+
/**
|
|
63
|
+
* Sets the maximum number of listeners allowed for the event emitter.
|
|
64
|
+
*
|
|
65
|
+
* @param {number} n - The maximum number of listeners to set. Must be a positive integer.
|
|
66
|
+
* @returns {EventEmitter} The {@link EventEmitter} instance for method chaining.
|
|
67
|
+
* @throws {TypeError} If `n` is not a positive integer.
|
|
68
|
+
*/
|
|
52
69
|
setMaxListeners(n: number): EventEmitter;
|
|
53
70
|
/**
|
|
54
71
|
* Returns an array of functions that are registered to listen for the specified event.
|
package/src/event.js
CHANGED
|
@@ -30,6 +30,12 @@ Event.prototype.init = function(target, type) {
|
|
|
30
30
|
* @public
|
|
31
31
|
*/
|
|
32
32
|
this.stopped = false
|
|
33
|
+
/**
|
|
34
|
+
* Whether a result has been resolved (for 'first' result mode)
|
|
35
|
+
* @type {boolean}
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
this.resolved = false
|
|
33
39
|
/**
|
|
34
40
|
* Keep your event result here if any.
|
|
35
41
|
* @type {*}
|
package/src/eventable.js
CHANGED
|
@@ -25,6 +25,9 @@ function getEventableClass(aClass) {
|
|
|
25
25
|
Eventable.prototype.listenerCount = methods.listenerCount;
|
|
26
26
|
Eventable.prototype.emit = methods.emit;
|
|
27
27
|
Eventable.prototype.emitAsync = methods.emitAsync;
|
|
28
|
+
Eventable.prototype.configure = methods.configure;
|
|
29
|
+
Eventable.prototype.parallel = methods.parallel;
|
|
30
|
+
Eventable.prototype.setEmitterOptions = methods.setEmitterOptions;
|
|
28
31
|
Eventable.prototype.on = methods.on;
|
|
29
32
|
Eventable.prototype.addListener = methods.on;
|
|
30
33
|
Eventable.prototype.off = methods.off;
|
package/src/pipe-async.js
CHANGED
|
@@ -15,39 +15,73 @@ const emit = methods.emitAsync
|
|
|
15
15
|
* @param {import('./event-emitter').EventEmitter} e1 - The first event emitter.
|
|
16
16
|
* @param {import('./event-emitter').EventEmitter} e2 - The second event emitter.
|
|
17
17
|
* @param {string} [name='emitAsync'] - The name of the event to pipe (defaults to 'emitAsync').
|
|
18
|
+
* @param {Object} [options] - Configuration for the pipeline.
|
|
19
|
+
* @param {string} [options.asyncMode='serial'] - The mode of propagation ('serial' or 'parallel').
|
|
20
|
+
* @param {string} [options.resultMode] - Strategy for aggregating results from the pipe chain ('collect', 'first').
|
|
18
21
|
* @returns {Object} - An object with a `close` method that removes the pipeline between the two event emitters.
|
|
19
22
|
* @throws {TypeError} - If either of the arguments is not an event emitter object.
|
|
20
23
|
*/
|
|
21
|
-
export function pipeAsync(e1, e2/* , name */) {
|
|
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
|
-
|
|
24
|
+
export function pipeAsync(e1, e2/* , name, options */) {
|
|
25
|
+
let pipes
|
|
26
|
+
|
|
27
|
+
(validObject(e1) && validObject(e2))
|
|
28
|
+
let name = arguments[2]
|
|
29
|
+
let options = arguments[3]
|
|
30
|
+
if (typeof name === 'object') {
|
|
31
|
+
options = name
|
|
32
|
+
name = undefined
|
|
33
|
+
}
|
|
34
|
+
if (name === undefined) {name = 'emitAsync'}
|
|
35
|
+
if (!options) {options = {}}
|
|
36
|
+
|
|
37
|
+
const result = {
|
|
38
|
+
close() { arrRemove.call(pipes, e2) }
|
|
39
|
+
};
|
|
40
|
+
if (hasOwnProperty.call(e1, '__eePipes__')) {
|
|
41
|
+
(pipes = e1.__eePipes__).push(e2)
|
|
42
|
+
return result
|
|
43
|
+
}
|
|
44
|
+
defineProperty(e1, '__eePipes__', pipes = [e2])
|
|
45
|
+
let desc = getOwnPropertyDescriptor(e1, name)
|
|
46
|
+
if (!desc) {
|
|
47
|
+
desc = {}
|
|
48
|
+
} else {
|
|
49
|
+
delete desc.get
|
|
50
|
+
delete desc.set
|
|
51
|
+
}
|
|
52
|
+
desc.value = async function () {
|
|
53
|
+
const data = arrFrom(pipes)
|
|
54
|
+
const asyncMode = options.asyncMode || 'serial'
|
|
55
|
+
const resultMode = options.resultMode
|
|
56
|
+
|
|
57
|
+
const forward = async (target, args) => {
|
|
58
|
+
const fn = target[name] || target.emitAsync || emit
|
|
59
|
+
return fn.apply(target, args)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (asyncMode === 'parallel') {
|
|
63
|
+
const promises = [emit.apply(this, arguments)]
|
|
64
|
+
for (let i = 0; i < data.length; ++i) {
|
|
65
|
+
promises.push(forward(data[i], arguments))
|
|
66
|
+
}
|
|
67
|
+
const results = await Promise.all(promises)
|
|
68
|
+
if (resultMode === 'collect') return results
|
|
69
|
+
if (resultMode === 'first') return results.find(r => r !== undefined)
|
|
70
|
+
return results[0] // Default: return main emitter's result
|
|
71
|
+
} else {
|
|
72
|
+
const mainResult = await emit.apply(this, arguments)
|
|
73
|
+
const allResults = [mainResult]
|
|
74
|
+
for (let i = 0; i < data.length; ++i) {
|
|
75
|
+
const res = await forward(data[i], arguments)
|
|
76
|
+
allResults.push(res)
|
|
77
|
+
}
|
|
78
|
+
if (resultMode === 'collect') return allResults
|
|
79
|
+
if (resultMode === 'first') return allResults.find(r => r !== undefined)
|
|
80
|
+
return mainResult // Default: return main emitter's result
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
defineProperty(e1, name, desc.value, desc)
|
|
84
|
+
return result
|
|
51
85
|
};
|
|
52
86
|
|
|
53
87
|
export default pipeAsync
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple polyfill for AggregateError if it doesn't exist.
|
|
3
|
+
*/
|
|
4
|
+
const _AggregateError = typeof AggregateError !== 'undefined'
|
|
5
|
+
? AggregateError
|
|
6
|
+
: function AggregateError(errors, message) {
|
|
7
|
+
const error = new Error(message);
|
|
8
|
+
error.name = 'AggregateError';
|
|
9
|
+
error.errors = errors;
|
|
10
|
+
return error;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Ensures Promise.any exists, or provides a polyfill.
|
|
15
|
+
*/
|
|
16
|
+
if (typeof Promise.any !== 'function') {
|
|
17
|
+
Promise.any = function (promises) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
promises = Array.from(promises);
|
|
20
|
+
const len = promises.length;
|
|
21
|
+
let errors = [];
|
|
22
|
+
let rejectedCount = 0;
|
|
23
|
+
|
|
24
|
+
if (len === 0) {
|
|
25
|
+
return reject(new _AggregateError(errors, 'All promises were rejected'));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
promises.forEach((promise, index) => {
|
|
29
|
+
Promise.resolve(promise)
|
|
30
|
+
.then((value) => {
|
|
31
|
+
resolve(value);
|
|
32
|
+
})
|
|
33
|
+
.catch((error) => {
|
|
34
|
+
errors[index] = error;
|
|
35
|
+
rejectedCount++;
|
|
36
|
+
if (rejectedCount === len) {
|
|
37
|
+
reject(new _AggregateError(errors, 'All promises were rejected'));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default Promise.any;
|
|
@@ -6,36 +6,41 @@ const defineProperties = Object.defineProperties;
|
|
|
6
6
|
|
|
7
7
|
export const methods = eventable().methods;
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Minimal set of core methods to be injected into an existing object.
|
|
11
|
+
*/
|
|
9
12
|
const descriptors = {
|
|
10
|
-
on: {
|
|
11
|
-
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
},
|
|
16
|
-
off: {
|
|
17
|
-
value: methods.off
|
|
18
|
-
},
|
|
19
|
-
emit: {
|
|
20
|
-
value: methods.emit
|
|
21
|
-
},
|
|
22
|
-
emitAsync: {
|
|
23
|
-
value: methods.emitAsync
|
|
24
|
-
},
|
|
13
|
+
on: { value: methods.on },
|
|
14
|
+
once: { value: methods.once },
|
|
15
|
+
off: { value: methods.off },
|
|
16
|
+
emit: { value: methods.emit },
|
|
17
|
+
emitAsync: { value: methods.emitAsync },
|
|
18
|
+
setEmitterOptions: { value: methods.setEmitterOptions },
|
|
25
19
|
};
|
|
26
20
|
|
|
27
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Full set of all available methods from eventable for standalone instances.
|
|
23
|
+
*/
|
|
24
|
+
const fullDescriptors = {};
|
|
25
|
+
Object.keys(methods).forEach(key => {
|
|
26
|
+
fullDescriptors[key] = { value: methods[key] };
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const base = defineProperties({}, fullDescriptors);
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
32
|
* Create or inject the eventable instance into the object
|
|
31
33
|
* @param {Object} [o] the optional instance to eventable
|
|
34
|
+
* @param {Object} [options] optional configuration for the emitter
|
|
32
35
|
* @returns o or new Event instance
|
|
33
36
|
*/
|
|
34
|
-
export function wrapEventEmitter(o) {
|
|
37
|
+
export function wrapEventEmitter(o, options) {
|
|
35
38
|
const result = o == null ? create(base) : defineProperties(Object(o), descriptors)
|
|
36
39
|
defineProperty(result, '_events', {})
|
|
40
|
+
if (options && options.emitterOptions) {
|
|
41
|
+
result.setEmitterOptions(options.emitterOptions)
|
|
42
|
+
}
|
|
37
43
|
return result
|
|
38
44
|
};
|
|
39
45
|
|
|
40
|
-
|
|
41
46
|
export default wrapEventEmitter
|
package/docs/.nojekyll
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
[events-ex](../README.md) / [Exports](../modules.md) / [event](../modules/event.md) / Event
|
|
2
|
-
|
|
3
|
-
# Class: Event
|
|
4
|
-
|
|
5
|
-
[event](../modules/event.md).Event
|
|
6
|
-
|
|
7
|
-
## Table of contents
|
|
8
|
-
|
|
9
|
-
### Constructors
|
|
10
|
-
|
|
11
|
-
- [constructor](event.Event.md#constructor)
|
|
12
|
-
|
|
13
|
-
### Properties
|
|
14
|
-
|
|
15
|
-
- [result](event.Event.md#result)
|
|
16
|
-
- [stopped](event.Event.md#stopped)
|
|
17
|
-
- [target](event.Event.md#target)
|
|
18
|
-
- [type](event.Event.md#type)
|
|
19
|
-
|
|
20
|
-
### Methods
|
|
21
|
-
|
|
22
|
-
- [end](event.Event.md#end)
|
|
23
|
-
- [init](event.Event.md#init)
|
|
24
|
-
|
|
25
|
-
## Constructors
|
|
26
|
-
|
|
27
|
-
### constructor
|
|
28
|
-
|
|
29
|
-
• **new Event**(`target`, `type`): [`Event`](event.Event.md)
|
|
30
|
-
|
|
31
|
-
Creates a new Event object instance that contains information about the event, such as the target element and the return value of the event.
|
|
32
|
-
|
|
33
|
-
#### Parameters
|
|
34
|
-
|
|
35
|
-
| Name | Type | Description |
|
|
36
|
-
| :------ | :------ | :------ |
|
|
37
|
-
| `target` | [`EventEmitter`](event_emitter.EventEmitter.md) | Who trigger the event |
|
|
38
|
-
| `type` | `any` | - |
|
|
39
|
-
|
|
40
|
-
#### Returns
|
|
41
|
-
|
|
42
|
-
[`Event`](event.Event.md)
|
|
43
|
-
|
|
44
|
-
- The new Event instance.
|
|
45
|
-
|
|
46
|
-
**`Classdesc`**
|
|
47
|
-
|
|
48
|
-
Event Object that contains information about the event, such as the target element and the return value of the event.
|
|
49
|
-
|
|
50
|
-
#### Defined in
|
|
51
|
-
|
|
52
|
-
[src/event.js:8](https://github.com/snowyu/events-ex.js/blob/a6cce2d/src/event.js#L8)
|
|
53
|
-
|
|
54
|
-
## Properties
|
|
55
|
-
|
|
56
|
-
### result
|
|
57
|
-
|
|
58
|
-
• **result**: `any`
|
|
59
|
-
|
|
60
|
-
Keep your event result here if any.
|
|
61
|
-
|
|
62
|
-
#### Defined in
|
|
63
|
-
|
|
64
|
-
[src/event.js:38](https://github.com/snowyu/events-ex.js/blob/a6cce2d/src/event.js#L38)
|
|
65
|
-
|
|
66
|
-
___
|
|
67
|
-
|
|
68
|
-
### stopped
|
|
69
|
-
|
|
70
|
-
• **stopped**: `boolean`
|
|
71
|
-
|
|
72
|
-
Whether stop the bubbling event
|
|
73
|
-
|
|
74
|
-
#### Defined in
|
|
75
|
-
|
|
76
|
-
[src/event.js:32](https://github.com/snowyu/events-ex.js/blob/a6cce2d/src/event.js#L32)
|
|
77
|
-
|
|
78
|
-
___
|
|
79
|
-
|
|
80
|
-
### target
|
|
81
|
-
|
|
82
|
-
• **target**: `any`
|
|
83
|
-
|
|
84
|
-
Who trigger the event
|
|
85
|
-
|
|
86
|
-
#### Defined in
|
|
87
|
-
|
|
88
|
-
[src/event.js:26](https://github.com/snowyu/events-ex.js/blob/a6cce2d/src/event.js#L26)
|
|
89
|
-
|
|
90
|
-
___
|
|
91
|
-
|
|
92
|
-
### type
|
|
93
|
-
|
|
94
|
-
• **type**: `string`
|
|
95
|
-
|
|
96
|
-
The type of the event.
|
|
97
|
-
|
|
98
|
-
#### Defined in
|
|
99
|
-
|
|
100
|
-
[src/event.js:44](https://github.com/snowyu/events-ex.js/blob/a6cce2d/src/event.js#L44)
|
|
101
|
-
|
|
102
|
-
## Methods
|
|
103
|
-
|
|
104
|
-
### end
|
|
105
|
-
|
|
106
|
-
▸ **end**(): `any`
|
|
107
|
-
|
|
108
|
-
Ends the event and returns the result.
|
|
109
|
-
|
|
110
|
-
#### Returns
|
|
111
|
-
|
|
112
|
-
`any`
|
|
113
|
-
|
|
114
|
-
The result of the event.
|
|
115
|
-
|
|
116
|
-
#### Defined in
|
|
117
|
-
|
|
118
|
-
[src/event.js:51](https://github.com/snowyu/events-ex.js/blob/a6cce2d/src/event.js#L51)
|
|
119
|
-
|
|
120
|
-
___
|
|
121
|
-
|
|
122
|
-
### init
|
|
123
|
-
|
|
124
|
-
▸ **init**(`target`, `type`): `void`
|
|
125
|
-
|
|
126
|
-
Initializes the event with the target object.
|
|
127
|
-
|
|
128
|
-
#### Parameters
|
|
129
|
-
|
|
130
|
-
| Name | Type | Description |
|
|
131
|
-
| :------ | :------ | :------ |
|
|
132
|
-
| `target` | [`EventEmitter`](event_emitter.EventEmitter.md) | The target object for the event. |
|
|
133
|
-
| `type` | `any` | - |
|
|
134
|
-
|
|
135
|
-
#### Returns
|
|
136
|
-
|
|
137
|
-
`void`
|
|
138
|
-
|
|
139
|
-
#### Defined in
|
|
140
|
-
|
|
141
|
-
[src/event.js:20](https://github.com/snowyu/events-ex.js/blob/a6cce2d/src/event.js#L20)
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
[events-ex](../README.md) / [Exports](../modules.md) / [event-emitter](../modules/event_emitter-1.md) / EventEmitter
|
|
2
|
-
|
|
3
|
-
# Class: EventEmitter
|
|
4
|
-
|
|
5
|
-
[event-emitter](../modules/event_emitter-1.md).EventEmitter
|
|
6
|
-
|
|
7
|
-
## Table of contents
|
|
8
|
-
|
|
9
|
-
### Constructors
|
|
10
|
-
|
|
11
|
-
- [constructor](event_emitter-1.EventEmitter.md#constructor)
|
|
12
|
-
|
|
13
|
-
## Constructors
|
|
14
|
-
|
|
15
|
-
### constructor
|
|
16
|
-
|
|
17
|
-
• **new EventEmitter**(): [`EventEmitter`](event_emitter-1.EventEmitter.md)
|
|
18
|
-
|
|
19
|
-
#### Returns
|
|
20
|
-
|
|
21
|
-
[`EventEmitter`](event_emitter-1.EventEmitter.md)
|
|
22
|
-
|
|
23
|
-
**`Classdesc`**
|
|
24
|
-
|
|
25
|
-
Class that represents an event emitter.
|
|
26
|
-
|
|
27
|
-
#### Defined in
|
|
28
|
-
|
|
29
|
-
[src/event-emitter.js:8](https://github.com/snowyu/events-ex.js/blob/a6cce2d/src/event-emitter.js#L8)
|