Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc, test: Symbols as event names #4151

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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), []);