Skip to content

Commit

Permalink
doc, test: symbols as event names
Browse files Browse the repository at this point in the history
* Document that Symbol can used as event names.
* Add test for using Symbol as event names

PR-URL: #4151
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bengl authored and cjihrig committed Dec 15, 2015
1 parent 89ec013 commit 27a1e14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 4 additions & 3 deletions doc/api/events.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ is opened. All objects which emit events are instances of `events.EventEmitter`.
You can access this module by doing: `require("events");`

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

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

### Event: 'newListener'

* `event` {String} The event name
* `event` {String|Symbol} The event name
* `listener` {Function} The event handler function

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

### Event: 'removeListener'

* `event` {String} The event name
* `event` {String|Symbol} The event name
* `listener` {Function} The event handler function

This event is emitted *after* a listener is removed. When this event is
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-event-emitter-symbols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../common');
const EventEmitter = require('events');
const assert = require('assert');

const ee = new EventEmitter();
const foo = Symbol('foo');
const listener = common.mustCall(function() {});

ee.on(foo, listener);
assert.deepEqual(ee.listeners(foo), [listener]);

ee.emit(foo);

ee.removeAllListeners();
assert.deepEqual(ee.listeners(foo), []);

ee.on(foo, listener);
assert.deepEqual(ee.listeners(foo), [listener]);

ee.removeListener(foo, listener);
assert.deepEqual(ee.listeners(foo), []);

0 comments on commit 27a1e14

Please sign in to comment.