Uh oh!
There was an error while loading. Please reload this page.
doc, test: Symbols as event names - #4151
Conversation
JungMinu
commented
Dec 4, 2015
There was a problem hiding this comment.
You can wrap this in common.mustCall(), then you don't need didRun.
cjihrig
commented
Dec 4, 2015
LGTM with a few comments. |
Fishrock123
commented
Dec 4, 2015
Tests aren't happy: @bengl could you please be sure to run |
There was a problem hiding this comment.
I think this needs to be '../common'
There was a problem hiding this comment.
This should be require('../common');
638e599 to
f0f0484Comparebengl
commented
Dec 4, 2015
Fixed the |
There was a problem hiding this comment.
Hmm.. is this actually something we want to promote? I know it works but should it. /cc @nodejs/ctc
There was a problem hiding this comment.
It's not documented, but you can emit on objects as well.
constEE=require('events');conste=newEE();constb={foo: 42};e.on(b,function(){console.log('hi');});e.emit(b);There was a problem hiding this comment.
@trevnorris Sort of. Because b coerces to [object Object], emitting on any object will fire the same event as b.
constEE=require('events');conste=newEE();e.on({foo: 42},function(){console.log('hi');});e.emit({bar: 43});// hie.emit({baz: 44});// hiSymbols don't get coerced for property identifiers though, so they're distinct here I think.
constEE=require('events');conste=newEE();consta=Symbol('foo');e.on(a,function(){console.log('hi');});e.emit(Symbol('foo'));// no hiThere was a problem hiding this comment.
hah. nice. never bothered to test that thoroughly.
There was a problem hiding this comment.
Perhaps "any valid property key" is the most correct thing to put here? That encompasses everything that's actually supported and doesn't encourage or discourage the use of Symbols.
There was a problem hiding this comment.
That may be better. I'm wondering too if we shouldn't simply throw if it's anything other than a "valid property key". Passing in an object simply doesn't make sense. Either way tho, this LGTM. Thanks!
Qard
commented
Dec 8, 2015
👍 I like the idea of promoting symbol keys because it allows for private events on emitters. |
trevnorris
commented
Dec 9, 2015
@Qard Not really "private". Can still be just as easily accessed via |
Qard
commented
Dec 9, 2015
Yeah, private-ish. There's always some escape hatch, isn't there? 😉 |
bengl
commented
Dec 11, 2015
Alright I replaced "any string or symbol" with "any valid property key". |
jasnell
commented
Dec 11, 2015
LGTM |
jasnell
commented
Dec 14, 2015
@cjihrig .. does this still LGTY? |
cjihrig
commented
Dec 14, 2015
Yes, but I'd still like to see the additional assertion that I originally requested. |
jasnell
commented
Dec 14, 2015
bengl
commented
Dec 14, 2015
cjihrig
commented
Dec 14, 2015
Yes, thank you! LGTM |
* 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>
jasnell
commented
Dec 14, 2015
Landed in 7a51878 |
* 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>
* 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>
* 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>
* Document that Symbol can used as event names. * Add test for using Symbol as event names PR-URL: nodejs#4151 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
As a result of the new support for Symbols in V8, along with the implementation of EventEmitter in Node, it turns out that Symbols are usable as event names. This adds the possibility of Symbol event names to the docs, and adds an EventEmitter test specific to Symbols so as to ensure that this doesn't break due to any potential future alternative implementations.