objectExtends the Node.js events.EventEmitter with extra capabilities.
15 |Boolean'filter' callback.
23 |function'else' callback.
26 |function'async-emit' callback.
29 |Boolean
42 | * [.subscriptionSeparator](#EnhancedEventEmitter.subscriptionSeparator) : String
43 | * [#on(event, listener)](#EnhancedEventEmitter+on) ⇒ function
44 | * [#on(options)](#EnhancedEventEmitter+on) ⇒ function
45 | * [#once(event, listener)](#EnhancedEventEmitter+once) ⇒ function
46 | * [#removeAllListeners([event])](#EnhancedEventEmitter+removeAllListeners)
47 | * [#suspend(event)](#EnhancedEventEmitter+suspend)
48 | * [#unsuspend(event)](#EnhancedEventEmitter+unsuspend)
49 | * [#else(listener)](#EnhancedEventEmitter+else)
50 | * [#removeElseListener(listener)](#EnhancedEventEmitter+removeElseListener)
51 | * [#unelse(listener)](#EnhancedEventEmitter+unelse)
52 | * [#removeAllElseListeners()](#EnhancedEventEmitter+removeAllElseListeners)
53 | * [#elseError(event)](#EnhancedEventEmitter+elseError)
54 | * [#removeElseError(event)](#EnhancedEventEmitter+removeElseError)
55 | * [#unelseError(event)](#EnhancedEventEmitter+unelseError)
56 | * [#emit(event, [params])](#EnhancedEventEmitter+emit) ⇒ Boolean
57 | * [#emitAsync(event, [params], [callback])](#EnhancedEventEmitter+emitAsync)
58 | * [#onAsync(event, listener)](#EnhancedEventEmitter+onAsync) ⇒ function
59 | * [#onAny(events, listener)](#EnhancedEventEmitter+onAny) ⇒ function
60 | * [#addFilter([event], filter)](#EnhancedEventEmitter+addFilter) ⇒ function
61 | * [#addEventFilter(event, filter)](#EnhancedEventEmitter+addEventFilter) ⇒ function
62 | * [#addGlobalFilter(filter)](#EnhancedEventEmitter+addGlobalFilter) ⇒ function
63 | * [#filter([event], filter)](#EnhancedEventEmitter+filter) ⇒ function
64 | * [#proxyEvents(emitters, events)](#EnhancedEventEmitter+proxyEvents) ⇒ function
65 | * [#addNoop(event)](#EnhancedEventEmitter+addNoop) ⇒ function
66 | * [#ignoreError()](#EnhancedEventEmitter+ignoreError)
67 |
68 |
69 |
70 | ### new EnhancedEventEmitter()
71 | This class holds all the extended capabilities added to any emitter.
72 |
73 |
74 |
75 | ### EnhancedEventEmitter.suspended : Boolean
76 | If true, all events will not trigger any listener (or 'else' listener).String
83 | If defined, events will be splitted by this separator and emitted as partials.function
90 | See node.js events.EventEmitter.on.function - The remove listener function
94 | **Access**: public
95 |
96 | | Param | Type | Description |
97 | | --- | --- | --- |
98 | | event | String | The name of the event |
99 | | listener | function | The callback function |
100 |
101 | **Example**
102 | ```js
103 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
104 | const emitter = new EnhancedEventEmitter();
105 |
106 | const remove = emitter.on('error', function (error) {
107 | console.error(error);
108 | });
109 |
110 | //remove listener (no longer need to keep a reference to the listener function)
111 | remove();
112 | ```
113 |
114 |
115 | ### EnhancedEventEmitter#on(options) ⇒ function
116 | Enables more complex on capabilities including providing multiple listeners/event names, timeout the listener and more.function - The remove listener function
120 | **Access**: public
121 |
122 | | Param | Type | Default | Description |
123 | | --- | --- | --- | --- |
124 | | options | Object | | All options needed to setup the listeners |
125 | | options.event | Array.<String> \| String | | The event name or an array of event names |
126 | | options.listener | Array.<function()> \| function | | The callback function or an array of callback functions |
127 | | [options.async] | Boolean | false | If true, the callback functions will be called after next tick |
128 | | [options.timeout] | Number | | If provided, the returned remove listener function will be called after the provided timeout value in millies (unless called manually before the timeout was triggered) |
129 |
130 | **Example**
131 | ```js
132 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
133 | const emitter = new EnhancedEventEmitter();
134 |
135 | const removeListener = emitter.on({
136 | event: ['error', 'connection-error', 'write-error', 'read-error'], //The event names (can be string for a single event)
137 | listener: [ //The listener callback functions (can be a function instead of an array for a single listener callback)
138 | function firstListener(arg1, arg2) {
139 | //do something
140 | },
141 | function secondListener(arg1, arg2) {
142 | //do something
143 | }
144 | ],
145 | async: true, //The callback functions will be called after next tick
146 | timeout: 1500 //All listeners will be removed after the provided timeout (if not provided, listeners can only be removed manually via returned function)
147 | });
148 |
149 | //emit any event
150 | emitter.emit('write-error', 1, 2, 3);
151 |
152 | //once done, remove all listeners from all events
153 | removeListener();
154 | ```
155 |
156 |
157 | ### EnhancedEventEmitter#once(event, listener) ⇒ function
158 | See node.js events.EventEmitter.once.function - The remove listener function
162 | **Access**: public
163 |
164 | | Param | Type | Description |
165 | | --- | --- | --- |
166 | | event | String | The name of the event |
167 | | listener | function | The callback function |
168 |
169 | **Example**
170 | ```js
171 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
172 | const emitter = new EnhancedEventEmitter();
173 |
174 | const remove = emitter.once('error', function (error) {
175 | console.error(error);
176 | });
177 |
178 | //remove listener (no longer need to keep a reference to the listener function)
179 | remove();
180 | ```
181 |
182 |
183 | ### EnhancedEventEmitter#removeAllListeners([event])
184 | See node.js events.EventEmitter.removeAllListeners.String \| Array | The name/s of the event |
192 |
193 | **Example**
194 | ```js
195 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
196 | const emitter = new EnhancedEventEmitter();
197 |
198 | //same as the basic removeAllListeners
199 | emitter.removeAllListeners('my-event');
200 |
201 | //also supports array of event names
202 | emitter.removeAllListeners(['my-event', 'another-event']);
203 | ```
204 |
205 |
206 | ### EnhancedEventEmitter#suspend(event)
207 | Suspends all emit calls for the provided event name (including 'else' listeners).String | The event to suspend |
215 |
216 | **Example**
217 | ```js
218 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
219 | const emitter = new EnhancedEventEmitter();
220 | emitter.on('test', function () {
221 | //will never be called
222 | });
223 |
224 | emitter.suspended = true; //suspend ALL events (to unsuspend use emitter.suspended = false;)
225 | //or
226 | emitter.suspend('test'); //suspend only 'test' event (to unsuspend use emitter.unsuspend('test');)
227 |
228 | emitter.emit('test');
229 | ```
230 |
231 |
232 | ### EnhancedEventEmitter#unsuspend(event)
233 | Unsuspends the emit calls for the provided event name.
234 |
235 | **Access**: public
236 |
237 | | Param | Type | Description |
238 | | --- | --- | --- |
239 | | event | String | The event to unsuspend |
240 |
241 |
242 |
243 | ### EnhancedEventEmitter#else(listener)
244 | Adds an 'else' listener which will be triggered by all events that do not have a listener currently for them (apart of the special 'error' event).
245 |
246 | **Access**: public
247 |
248 | | Param | Type | Description |
249 | | --- | --- | --- |
250 | | listener | [ElseCallback](#ElseCallback) | The listener that will catch all 'else' events |
251 |
252 | **Example**
253 | ```js
254 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
255 | const emitter = new EnhancedEventEmitter();
256 | emitter.else(function onUnhandledEvent(event, arg1, arg2) {
257 | //logic here....
258 |
259 | //to remove 'else' listeners, simply use the unelse function
260 | emitter.unelse(this);
261 | });
262 |
263 | emitter.emit('test', 1, 2);
264 | ```
265 |
266 |
267 | ### EnhancedEventEmitter#removeElseListener(listener)
268 | Removes the provided 'else' listener.ElseCallback](#ElseCallback) | The listener to remove |
276 |
277 |
278 |
279 | ### EnhancedEventEmitter#unelse(listener)
280 | See 'removeElseListener' documentation.
281 |
282 | **Access**: public
283 |
284 | | Param | Type | Description |
285 | | --- | --- | --- |
286 | | listener | [ElseCallback](#ElseCallback) | The listener to remove |
287 |
288 |
289 |
290 | ### EnhancedEventEmitter#removeAllElseListeners()
291 | Removes all 'else' listeners.
292 |
293 | **Access**: public
294 |
295 |
296 | ### EnhancedEventEmitter#elseError(event)
297 | In case an event with the provided name is emitted but no listener is attached to it, an error event will emitted by this emitter instance instead.
298 |
299 | **Access**: public
300 |
301 | | Param | Type | Description |
302 | | --- | --- | --- |
303 | | event | String | The event name |
304 |
305 | **Example**
306 | ```js
307 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
308 | const emitter = new EnhancedEventEmitter();
309 | emitter.on('error', function (error) {
310 | //logic here...
311 |
312 | //To remove elseError
313 | emitter.unelseError('test');
314 | });
315 |
316 | emitter.elseError('test');
317 |
318 | emitter.emit('test');
319 | ```
320 |
321 |
322 | ### EnhancedEventEmitter#removeElseError(event)
323 | Removes the else-error handler for the provided event.String | The event name |
331 |
332 |
333 |
334 | ### EnhancedEventEmitter#unelseError(event)
335 | See 'removeElseError' documentation.
336 |
337 | **Access**: public
338 |
339 | | Param | Type | Description |
340 | | --- | --- | --- |
341 | | event | String | The event name |
342 |
343 |
344 |
345 | ### EnhancedEventEmitter#emit(event, [params]) ⇒ Boolean
346 | See Node.js events.EventEmitter documentation.
347 |
348 | **Returns**: Boolean - True if a listener or an 'else' listener handled the event
349 | **Access**: public
350 |
351 | | Param | Type | Description |
352 | | --- | --- | --- |
353 | | event | String | The event name |
354 | | [params] | \* | The event parameters |
355 |
356 |
357 |
358 | ### EnhancedEventEmitter#emitAsync(event, [params], [callback])
359 | Invokes the emit after a timeout to enable calling flow to continue and not block due to event listeners.
360 |
361 | **Access**: public
362 |
363 | | Param | Type | Description |
364 | | --- | --- | --- |
365 | | event | String | The event name |
366 | | [params] | \* | The event parameters (if last param is a function, it is considered as the callback of the emitAsync) |
367 | | [callback] | [AsyncEmitCallback](#AsyncEmitCallback) | The async callback |
368 |
369 | **Example**
370 | ```js
371 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
372 | const emitter = new EnhancedEventEmitter();
373 | emitter.on('test', function onTestEvent(num1, num2) {
374 | //event logic here
375 | });
376 |
377 | emitter.emitAsync('test', 1, 2, function onEmitDone(event, num1, num2, emitted) {
378 | //emit callback logic
379 | });
380 | ```
381 |
382 |
383 | ### EnhancedEventEmitter#onAsync(event, listener) ⇒ function
384 | Adds a listener that will be triggered after a timeout during an emit.function - The remove listener function
389 | **Access**: public
390 |
391 | | Param | Type | Description |
392 | | --- | --- | --- |
393 | | event | String | The event name |
394 | | listener | function | The listener function |
395 |
396 | **Example**
397 | ```js
398 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
399 | const emitter = new EnhancedEventEmitter();
400 | emitter.on('test', function onEventSync() {
401 | //sync handle function logic
402 | });
403 | const removeListener = emitter.onAsync('test', function onEventAsync() {
404 | //async handle function logic
405 | });
406 |
407 | emitter.emit('test', 1, 2);
408 |
409 | //remove the async listener
410 | removeListener();
411 | ```
412 |
413 |
414 | ### EnhancedEventEmitter#onAny(events, listener) ⇒ function
415 | Adds a listener to all provided event names.function - The remove listener function
419 | **Access**: public
420 |
421 | | Param | Type | Description |
422 | | --- | --- | --- |
423 | | events | String \| Array | The event name/s |
424 | | listener | function | The listener function |
425 |
426 | **Example**
427 | ```js
428 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
429 | const emitter = new EnhancedEventEmitter();
430 |
431 | //add same listener to multiple events
432 | const remove = emitter.onAny(['test1', 'test2', 'test3'], function (arg1, arg2, arg3) {
433 | console.log(arg1, arg2, arg3);
434 | });
435 |
436 | //remove listener from all events
437 | remove();
438 | ```
439 |
440 |
441 | ### EnhancedEventEmitter#addFilter([event], filter) ⇒ function
442 | Adds a filter that will be triggered before every emit for the provided event type (if no event is provided, than the filter is invoked for all events).function - The remove filter function
446 | **Access**: public
447 |
448 | | Param | Type | Description |
449 | | --- | --- | --- |
450 | | [event] | String | The event name. If not provided, the filter is relevant for all events. |
451 | | filter | [FilterCallback](#FilterCallback) | The filter function |
452 |
453 | **Example**
454 | ```js
455 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
456 | const emitter = new EnhancedEventEmitter();
457 |
458 | //add filters for test event only
459 | const removeTestEventFilter = emitter.filter('test', function (event, arg1, arg2) {
460 | if (arg1 && (arg1 > 3)) {
461 | return true; //continue with emit
462 | }
463 |
464 | return false; //prevent emit
465 | });
466 | emitter.filter('test', function (event, arg1, arg2) {
467 | if (arg2 && (arg2 < 20)) {
468 | return true; //continue with emit
469 | }
470 |
471 | return false; //prevent emit
472 | });
473 |
474 | //add global filter for all events
475 | emitter.filter(function (event, arg1, arg2) {
476 | if (arg1 && (arg1 > 5)) {
477 | return true; //continue with emit
478 | }
479 |
480 | return false; //prevent emit
481 | });
482 | const removeGlobalArg2Filter = emitter.filter(function (event, arg1, arg2) {
483 | if (arg2 && (arg2 < 18)) {
484 | return true; //continue with emit
485 | }
486 |
487 | return false; //prevent emit
488 | });
489 |
490 | emitter.on('test', function onTestEvent(arg1, arg2) {
491 | //event logic here...
492 | });
493 |
494 | emitter.emit('test', 10, 15);
495 |
496 | //remove some filters
497 | removeTestEventFilter();
498 | removeGlobalArg2Filter();
499 | ```
500 |
501 |
502 | ### EnhancedEventEmitter#addEventFilter(event, filter) ⇒ function
503 | Adds an event filter (See addFilter)
504 |
505 | **Returns**: function - The remove filter function
506 | **Access**: public
507 |
508 | | Param | Type | Description |
509 | | --- | --- | --- |
510 | | event | String | The event name. |
511 | | filter | [FilterCallback](#FilterCallback) | The filter function |
512 |
513 |
514 |
515 | ### EnhancedEventEmitter#addGlobalFilter(filter) ⇒ function
516 | Adds a global filter (See addFilter)
517 |
518 | **Returns**: function - The remove filter function
519 | **Access**: public
520 |
521 | | Param | Type | Description |
522 | | --- | --- | --- |
523 | | filter | [FilterCallback](#FilterCallback) | The filter function |
524 |
525 |
526 |
527 | ### EnhancedEventEmitter#filter([event], filter) ⇒ function
528 | See 'addFilter' documentation.
529 |
530 | **Returns**: function - The remove filter function
531 | **Access**: public
532 |
533 | | Param | Type | Description |
534 | | --- | --- | --- |
535 | | [event] | String | The event name. If not provided, the filter is relevant for all events. |
536 | | filter | [FilterCallback](#FilterCallback) | The filter function |
537 |
538 |
539 |
540 | ### EnhancedEventEmitter#proxyEvents(emitters, events) ⇒ function
541 | Will setup an event proxy so if any of the requested event/s are fired from the provided emitter/s, they will be triggered by this emitter.
542 |
543 | **Returns**: function - Once invoked, will stop proxying of events
544 | **Access**: public
545 |
546 | | Param | Type | Description |
547 | | --- | --- | --- |
548 | | emitters | Array.<Object> \| Object | An event emitter or array of event emitters to proxy the events from |
549 | | events | Array.<String> \| String | A single event name or an array of event names to proxy from the provided emitter/s |
550 |
551 | **Example**
552 | ```js
553 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
554 | const emitter = new EnhancedEventEmitter();
555 |
556 | //proxy the 'data' and 'end' events from all sockets
557 | const stop = emitter.proxyEvents(sockets, ['data', 'end']);
558 |
559 | //listen to events via emitter
560 | emitter.on('data', onData);
561 |
562 | //stop events proxy
563 | stop();
564 | ```
565 |
566 |
567 | ### EnhancedEventEmitter#addNoop(event) ⇒ function
568 | Adds empty event handler.
569 |
570 | **Returns**: function - The remove listener function
571 | **Access**: public
572 |
573 | | Param | Type | Description |
574 | | --- | --- | --- |
575 | | event | String | The name of the event |
576 |
577 | **Example**
578 | ```js
579 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
580 | const emitter = new EnhancedEventEmitter();
581 |
582 | //add noop even handler for the 'error' event
583 | const remove = emitter.addNoop('error');
584 |
585 | //remove listener
586 | remove();
587 | ```
588 |
589 |
590 | ### EnhancedEventEmitter#ignoreError()
591 | Adds empty error event handler to prevent node.js from crashing in case of an error which we do not want/need to handle.EventEmitter
616 | * [#extend(EmitterType)](#EventEmitterEnhancer+extend) ⇒ Object
617 | * [#modify(EmitterType)](#EventEmitterEnhancer+modify)
618 | * [#modifyInstance(emitterInstance)](#EventEmitterEnhancer+modifyInstance)
619 |
620 |
621 |
622 | ### new EventEmitterEnhancer()
623 | This class enables to enhance event emitter prototypes and instances with extra capabilities.
624 |
625 |
626 |
627 | ### EventEmitterEnhancer.EnhancedEventEmitter : EventEmitter
628 | The node.js event emitter prototype extended with the extra capabilities.
629 |
630 | **Access**: public
631 |
632 |
633 | ### EventEmitterEnhancer#extend(EmitterType) ⇒ Object
634 | Extends the provided object prototype with the extended emitter capabilities.Object - The modified object type
638 | **Access**: public
639 |
640 | | Param | Type | Description |
641 | | --- | --- | --- |
642 | | EmitterType | Object | The object type |
643 |
644 | **Example**
645 | ```js
646 | //extend events.EventEmitter class (or any class that has the same interface)
647 | //now you can create instances of the new EnhancedEventEmitter type while events.EventEmitter is not modified/impacted in any way
648 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter); //extend the event emitter class (can be Node.js of some custom event emitter). original base class is not affected.
649 | const emitter = new EnhancedEventEmitter(); //create a new instance using the new extended class type.
650 | ```
651 |
652 |
653 | ### EventEmitterEnhancer#modify(EmitterType)
654 | Modified the provided object prototype with the extended emitter capabilities.Object | The object type |
662 |
663 | **Example**
664 | ```js
665 | //modify the proto of an events.EventEmitter class (or any class that has the same interface)
666 | //now all existing and future instances of the original class are modified to include the new extended capabilities.
667 | EventEmitterEnhancer.modify(EventEmitter); //modify the event emitter class prototype (can be Node.js of some custom event emitter). existing instances are impacted.
668 | const emitter = new EventEmitter(); //create an instance of the original class and automatically get the new extended capabilities.
669 | ```
670 |
671 |
672 | ### EventEmitterEnhancer#modifyInstance(emitterInstance)
673 | Modified the specific object instance with the extended emitter capabilities.Object | The emitter instance |
681 |
682 | **Example**
683 | ```js
684 | //modify specific instance to include the extended capabilities (other existing/future instances of that class type are not modified/impacted in any way).
685 | const emitter = new EventEmitter(); //create an instance of an event emitter (can be Node.js of some custom event emitter)
686 | EventEmitterEnhancer.modifyInstance(emitter); //modify the specific instance and add the extended capabilities. the original prototype is not affected.
687 | ```
688 |
689 |
690 | ## EventEmitterEnhancer : object
691 | Extends the Node.js events.EventEmitter with extra capabilities.
692 |
693 | **Kind**: global namespace
694 | **Author**: Sagie Gur-Ari
695 |
696 | * [EventEmitterEnhancer](#EventEmitterEnhancer) : object
697 | * [new EventEmitterEnhancer()](#new_EventEmitterEnhancer_new)
698 | * [.EnhancedEventEmitter](#EventEmitterEnhancer.EnhancedEventEmitter) : EventEmitter
699 | * [#extend(EmitterType)](#EventEmitterEnhancer+extend) ⇒ Object
700 | * [#modify(EmitterType)](#EventEmitterEnhancer+modify)
701 | * [#modifyInstance(emitterInstance)](#EventEmitterEnhancer+modifyInstance)
702 |
703 |
704 |
705 | ### new EventEmitterEnhancer()
706 | This class enables to enhance event emitter prototypes and instances with extra capabilities.
707 |
708 |
709 |
710 | ### EventEmitterEnhancer.EnhancedEventEmitter : EventEmitter
711 | The node.js event emitter prototype extended with the extra capabilities.
712 |
713 | **Access**: public
714 |
715 |
716 | ### EventEmitterEnhancer#extend(EmitterType) ⇒ Object
717 | Extends the provided object prototype with the extended emitter capabilities.Object - The modified object type
721 | **Access**: public
722 |
723 | | Param | Type | Description |
724 | | --- | --- | --- |
725 | | EmitterType | Object | The object type |
726 |
727 | **Example**
728 | ```js
729 | //extend events.EventEmitter class (or any class that has the same interface)
730 | //now you can create instances of the new EnhancedEventEmitter type while events.EventEmitter is not modified/impacted in any way
731 | const EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter); //extend the event emitter class (can be Node.js of some custom event emitter). original base class is not affected.
732 | const emitter = new EnhancedEventEmitter(); //create a new instance using the new extended class type.
733 | ```
734 |
735 |
736 | ### EventEmitterEnhancer#modify(EmitterType)
737 | Modified the provided object prototype with the extended emitter capabilities.Object | The object type |
745 |
746 | **Example**
747 | ```js
748 | //modify the proto of an events.EventEmitter class (or any class that has the same interface)
749 | //now all existing and future instances of the original class are modified to include the new extended capabilities.
750 | EventEmitterEnhancer.modify(EventEmitter); //modify the event emitter class prototype (can be Node.js of some custom event emitter). existing instances are impacted.
751 | const emitter = new EventEmitter(); //create an instance of the original class and automatically get the new extended capabilities.
752 | ```
753 |
754 |
755 | ### EventEmitterEnhancer#modifyInstance(emitterInstance)
756 | Modified the specific object instance with the extended emitter capabilities.Object | The emitter instance |
764 |
765 | **Example**
766 | ```js
767 | //modify specific instance to include the extended capabilities (other existing/future instances of that class type are not modified/impacted in any way).
768 | const emitter = new EventEmitter(); //create an instance of an event emitter (can be Node.js of some custom event emitter)
769 | EventEmitterEnhancer.modifyInstance(emitter); //modify the specific instance and add the extended capabilities. the original prototype is not affected.
770 | ```
771 |
772 |
773 | ## FilterCallback ⇒ Boolean
774 | 'filter' callback.
775 |
776 | **Kind**: global typedef
777 | **Returns**: Boolean - True to continue with the emit, false to prevent emit
778 |
779 | | Param | Type | Description |
780 | | --- | --- | --- |
781 | | type | String | The event type |
782 | | [params] | \* | The event parameters |
783 |
784 |
785 |
786 | ## ElseCallback : function
787 | 'else' callback.
788 |
789 | **Kind**: global typedef
790 |
791 | | Param | Type | Description |
792 | | --- | --- | --- |
793 | | type | String | The event type |
794 | | [params] | \* | The event parameters |
795 |
796 |
797 |
798 | ## AsyncEmitCallback : function
799 | 'async-emit' callback.
800 |
801 | **Kind**: global typedef
802 |
803 | | Param | Type | Description |
804 | | --- | --- | --- |
805 | | type | String | The event type |
806 | | [params] | \* | The event parameters |
807 | | emitted | Boolean | True if emitted, else false |
808 |
809 |
--------------------------------------------------------------------------------
/lib/enhanced-event-emitter.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const funcs = require('funcs-js');
4 | const later = require('node-later');
5 | const defer = later();
6 |
7 | /**
8 | * Extends the Node.js events.EventEmitter with extra capabilities.
9 | *
10 | * @author Sagie Gur-Ari
11 | * @namespace EventEmitterEnhancer
12 | */
13 |
14 | /**
15 | * 'filter' callback.
16 | *
17 | * @callback FilterCallback
18 | * @param {String} type - The event type
19 | * @param {*} [params] - The event parameters
20 | * @returns {Boolean} True to continue with the emit, false to prevent emit
21 | */
22 |
23 | /**
24 | * 'else' callback.
25 | *
26 | * @callback ElseCallback
27 | * @param {String} type - The event type
28 | * @param {*} [params] - The event parameters
29 | */
30 |
31 | /**
32 | * 'async-emit' callback.
33 | *
34 | * @callback AsyncEmitCallback
35 | * @param {String} type - The event type
36 | * @param {*} [params] - The event parameters
37 | * @param {Boolean} emitted - True if emitted, else false
38 | */
39 |
40 | /*jslint debug: true */
41 | /*istanbul ignore next*/
42 | /**
43 | * This class holds all the extended capabilities added to any emitter.
44 | *
45 | * @author Sagie Gur-Ari
46 | * @class EnhancedEventEmitter
47 | * @public
48 | */
49 | function EnhancedEventEmitter() {
50 | //should not be called
51 | }
52 | /*jslint debug: false */
53 |
54 | /**
55 | * Marker attribute to prevent multiple wrapping of emitter.
56 | *
57 | * @member {Boolean}
58 | * @alias EnhancedEventEmitter.enhancedEmitterType
59 | * @memberof! EnhancedEventEmitter
60 | * @private
61 | */
62 | EnhancedEventEmitter.prototype.enhancedEmitterType = true;
63 |
64 | /**
65 | * If true, all events will not trigger any listener (or 'else' listener).