Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/core/EventDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Object.assign( EventDispatcher.prototype, {

}

event.target = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, I'm not sure it's a good idea to alter the event object after it was dispatched. Users could work with the event object not just within the event listener. They could save the event and work with it later on. In this case it would be unexpected when target is suddenly null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point, although anyone being called knows what target is (this), in the listener functions. Is target used anywhere within Three? I can't see any obvious uses.

An alternative would be to zap it specifically in the Object3D.remove() method after the dispatch call. In those cases anyone saving the event object could get surprises anyway since it would be shared with other _removed events after that point.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the same thing but if that's the case any reused event definitions like this would be problematic because target will get overwritten with every subsequent call. TrackballControls, TransformControls, OrbitControls and I'm sure others all use this same pattern.

These events were originally moved to be reused in order to save on memory allocation (see #17224, #17134 (comment)) but I missed that the object was modified in the dispatchEvent function. I personally am okay with denoting that an event is only valid during the execution of the callback, which would allow us to save memory allocation in quite a few other places.

Setting the target to null in the dispatchEvent function would change the functionality relative to the built relative to the EventTarget.dispatchEvent function, though, so it might be more sensible to set target to null in the remove() function, instead.

Copy link
Collaborator

@Mugen87 Mugen87 Feb 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've totally forgot that three.js code already reuses event objects...

In this case, I prefer whatever @mrdoob likes. Also note that this class comes from a separate github repo. It could be useful to make changes also there: https://github.com/mrdoob/eventdispatcher.js/


}

}
Expand Down