From 830caeb1bd0ba4771a671bba87ac427421e455e1 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Thu, 3 Dec 2015 22:58:18 -0800 Subject: [PATCH] doc, test: symbols as event names * Document that Symbol can used as event names. * Add test for using Symbol as event names PR-URL: https://github.com/nodejs/node/pull/4151 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- doc/api/events.markdown | 7 ++++--- test/parallel/test-event-emitter-symbols.js | 23 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-event-emitter-symbols.js diff --git a/doc/api/events.markdown b/doc/api/events.markdown index 2d17ca2f83b6b8..92bec13632683a 100644 --- a/doc/api/events.markdown +++ b/doc/api/events.markdown @@ -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 @@ -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 @@ -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 diff --git a/test/parallel/test-event-emitter-symbols.js b/test/parallel/test-event-emitter-symbols.js new file mode 100644 index 00000000000000..d34938da3c8ad6 --- /dev/null +++ b/test/parallel/test-event-emitter-symbols.js @@ -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), []);