events: pass the original listener added by EventEmitter#once to the removeListener handler#6394
events: pass the original listener added by EventEmitter#once to the removeListener handler#6394DavidCai1111 wants to merge 1 commit intonodejs:masterfrom
Conversation
|
Why would we go through the trouble of trying to prevent that? Using the |
|
I’d say by explicitly invoking the listener in the |
|
@addaleax But as the output showed in the first code example, no matter invoking the |
|
I have to admit, I find the PR title here a bit confusing then. I think a much cleaner solution (and more aligned with user expectations?) would be to pass the original listener which has been passed to |
|
@addaleax It's a better solution, i will change the code. |
cffb713 to
b12c2ac
Compare
|
@addaleax Hey bro, I've changed the code and the content of this PR. Now the user will get the original listener which added by |
|
I like the change, but would like to head other’s opinions on this. I think this could be considered a bugfix, because this is how I’d understand the documentation (if I didn’t know how
Appreciate you not wanting to be formal, but I’m definitely not a bro, for more than one reason. :) |
|
I think so, too : = ) |
|
This makes sense, but could you add tests. |
|
@cjihrig 👌 |
b12c2ac to
fa40a5a
Compare
|
@cjihrig Already added : = ) |
|
I had no idea the original function wasn't passed, I'd expect the listener passed to once to be the listener passed in the removeListener event! |
|
@sam-github It will be, if this PR merged. |
There was a problem hiding this comment.
Both assertions can be assert.strictEqual().
|
LGTM pending comments and CI. Are we classifying this as semver major or patch? I'd lean toward patch, but this could potentially break things. |
fa40a5a to
d26733d
Compare
|
Yeah, I think this breaks only code that is already broken. So, leaning towards patch, too. |
|
I lean towards patch, too. |
|
Related (covers the same ground): #5564 |
|
Seems that the solution code in this PR is more lightweight and harmless ? |
|
Look into #5564 line 338: change to seems more readable. |
There was a problem hiding this comment.
might be clearer to just inline listener6 with the removeListener line below instead of creating a separate decl for it
|
LGTM with a nit. Pending CI of course. |
|
Looks good, landing |
When removing a `once` listener, the listener being passed to the `removeListener` callback is the wrapper. This unwraps the listener so that `removeListener` is passed the actual listener. PR-URL: #6394 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
|
Landed in 706778a |
|
Shouldn't this be tagged as |
|
Yes. Done. |
|
Wait, I take that back. We talked about this the other day. Most people (myself included) were leaning toward patch. I can see how it would be a major though. |
|
That was brought up a few comments up: #6394 (comment) |
|
@mscdex ... it could be see discussion here #6394 (comment) ... the existing behavior can be rightfully considered to be a bug and this is just a fix. I'm -1 on it being semver-major. |
|
@mscdex ... to be on the safe side we can hold off on pulling this back to v5 or v4 for a while in case there are any regressions caused. |
When removing a `once` listener, the listener being passed to the `removeListener` callback is the wrapper. This unwraps the listener so that `removeListener` is passed the actual listener. PR-URL: #6394 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
When removing a `once` listener, the listener being passed to the `removeListener` callback is the wrapper. This unwraps the listener so that `removeListener` is passed the actual listener. PR-URL: nodejs#6394 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
|
do you think it is about time ot backport @nodejs/lts? |
|
@nodejs/lts @nodejs/ctc thoughts on this being backported? |
|
I think its reasonable to backport |
When removing a `once` listener, the listener being passed to the `removeListener` callback is the wrapper. This unwraps the listener so that `removeListener` is passed the actual listener. PR-URL: #6394 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Checklist
Description of change
We use the
_onceWrapfunction to wrap the listener added byEventEmitter#once, and use the flagfiredinside to make sure the listener will only be called once, including the calling in theremoveListenerevent:But by explicitly invoking the listener in the removeListener handler the user expresses a pretty strong intent that the listener should be called. By now we can use a
listener.listenertrick to archive this:But those who have not read the the code of
lib/events.jsshould not know this trick at all, and using tricks is alway not a good way. so this PR is to pass the original listener added byEventEmitter#onceto theremoveListenerhandler: