Skip to content

Commit 7a51878

Browse files
bengljasnell
authored andcommitted
doc, test: symbols as event names
* Document that Symbol can used as event names. * Add test for using Symbol as event names PR-URL: #4151 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9538fd0 commit 7a51878

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

doc/api/events.markdown

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ is opened. All objects which emit events are instances of `events.EventEmitter`.
1010
You can access this module by doing: `require("events");`
1111

1212
Typically, event names are represented by a camel-cased string, however,
13-
there aren't any strict restrictions on that, as any string will be accepted.
13+
there aren't any strict restrictions on that, as any valid property key will be
14+
accepted.
1415

1516
Functions can then be attached to objects, to be executed when an event
1617
is emitted. These functions are called _listeners_. Inside a listener
@@ -59,7 +60,7 @@ Returns the number of listeners for a given event.
5960

6061
### Event: 'newListener'
6162

62-
* `event` {String} The event name
63+
* `event` {String|Symbol} The event name
6364
* `listener` {Function} The event handler function
6465

6566
This event is emitted *before* a listener is added. When this event is
@@ -70,7 +71,7 @@ added.
7071

7172
### Event: 'removeListener'
7273

73-
* `event` {String} The event name
74+
* `event` {String|Symbol} The event name
7475
* `listener` {Function} The event handler function
7576

7677
This event is emitted *after* a listener is removed. When this event is
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const EventEmitter = require('events');
5+
const assert = require('assert');
6+
7+
const ee = new EventEmitter();
8+
const foo = Symbol('foo');
9+
const listener = common.mustCall(function() {});
10+
11+
ee.on(foo, listener);
12+
assert.deepEqual(ee.listeners(foo), [listener]);
13+
14+
ee.emit(foo);
15+
16+
ee.removeAllListeners();
17+
assert.deepEqual(ee.listeners(foo), []);
18+
19+
ee.on(foo, listener);
20+
assert.deepEqual(ee.listeners(foo), [listener]);
21+
22+
ee.removeListener(foo, listener);
23+
assert.deepEqual(ee.listeners(foo), []);

0 commit comments

Comments
 (0)