Skip to content

Commit

Permalink
removeAllListeners to accept array #1
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari committed Oct 30, 2017
1 parent 7131b0f commit e6961f4
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 25 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Expand Up @@ -22,6 +22,7 @@ module.exports = function (grunt) {
occurrence: 2
},
'usage-once': 'EnhancedEventEmitter+once',
'usage-removeAllListeners': 'EnhancedEventEmitter+removeAllListeners',
'usage-else': 'EnhancedEventEmitter+else',
'usage-suspend': 'EnhancedEventEmitter+suspend',
'usage-else-error': 'EnhancedEventEmitter+elseError',
Expand Down
33 changes: 21 additions & 12 deletions README.md
Expand Up @@ -11,6 +11,7 @@
* [on(event, listener)](#usage-on1)
* [on(options)](#usage-on2)
* [once(event, listener)](#usage-once)
* [removeAllListeners](#usage-removeAllListeners)
* [else](#usage-else)
* [suspend](#usage-suspend)
* [elseError](#usage-else-error)
Expand Down Expand Up @@ -133,6 +134,25 @@ remove();
```
<!-- markdownlint-enable MD009 MD031 MD036 -->

<a name="usage-removeAllListeners"></a>
<!-- markdownlint-disable MD009 MD031 MD036 -->
### 'emitter.removeAllListeners([event])'
See node.js events.EventEmitter.removeAllListeners.<br>
This function is modified to also accept an array of event names.

**Example**
```js
var EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
var emitter = new EnhancedEventEmitter();

//same as the basic removeAllListeners
emitter.removeAllListeners('my-event');

//also supports array of event names
emitter.removeAllListeners(['my-event', 'another-event']);
```
<!-- markdownlint-enable MD009 MD031 MD036 -->

<a name="usage-else"></a>
<!-- markdownlint-disable MD009 MD031 MD036 -->
### 'emitter.else(listener)'
Expand Down Expand Up @@ -357,26 +377,15 @@ See [contributing guide](.github/CONTRIBUTING.md)

| Date | Version | Description |
| ----------- | ------- | ----------- |
| 2017-06-26 | v1.0.47 | Maintenance |
| 2017-10-30 | v1.0.48 | New extended 'removeAllListeners' function |
| 2017-01-16 | v1.0.27 | New extended 'once' function |
| 2017-01-13 | v1.0.26 | Maintenance |
| 2017-01-07 | v1.0.25 | New 'proxyEvents' function |
| 2017-01-06 | v1.0.24 | New extended 'on' function |
| 2016-12-31 | v1.0.23 | Maintenance |
| 2016-11-11 | v1.0.15 | 'emitAsync' callback is now optional |
| 2016-11-05 | v1.0.14 | Maintenance |
| 2015-09-23 | v0.0.44 | Added 'onAny' |
| 2015-09-08 | v0.0.43 | Maintenance |
| 2015-04-22 | v0.0.31 | Prevent from multiple enhance of same prototype/instance |
| 2015-04-05 | v0.0.30 | Maintenance |
| 2015-02-09 | v0.0.19 | Doc fix. |
| 2015-02-09 | v0.0.18 | Grunt cleanups |
| 2015-02-06 | v0.0.14 | Internal quality changes. |
| 2014-12-31 | v0.0.11 | Doc fix. |
| 2014-12-31 | v0.0.10 | EventEmitter is no longer automatically modified,<br>instead there are 2 ways to extend/modify prototype/modify instance<br>functions exposed by this library. |
| 2014-12-30 | v0.0.9 | Added ability to enhance compatible EventEmitter types |
| 2014-12-30 | v0.0.8 | Doc changes |
| 2014-12-29 | v0.0.7 | Added additional tests |
| 2014-12-29 | v0.0.6 | Added 'filter' |
| 2014-12-28 | v0.0.5 | Added 'onAsync' |
| 2014-12-28 | v0.0.4 | Added 'emitAsync' |
Expand Down
13 changes: 1 addition & 12 deletions docs/CHANGELOG.md
@@ -1,25 +1,14 @@
| Date | Version | Description |
| ----------- | ------- | ----------- |
| 2017-06-26 | v1.0.47 | Maintenance |
| 2017-10-30 | v1.0.48 | New extended 'removeAllListeners' function |
| 2017-01-16 | v1.0.27 | New extended 'once' function |
| 2017-01-13 | v1.0.26 | Maintenance |
| 2017-01-07 | v1.0.25 | New 'proxyEvents' function |
| 2017-01-06 | v1.0.24 | New extended 'on' function |
| 2016-12-31 | v1.0.23 | Maintenance |
| 2016-11-11 | v1.0.15 | 'emitAsync' callback is now optional |
| 2016-11-05 | v1.0.14 | Maintenance |
| 2015-09-23 | v0.0.44 | Added 'onAny' |
| 2015-09-08 | v0.0.43 | Maintenance |
| 2015-04-22 | v0.0.31 | Prevent from multiple enhance of same prototype/instance |
| 2015-04-05 | v0.0.30 | Maintenance |
| 2015-02-09 | v0.0.19 | Doc fix. |
| 2015-02-09 | v0.0.18 | Grunt cleanups |
| 2015-02-06 | v0.0.14 | Internal quality changes. |
| 2014-12-31 | v0.0.11 | Doc fix. |
| 2014-12-31 | v0.0.10 | EventEmitter is no longer automatically modified,<br>instead there are 2 ways to extend/modify prototype/modify instance<br>functions exposed by this library. |
| 2014-12-30 | v0.0.9 | Added ability to enhance compatible EventEmitter types |
| 2014-12-30 | v0.0.8 | Doc changes |
| 2014-12-29 | v0.0.7 | Added additional tests |
| 2014-12-29 | v0.0.6 | Added 'filter' |
| 2014-12-28 | v0.0.5 | Added 'onAsync' |
| 2014-12-28 | v0.0.4 | Added 'emitAsync' |
Expand Down
24 changes: 24 additions & 0 deletions docs/api.md
Expand Up @@ -42,6 +42,7 @@
* [#on(event, listener)](#EnhancedEventEmitter+on) ⇒ <code>function</code>
* [#on(options)](#EnhancedEventEmitter+on) ⇒ <code>function</code>
* [#once(event, listener)](#EnhancedEventEmitter+once) ⇒ <code>function</code>
* [#removeAllListeners([event])](#EnhancedEventEmitter+removeAllListeners)
* [#suspend(event)](#EnhancedEventEmitter+suspend)
* [#unsuspend(event)](#EnhancedEventEmitter+unsuspend)
* [#else(listener)](#EnhancedEventEmitter+else)
Expand Down Expand Up @@ -167,6 +168,29 @@ var remove = emitter.once('error', function (error) {
//remove listener (no longer need to keep a reference to the listener function)
remove();
```
<a name="EnhancedEventEmitter+removeAllListeners"></a>

### EnhancedEventEmitter#removeAllListeners([event])
See node.js events.EventEmitter.removeAllListeners.<br>
This function is modified to also accept an array of event names.

**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| [event] | <code>String</code> \| <code>Array</code> | The name/s of the event |

**Example**
```js
var EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
var emitter = new EnhancedEventEmitter();

//same as the basic removeAllListeners
emitter.removeAllListeners('my-event');

//also supports array of event names
emitter.removeAllListeners(['my-event', 'another-event']);
```
<a name="EnhancedEventEmitter+suspend"></a>

### EnhancedEventEmitter#suspend(event)
Expand Down
33 changes: 33 additions & 0 deletions lib/enhanced-event-emitter.js
Expand Up @@ -242,6 +242,39 @@ EnhancedEventEmitter.prototype.once = function (event, listener) {
});
};

/**
* See node.js events.EventEmitter.removeAllListeners.<br>
* This function is modified to also accept an array of event names.
*
* @function
* @memberof! EnhancedEventEmitter
* @public
* @param {String|Array} [event] - The name/s of the event
* @example
* ```js
* var EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
* var emitter = new EnhancedEventEmitter();
*
* //same as the basic removeAllListeners
* emitter.removeAllListeners('my-event');
*
* //also supports array of event names
* emitter.removeAllListeners(['my-event', 'another-event']);
* ```
*/
EnhancedEventEmitter.prototype.removeAllListeners = function (event) {
if (event) {
if (Array.isArray(event)) {
var index;
for (index = 0; index < event.length; index++) {
this.removeAllListeners(event[index]);
}
} else {
this.baseRemoveAllListeners(event);
}
}
};

/**
* Suspends all emit calls for the provided event name (including 'else' listeners).<br>
* For suspended events, the emit function will simply do nothing ('else' listeners won't be invoked either).
Expand Down
1 change: 1 addition & 0 deletions lib/event-emitter-enhancer.js
Expand Up @@ -84,6 +84,7 @@ EventEmitterEnhancer.prototype.enhance = function (EmitterType, modifyType) {
Emitter.baseEmit = Emitter.emit;
Emitter.baseOn = Emitter.on;
Emitter.baseOnce = Emitter.once;
Emitter.baseRemoveAllListeners = Emitter.removeAllListeners;

var functions = Object.keys(EnhancedEventEmitter.prototype);
functions.forEach(function addProperty(property) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "event-emitter-enhancer",
"version": "1.0.47",
"version": "1.0.48",
"description": "Enhances the Node.js EventEmitter with extra capabilities.",
"author": {
"name": "Sagie Gur-Ari",
Expand Down
3 changes: 3 additions & 0 deletions project/config/README-template.md
Expand Up @@ -11,6 +11,7 @@
* [on(event, listener)](#usage-on1)
* [on(options)](#usage-on2)
* [once(event, listener)](#usage-once)
* [removeAllListeners](#usage-removeAllListeners)
* [else](#usage-else)
* [suspend](#usage-suspend)
* [elseError](#usage-else-error)
Expand Down Expand Up @@ -66,6 +67,8 @@ EventEmitterEnhancer.modifyInstance(emitter); //modify the specific instance a

<a name="usage-once"></a>

<a name="usage-removeAllListeners"></a>

<a name="usage-else"></a>

<a name="usage-suspend"></a>
Expand Down
42 changes: 42 additions & 0 deletions test/spec/event-emitter-enhancer-spec.js
Expand Up @@ -6,6 +6,7 @@ var chai = require('chai');
var assert = chai.assert;
var EventEmitter = require('events').EventEmitter;
var EventEmitterEnhancer = require('../../lib/event-emitter-enhancer');
var funcs = require('funcs-js');

function createEventEmitter() {
var EnhancedEventEmitter = EventEmitterEnhancer.extend(EventEmitter);
Expand Down Expand Up @@ -33,6 +34,7 @@ describe('event-emitter-enhancer', function () {

assert.isFunction(emitter.baseOn);
assert.isFunction(emitter.baseOnce);
assert.isFunction(emitter.baseRemoveAllListeners);
assert.isFunction(emitter.baseEmit);

assert.isFunction(emitter.onAsync);
Expand All @@ -54,6 +56,7 @@ describe('event-emitter-enhancer', function () {

assert.isFunction(emitter.baseOn);
assert.isFunction(emitter.baseOnce);
assert.isFunction(emitter.baseRemoveAllListeners);
assert.isFunction(emitter.baseEmit);

assert.isFunction(emitter.onAsync);
Expand Down Expand Up @@ -100,6 +103,7 @@ describe('event-emitter-enhancer', function () {

assert.isFunction(emitter.baseOn);
assert.isFunction(emitter.baseOnce);
assert.isFunction(emitter.baseRemoveAllListeners);
assert.isFunction(emitter.baseEmit);

assert.isFunction(emitter.onAsync);
Expand Down Expand Up @@ -146,6 +150,7 @@ describe('event-emitter-enhancer', function () {

assert.isFunction(emitter.baseOn);
assert.isFunction(emitter.baseOnce);
assert.isFunction(emitter.baseRemoveAllListeners);
assert.isFunction(emitter.baseEmit);

assert.isFunction(emitter.onAsync);
Expand All @@ -167,6 +172,7 @@ describe('event-emitter-enhancer', function () {

assert.isFunction(emitter.baseOn);
assert.isFunction(emitter.baseOnce);
assert.isFunction(emitter.baseRemoveAllListeners);
assert.isFunction(emitter.baseEmit);

assert.isUndefined(EventEmitter.prototype.enhancedEmitterType);
Expand Down Expand Up @@ -225,6 +231,42 @@ describe('event-emitter-enhancer', function () {
});
});

describe('removeAllListeners', function () {
it('undefined', function () {
var emitter = createEventEmitter();

emitter.removeAllListeners();
});

it('single', function () {
var emitter = createEventEmitter();

emitter.on('error', funcs.noop);
emitter.on('error', funcs.noop);
assert.strictEqual(emitter.listenerCount('error'), 2);

emitter.removeAllListeners('error');

assert.strictEqual(emitter.listenerCount('error'), 0);
});

it('array', function () {
var emitter = createEventEmitter();

emitter.on('error', funcs.noop);
emitter.on('error', funcs.noop);
assert.strictEqual(emitter.listenerCount('error'), 2);

emitter.on('info', funcs.noop);
assert.strictEqual(emitter.listenerCount('info'), 1);

emitter.removeAllListeners(['error', 'info']);

assert.strictEqual(emitter.listenerCount('error'), 0);
assert.strictEqual(emitter.listenerCount('info'), 0);
});
});

describe('on', function () {
it('nodejs with remove', function () {
var emitter = createEventEmitter();
Expand Down

0 comments on commit e6961f4

Please sign in to comment.