-
Notifications
You must be signed in to change notification settings - Fork 7.3k
events: shared object references are emitted to listeners #25466
Comments
Yes, I've run into this a few times myself. It's definitely not ideal behavior. I'll schedule some time to review the PR but changing this behavior is, unfortunately, an API change that needs to be handled with a bit of care. cc @joyent/node-coreteam |
Oh joy. I bet at least some people are using this in convoluted ways. |
I'm -1 on this. While I sympathize with the current implementation's problems, performing deep copies can be expensive. Isn't it JavaScript convention to pass objects around by reference anyway? |
-1 for the same reasons as @cjihrig though. |
@cjihrig ... generally yes, but this behavior should, at the very least, be documented and as far as I can tell it is not. I'm just as concerned about the deep copying tho. Forcing a deep copy for every listener is going to be extremely expensive and likely completely unnecessary. However, it may be worthwhile providing a utility that performs a deep copy on a per listener basis... something along the lines of:
This way not all listeners would pay the deep copy penalty. At the very least, this needs to be documented. |
+1 for documenting |
@Hunchmun ... definitely appreciate the PR but given the discussion here, I think the better approach is going to be documenting the behavior in |
@jasnell absolutely, I'll comment back when it has been updated! |
@jasnell please find above the updated PR (With the initial commit reverted). |
@Hunchmun ... Thank you for updating and being patient with us. I have this on my list of items to review either tomorrow or Friday (it's a bit of a crazy week here). If you don't see action on this by the end of the day Friday, give me another ping here. |
Hi @jasnell , just following up as requested, Thanks, |
Thanks! I haven't forgotten, just on vacation at the moment. It's on my
|
@jasnell Hope you had a great vacation, just a friendly reminder of this issue, All the best! |
Updated documentation as per the issue below: #25466 Event listeners can alter parts of the passed object, in some circumstances the changes are passed to the next listeners due to pass by reference. This is documentation of that behavior. PR-URL: #25467 Reviewed-By: jasnell - James M Snell <jasnell@gmail.com>
Updated documentation as per the issue below: nodejs#25466 Event listeners can alter parts of the passed object, in some circumstances the changes are passed to the next listeners due to pass by reference. This is documentation of that behavior. PR-URL: nodejs#25467 Reviewed-By: jasnell - James M Snell <jasnell@gmail.com>
When emitting to multiple listeners for the same event type, the for
loop iteration over these listeners shares the same arguments. This
means (for objects), the copy is only shallow, and changes made by one
listener are overwritten on the object for the next listener.
For example, if we pass an object with arrays: as below
And the first listener sets the users array as null like so:
The second event listener will ALWAYS show the array as null. This is
because only a shallow copy of the arguments is made and not a deep
copy.
Here is the full code snippet that will reproduce the issue predictably:
The text was updated successfully, but these errors were encountered: