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/lib/default-methods.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
exports.getEventableMethods = getEventableMethods;
|
|
8
8
|
var _utilEx = require("util-ex");
|
|
9
|
+
require("./util/promise-any");
|
|
9
10
|
var _consts = require("./consts");
|
|
10
11
|
var _event = require("./event");
|
|
11
12
|
const create = Object.create;
|
|
@@ -17,6 +18,52 @@ function isRegExp(value) {
|
|
|
17
18
|
}
|
|
18
19
|
function getEventableMethods(aClass) {
|
|
19
20
|
return {
|
|
21
|
+
/**
|
|
22
|
+
* Configures the event emitter with specified options using a Fluent API.
|
|
23
|
+
* @param {Object} options - Configuration options for event emission.
|
|
24
|
+
* @param {string} [options.asyncMode='serial'] - The mode of asynchronous emission ('serial' or 'parallel').
|
|
25
|
+
* @param {string} [options.resultMode='last'] - The strategy for handling multiple return values ('last', 'first', 'collect').
|
|
26
|
+
* @returns {import('./event-emitter').EventEmitter} A proxy object representing the configured EventEmitter.
|
|
27
|
+
*/
|
|
28
|
+
configure(options) {
|
|
29
|
+
const proxy = Object.create(this);
|
|
30
|
+
const mergedOptions = Object.assign({}, this._eeRuntimeOptions, options);
|
|
31
|
+
(0, _utilEx.defineProperty)(proxy, '_eeRuntimeOptions', mergedOptions);
|
|
32
|
+
return proxy;
|
|
33
|
+
},
|
|
34
|
+
/**
|
|
35
|
+
* A shortcut for parallel configuration.
|
|
36
|
+
* @param {string} [resultMode='last'] - The strategy for handling multiple return values.
|
|
37
|
+
* @returns {import('./event-emitter').EventEmitter} A proxy object representing the configured EventEmitter.
|
|
38
|
+
*/
|
|
39
|
+
parallel(resultMode) {
|
|
40
|
+
return this.configure({
|
|
41
|
+
asyncMode: 'parallel',
|
|
42
|
+
resultMode: resultMode || 'last'
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
/**
|
|
46
|
+
* Sets the configuration options for the EventEmitter instance.
|
|
47
|
+
* @param {Object} options - Configuration options for the emitter (e.g., asyncMode, resultMode, maxListeners).
|
|
48
|
+
* @returns {import('./event-emitter').EventEmitter} The EventEmitter instance for chaining.
|
|
49
|
+
*/
|
|
50
|
+
setEmitterOptions(options) {
|
|
51
|
+
if (!(0, _utilEx.isObject)(options)) {
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
let data;
|
|
55
|
+
if (!this.hasOwnProperty('_emitterOptions')) {
|
|
56
|
+
data = create(null);
|
|
57
|
+
(0, _utilEx.defineProperty)(this, '_emitterOptions', data);
|
|
58
|
+
} else {
|
|
59
|
+
data = this._emitterOptions;
|
|
60
|
+
}
|
|
61
|
+
Object.assign(data, options);
|
|
62
|
+
if (!(0, _utilEx.isUndefined)(options.maxListeners) && (0, _utilEx.isFunction)(this.setMaxListeners)) {
|
|
63
|
+
this.setMaxListeners(options.maxListeners);
|
|
64
|
+
}
|
|
65
|
+
return this;
|
|
66
|
+
},
|
|
20
67
|
/**
|
|
21
68
|
* Adds a listener function to the specified event type.
|
|
22
69
|
* @param {string|RegExp} type - The event type to listen for.
|
|
@@ -100,7 +147,7 @@ function getEventableMethods(aClass) {
|
|
|
100
147
|
* @param {...*} args - The event type followed by any number of arguments to be passed to the listener functions.
|
|
101
148
|
* @returns {*} The result of the event.
|
|
102
149
|
*/
|
|
103
|
-
emit(
|
|
150
|
+
emit(/* type, msg , ... */
|
|
104
151
|
) {
|
|
105
152
|
const r = _emit.apply(this, arguments);
|
|
106
153
|
if (!r) {
|
|
@@ -143,7 +190,7 @@ function getEventableMethods(aClass) {
|
|
|
143
190
|
* @param {...*} args - The event type followed by any number of arguments to be passed to the listener functions.
|
|
144
191
|
* @returns {Promise<*>} A promise that resolves with the result of the event.
|
|
145
192
|
*/
|
|
146
|
-
async emitAsync(
|
|
193
|
+
async emitAsync(/* type, msg , ... */
|
|
147
194
|
) {
|
|
148
195
|
const r = _emit.apply(this, arguments);
|
|
149
196
|
if (!r) {
|
|
@@ -152,27 +199,9 @@ function getEventableMethods(aClass) {
|
|
|
152
199
|
const args = r.args;
|
|
153
200
|
const listeners = r.listeners;
|
|
154
201
|
const evt = (0, _event.Event)(this, r.type);
|
|
155
|
-
const
|
|
202
|
+
const options = Object.assign({}, this._emitterOptions, this._eeRuntimeOptions);
|
|
156
203
|
try {
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
await _notify(listener, evt, args);
|
|
160
|
-
if (evt.stopped) {
|
|
161
|
-
break;
|
|
162
|
-
}
|
|
163
|
-
} catch (err) {
|
|
164
|
-
errs.push({
|
|
165
|
-
err: err,
|
|
166
|
-
listener: listener
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (errs.length) {
|
|
171
|
-
for (let i = 0; i < errs.length; i++) {
|
|
172
|
-
const it = errs[i];
|
|
173
|
-
this.emit('error', it.err, 'notify', r.type, it.listener, args);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
204
|
+
await _executeAsync.call(this, listeners, evt, args, options);
|
|
176
205
|
} finally {
|
|
177
206
|
// eslint-disable-next-line no-unsafe-finally
|
|
178
207
|
return evt.end();
|
|
@@ -420,4 +449,67 @@ function _notify(listener, evt, args) {
|
|
|
420
449
|
}
|
|
421
450
|
}
|
|
422
451
|
return result;
|
|
452
|
+
}
|
|
453
|
+
async function _executeAsync(listeners, evt, args, options) {
|
|
454
|
+
const asyncMode = options.asyncMode || 'serial';
|
|
455
|
+
const resultMode = options.resultMode || 'last';
|
|
456
|
+
const errs = [];
|
|
457
|
+
if (resultMode === 'collect') {
|
|
458
|
+
evt.result = [];
|
|
459
|
+
}
|
|
460
|
+
const notifyListener = async listener => {
|
|
461
|
+
try {
|
|
462
|
+
const result = await _notify(listener, evt, args);
|
|
463
|
+
if (result !== undefined) {
|
|
464
|
+
if (resultMode === 'first' && !evt.resolved) {
|
|
465
|
+
evt.result = result;
|
|
466
|
+
evt.resolved = true;
|
|
467
|
+
} else if (resultMode === 'last') {
|
|
468
|
+
evt.result = result;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return result;
|
|
472
|
+
} catch (err) {
|
|
473
|
+
errs.push({
|
|
474
|
+
err,
|
|
475
|
+
listener
|
|
476
|
+
});
|
|
477
|
+
throw err;
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
if (asyncMode === 'parallel') {
|
|
481
|
+
const promises = listeners.map(listener => notifyListener(listener));
|
|
482
|
+
if (resultMode === 'collect') {
|
|
483
|
+
evt.result = await Promise.all(promises.map(p => p.catch(() => undefined)));
|
|
484
|
+
} else if (resultMode === 'first') {
|
|
485
|
+
try {
|
|
486
|
+
await Promise.any(promises.map(p => p.then(res => res === undefined ? Promise.reject() : res)));
|
|
487
|
+
} catch (e) {
|
|
488
|
+
// If all rejected or returned undefined, ignore
|
|
489
|
+
}
|
|
490
|
+
} else {
|
|
491
|
+
await Promise.all(promises.map(p => p.catch(() => undefined)));
|
|
492
|
+
}
|
|
493
|
+
} else {
|
|
494
|
+
// Serial mode (default)
|
|
495
|
+
for (const listener of listeners) {
|
|
496
|
+
try {
|
|
497
|
+
const result = await notifyListener(listener);
|
|
498
|
+
if (resultMode === 'collect') {
|
|
499
|
+
evt.result.push(result);
|
|
500
|
+
}
|
|
501
|
+
if (evt.stopped || resultMode === 'first' && evt.resolved) break;
|
|
502
|
+
} catch (err) {
|
|
503
|
+
if (resultMode === 'collect') {
|
|
504
|
+
evt.result.push(undefined);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
if (errs.length) {
|
|
510
|
+
for (let i = 0; i < errs.length; i++) {
|
|
511
|
+
const it = errs[i];
|
|
512
|
+
this.emit('error', it.err, 'notify', evt.type, it.listener, args);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
423
515
|
}
|
package/lib/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/lib/event.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @param {import('./event-emitter').EventEmitter} target - Who trigger the event
|
|
6
6
|
* @returns {Event} - The new Event instance.
|
|
7
7
|
*/
|
|
8
|
-
export function Event(target: import(
|
|
8
|
+
export function Event(target: import("./event-emitter").EventEmitter, type: any): Event;
|
|
9
9
|
export class Event {
|
|
10
10
|
/**
|
|
11
11
|
* Creates a new Event object instance that contains information about the event, such as the target element and the return value of the event.
|
|
@@ -14,12 +14,12 @@ export class Event {
|
|
|
14
14
|
* @param {import('./event-emitter').EventEmitter} target - Who trigger the event
|
|
15
15
|
* @returns {Event} - The new Event instance.
|
|
16
16
|
*/
|
|
17
|
-
constructor(target: import(
|
|
17
|
+
constructor(target: import("./event-emitter").EventEmitter, type: any);
|
|
18
18
|
/**
|
|
19
19
|
* Initializes the event with the target object.
|
|
20
20
|
* @param {import('./event-emitter').EventEmitter} target - The target object for the event.
|
|
21
21
|
*/
|
|
22
|
-
init(target: import(
|
|
22
|
+
init(target: import("./event-emitter").EventEmitter, type: any): void;
|
|
23
23
|
/**
|
|
24
24
|
* Who trigger the event
|
|
25
25
|
* @type {Object}
|
|
@@ -32,6 +32,12 @@ export class Event {
|
|
|
32
32
|
* @public
|
|
33
33
|
*/
|
|
34
34
|
public stopped: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Whether a result has been resolved (for 'first' result mode)
|
|
37
|
+
* @type {boolean}
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
public resolved: boolean;
|
|
35
41
|
/**
|
|
36
42
|
* Keep your event result here if any.
|
|
37
43
|
* @type {*}
|
package/lib/event.js
CHANGED
|
@@ -37,6 +37,12 @@ Event.prototype.init = function (target, type) {
|
|
|
37
37
|
* @public
|
|
38
38
|
*/
|
|
39
39
|
this.stopped = false;
|
|
40
|
+
/**
|
|
41
|
+
* Whether a result has been resolved (for 'first' result mode)
|
|
42
|
+
* @type {boolean}
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
this.resolved = false;
|
|
40
46
|
/**
|
|
41
47
|
* Keep your event result here if any.
|
|
42
48
|
* @type {*}
|
package/lib/eventable.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.eventable = exports.default = void 0;
|
|
|
7
7
|
var _customAbility = require("custom-ability");
|
|
8
8
|
var _defineProperty = _interopRequireDefault(require("util-ex/lib/defineProperty"));
|
|
9
9
|
var _defaultMethods = _interopRequireDefault(require("./default-methods"));
|
|
10
|
-
function _interopRequireDefault(
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
/**
|
|
12
12
|
* Creates an Eventable class with event-emitting capabilities.
|
|
13
13
|
*
|
|
@@ -31,6 +31,9 @@ function getEventableClass(aClass) {
|
|
|
31
31
|
Eventable.prototype.listenerCount = methods.listenerCount;
|
|
32
32
|
Eventable.prototype.emit = methods.emit;
|
|
33
33
|
Eventable.prototype.emitAsync = methods.emitAsync;
|
|
34
|
+
Eventable.prototype.configure = methods.configure;
|
|
35
|
+
Eventable.prototype.parallel = methods.parallel;
|
|
36
|
+
Eventable.prototype.setEmitterOptions = methods.setEmitterOptions;
|
|
34
37
|
Eventable.prototype.on = methods.on;
|
|
35
38
|
Eventable.prototype.addListener = methods.on;
|
|
36
39
|
Eventable.prototype.off = methods.off;
|
package/lib/pipe-async.d.ts
CHANGED
|
@@ -4,8 +4,11 @@
|
|
|
4
4
|
* @param {import('./event-emitter').EventEmitter} e1 - The first event emitter.
|
|
5
5
|
* @param {import('./event-emitter').EventEmitter} e2 - The second event emitter.
|
|
6
6
|
* @param {string} [name='emitAsync'] - The name of the event to pipe (defaults to 'emitAsync').
|
|
7
|
+
* @param {Object} [options] - Configuration for the pipeline.
|
|
8
|
+
* @param {string} [options.asyncMode='serial'] - The mode of propagation ('serial' or 'parallel').
|
|
9
|
+
* @param {string} [options.resultMode] - Strategy for aggregating results from the pipe chain ('collect', 'first').
|
|
7
10
|
* @returns {Object} - An object with a `close` method that removes the pipeline between the two event emitters.
|
|
8
11
|
* @throws {TypeError} - If either of the arguments is not an event emitter object.
|
|
9
12
|
*/
|
|
10
|
-
export function pipeAsync(e1: import(
|
|
13
|
+
export function pipeAsync(e1: import("./event-emitter").EventEmitter, e2: import("./event-emitter").EventEmitter, ...args: any[]): any;
|
|
11
14
|
export default pipeAsync;
|
package/lib/pipe-async.js
CHANGED
|
@@ -9,7 +9,7 @@ var _defineProperty = _interopRequireDefault(require("util-ex/lib/defineProperty
|
|
|
9
9
|
var _wrapEventEmitter = require("./wrap-event-emitter");
|
|
10
10
|
var _arrayRemove = _interopRequireDefault(require("./util/array-remove"));
|
|
11
11
|
var _validObject = _interopRequireDefault(require("./util/valid-object"));
|
|
12
|
-
function _interopRequireDefault(
|
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
14
14
|
const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
|
|
15
15
|
const arrFrom = Array.from;
|
|
@@ -21,16 +21,27 @@ const emit = _wrapEventEmitter.methods.emitAsync;
|
|
|
21
21
|
* @param {import('./event-emitter').EventEmitter} e1 - The first event emitter.
|
|
22
22
|
* @param {import('./event-emitter').EventEmitter} e2 - The second event emitter.
|
|
23
23
|
* @param {string} [name='emitAsync'] - The name of the event to pipe (defaults to 'emitAsync').
|
|
24
|
+
* @param {Object} [options] - Configuration for the pipeline.
|
|
25
|
+
* @param {string} [options.asyncMode='serial'] - The mode of propagation ('serial' or 'parallel').
|
|
26
|
+
* @param {string} [options.resultMode] - Strategy for aggregating results from the pipe chain ('collect', 'first').
|
|
24
27
|
* @returns {Object} - An object with a `close` method that removes the pipeline between the two event emitters.
|
|
25
28
|
* @throws {TypeError} - If either of the arguments is not an event emitter object.
|
|
26
29
|
*/
|
|
27
|
-
function pipeAsync(e1, e2 /* , name */) {
|
|
30
|
+
function pipeAsync(e1, e2 /* , name, options */) {
|
|
28
31
|
let pipes;
|
|
29
32
|
(0, _validObject.default)(e1) && (0, _validObject.default)(e2);
|
|
30
33
|
let name = arguments[2];
|
|
34
|
+
let options = arguments[3];
|
|
35
|
+
if (typeof name === 'object') {
|
|
36
|
+
options = name;
|
|
37
|
+
name = undefined;
|
|
38
|
+
}
|
|
31
39
|
if (name === undefined) {
|
|
32
40
|
name = 'emitAsync';
|
|
33
41
|
}
|
|
42
|
+
if (!options) {
|
|
43
|
+
options = {};
|
|
44
|
+
}
|
|
34
45
|
const result = {
|
|
35
46
|
close() {
|
|
36
47
|
_arrayRemove.default.call(pipes, e2);
|
|
@@ -50,9 +61,32 @@ function pipeAsync(e1, e2 /* , name */) {
|
|
|
50
61
|
}
|
|
51
62
|
desc.value = async function () {
|
|
52
63
|
const data = arrFrom(pipes);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
64
|
+
const asyncMode = options.asyncMode || 'serial';
|
|
65
|
+
const resultMode = options.resultMode;
|
|
66
|
+
const forward = async (target, args) => {
|
|
67
|
+
const fn = target[name] || target.emitAsync || emit;
|
|
68
|
+
return fn.apply(target, args);
|
|
69
|
+
};
|
|
70
|
+
if (asyncMode === 'parallel') {
|
|
71
|
+
const promises = [emit.apply(this, arguments)];
|
|
72
|
+
for (let i = 0; i < data.length; ++i) {
|
|
73
|
+
promises.push(forward(data[i], arguments));
|
|
74
|
+
}
|
|
75
|
+
const results = await Promise.all(promises);
|
|
76
|
+
if (resultMode === 'collect') return results;
|
|
77
|
+
if (resultMode === 'first') return results.find(r => r !== undefined);
|
|
78
|
+
return results[0]; // Default: return main emitter's result
|
|
79
|
+
} else {
|
|
80
|
+
const mainResult = await emit.apply(this, arguments);
|
|
81
|
+
const allResults = [mainResult];
|
|
82
|
+
for (let i = 0; i < data.length; ++i) {
|
|
83
|
+
const res = await forward(data[i], arguments);
|
|
84
|
+
allResults.push(res);
|
|
85
|
+
}
|
|
86
|
+
if (resultMode === 'collect') return allResults;
|
|
87
|
+
if (resultMode === 'first') return allResults.find(r => r !== undefined);
|
|
88
|
+
return mainResult; // Default: return main emitter's result
|
|
89
|
+
}
|
|
56
90
|
};
|
|
57
91
|
(0, _defineProperty.default)(e1, name, desc.value, desc);
|
|
58
92
|
return result;
|
package/lib/pipe.d.ts
CHANGED
|
@@ -7,5 +7,5 @@
|
|
|
7
7
|
* @returns {Object} - An object with a `close` method that removes the pipeline between the two event emitters.
|
|
8
8
|
* @throws {TypeError} - If either of the arguments is not an event emitter object.
|
|
9
9
|
*/
|
|
10
|
-
export function pipe(e1: import(
|
|
10
|
+
export function pipe(e1: import("./event-emitter").EventEmitter, e2: import("./event-emitter").EventEmitter, ...args: any[]): any;
|
|
11
11
|
export default pipe;
|
package/lib/pipe.js
CHANGED
|
@@ -9,7 +9,7 @@ var _defineProperty = _interopRequireDefault(require("util-ex/lib/defineProperty
|
|
|
9
9
|
var _wrapEventEmitter = require("./wrap-event-emitter");
|
|
10
10
|
var _arrayRemove = _interopRequireDefault(require("./util/array-remove"));
|
|
11
11
|
var _validObject = _interopRequireDefault(require("./util/valid-object"));
|
|
12
|
-
function _interopRequireDefault(
|
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
14
14
|
const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
|
|
15
15
|
const arrFrom = Array.from;
|
package/lib/unify.d.ts
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* @param {import('./event-emitter').EventEmitter} e2 - The second event emitter object.
|
|
6
6
|
* @throws {TypeError} - If either of the arguments is not an event emitter object.
|
|
7
7
|
*/
|
|
8
|
-
export function unify(e1: import(
|
|
8
|
+
export function unify(e1: import("./event-emitter").EventEmitter, e2: import("./event-emitter").EventEmitter): void;
|
|
9
9
|
export default unify;
|
package/lib/unify.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.default = void 0;
|
|
|
7
7
|
exports.unify = unify;
|
|
8
8
|
var _validObject = _interopRequireDefault(require("./util/valid-object"));
|
|
9
9
|
var _objectForEach = _interopRequireDefault(require("./util/object-for-each"));
|
|
10
|
-
function _interopRequireDefault(
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
const push = Array.prototype.apply;
|
|
12
12
|
const defineProperty = Object.defineProperty;
|
|
13
13
|
const create = Object.create;
|
package/lib/util/array-remove.js
CHANGED
|
@@ -8,7 +8,7 @@ exports.remove = remove;
|
|
|
8
8
|
const indexOf = Array.prototype.indexOf;
|
|
9
9
|
const forEach = Array.prototype.forEach;
|
|
10
10
|
const splice = Array.prototype.splice;
|
|
11
|
-
function remove(
|
|
11
|
+
function remove(/* ...itemsToRemove */
|
|
12
12
|
) {
|
|
13
13
|
forEach.call(arguments, function (item) {
|
|
14
14
|
const index = indexOf.call(this, item);
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
exports.forEach = forEach;
|
|
8
8
|
var _validObject = _interopRequireDefault(require("./valid-object"));
|
|
9
|
-
function _interopRequireDefault(
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
10
|
const bind = Function.prototype.bind;
|
|
11
11
|
const keys = Object.keys;
|
|
12
12
|
function forEach(obj, cb /* , thisArg, compareFn */) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Simple polyfill for AggregateError if it doesn't exist.
|
|
9
|
+
*/
|
|
10
|
+
const _AggregateError = typeof AggregateError !== 'undefined' ? AggregateError : function AggregateError(errors, message) {
|
|
11
|
+
const error = new Error(message);
|
|
12
|
+
error.name = 'AggregateError';
|
|
13
|
+
error.errors = errors;
|
|
14
|
+
return error;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Ensures Promise.any exists, or provides a polyfill.
|
|
19
|
+
*/
|
|
20
|
+
if (typeof Promise.any !== 'function') {
|
|
21
|
+
Promise.any = function (promises) {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
promises = Array.from(promises);
|
|
24
|
+
const len = promises.length;
|
|
25
|
+
let errors = [];
|
|
26
|
+
let rejectedCount = 0;
|
|
27
|
+
if (len === 0) {
|
|
28
|
+
return reject(new _AggregateError(errors, 'All promises were rejected'));
|
|
29
|
+
}
|
|
30
|
+
promises.forEach((promise, index) => {
|
|
31
|
+
Promise.resolve(promise).then(value => {
|
|
32
|
+
resolve(value);
|
|
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
|
+
var _default = exports.default = Promise.any;
|
package/lib/util/string-pad.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.default = void 0;
|
|
|
7
7
|
exports.pad = pad;
|
|
8
8
|
var _toInt = _interopRequireDefault(require("./to-int"));
|
|
9
9
|
var _validObject = _interopRequireDefault(require("./valid-object"));
|
|
10
|
-
function _interopRequireDefault(
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
const repeat = String.prototype.repeat;
|
|
12
12
|
const abs = Math.abs;
|
|
13
13
|
const max = Math.max;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Create or inject the eventable instance into the object
|
|
3
3
|
* @param {Object} [o] the optional instance to eventable
|
|
4
|
+
* @param {Object} [options] optional configuration for the emitter
|
|
4
5
|
* @returns o or new Event instance
|
|
5
6
|
*/
|
|
6
|
-
export function wrapEventEmitter(o?: any): any;
|
|
7
|
+
export function wrapEventEmitter(o?: any, options?: any): any;
|
|
7
8
|
export const methods: any;
|
|
8
9
|
export default wrapEventEmitter;
|
|
@@ -7,10 +7,14 @@ exports.methods = exports.default = void 0;
|
|
|
7
7
|
exports.wrapEventEmitter = wrapEventEmitter;
|
|
8
8
|
var _utilEx = require("util-ex");
|
|
9
9
|
var _eventable = _interopRequireDefault(require("./eventable"));
|
|
10
|
-
function _interopRequireDefault(
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
const create = Object.create;
|
|
12
12
|
const defineProperties = Object.defineProperties;
|
|
13
13
|
const methods = exports.methods = (0, _eventable.default)().methods;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Minimal set of core methods to be injected into an existing object.
|
|
17
|
+
*/
|
|
14
18
|
const descriptors = {
|
|
15
19
|
on: {
|
|
16
20
|
value: methods.on
|
|
@@ -26,18 +30,35 @@ const descriptors = {
|
|
|
26
30
|
},
|
|
27
31
|
emitAsync: {
|
|
28
32
|
value: methods.emitAsync
|
|
33
|
+
},
|
|
34
|
+
setEmitterOptions: {
|
|
35
|
+
value: methods.setEmitterOptions
|
|
29
36
|
}
|
|
30
37
|
};
|
|
31
|
-
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Full set of all available methods from eventable for standalone instances.
|
|
41
|
+
*/
|
|
42
|
+
const fullDescriptors = {};
|
|
43
|
+
Object.keys(methods).forEach(key => {
|
|
44
|
+
fullDescriptors[key] = {
|
|
45
|
+
value: methods[key]
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
const base = defineProperties({}, fullDescriptors);
|
|
32
49
|
|
|
33
50
|
/**
|
|
34
51
|
* Create or inject the eventable instance into the object
|
|
35
52
|
* @param {Object} [o] the optional instance to eventable
|
|
53
|
+
* @param {Object} [options] optional configuration for the emitter
|
|
36
54
|
* @returns o or new Event instance
|
|
37
55
|
*/
|
|
38
|
-
function wrapEventEmitter(o) {
|
|
56
|
+
function wrapEventEmitter(o, options) {
|
|
39
57
|
const result = o == null ? create(base) : defineProperties(Object(o), descriptors);
|
|
40
58
|
(0, _utilEx.defineProperty)(result, '_events', {});
|
|
59
|
+
if (options && options.emitterOptions) {
|
|
60
|
+
result.setEmitterOptions(options.emitterOptions);
|
|
61
|
+
}
|
|
41
62
|
return result;
|
|
42
63
|
}
|
|
43
64
|
;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "events-ex",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Browser-friendly enhanced events most compatible with standard node.js, it's powerful eventable ability.",
|
|
5
5
|
"contributors": [
|
|
6
6
|
{
|
|
@@ -37,25 +37,25 @@
|
|
|
37
37
|
"main": "./lib/index.js",
|
|
38
38
|
"module": "./src/index.js",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"custom-ability": "^2.0
|
|
41
|
-
"util-ex": "^2.
|
|
40
|
+
"custom-ability": "^2.1.0",
|
|
41
|
+
"util-ex": "^2.5.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@antfu/eslint-config": "^
|
|
45
|
-
"@babel/cli": "^7.
|
|
46
|
-
"@babel/core": "^7.
|
|
44
|
+
"@antfu/eslint-config": "^7.7.0",
|
|
45
|
+
"@babel/cli": "^7.28.6",
|
|
46
|
+
"@babel/core": "^7.29.0",
|
|
47
47
|
"@babel/plugin-proposal-dynamic-import": "^7.18.6",
|
|
48
|
-
"@babel/plugin-transform-modules-commonjs": "^7.
|
|
49
|
-
"@babel/register": "^7.
|
|
50
|
-
"chai": "~4.3.
|
|
51
|
-
"eslint": "^
|
|
52
|
-
"eslint-config-prettier": "^
|
|
53
|
-
"eslint-plugin-tsdoc": "^0.2
|
|
54
|
-
"mocha": "^
|
|
55
|
-
"prettier": "^3.
|
|
56
|
-
"typedoc": "^0.
|
|
57
|
-
"typedoc-plugin-markdown": "^
|
|
58
|
-
"typescript": "
|
|
48
|
+
"@babel/plugin-transform-modules-commonjs": "^7.28.6",
|
|
49
|
+
"@babel/register": "^7.28.6",
|
|
50
|
+
"chai": "~4.3.10",
|
|
51
|
+
"eslint": "^10.0.3",
|
|
52
|
+
"eslint-config-prettier": "^10.1.8",
|
|
53
|
+
"eslint-plugin-tsdoc": "^0.5.2",
|
|
54
|
+
"mocha": "^11.7.5",
|
|
55
|
+
"prettier": "^3.8.1",
|
|
56
|
+
"typedoc": "^0.28.17",
|
|
57
|
+
"typedoc-plugin-markdown": "^4.10.0",
|
|
58
|
+
"typescript": "~5.7.3"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "npm run build.cjs && npm run build.ts && npm run doc.md",
|