shama / on-load

On load/unload events for DOM elements using a MutationObserver
113 stars 19 forks source link

trigger the "onunload" event only if the node is not mounted on the DOM #25

Open yoshuawuyts opened 7 years ago

yoshuawuyts commented 7 years ago

Problem

on-load works by hooking into a MutationObserver and listening for change events. These events are the very first thing that resolve on each frame.

Sometimes DOM nodes are briefly removed from the DOM, only to be immediately reinserted. Reordering / sorting lists is a common example of this. Because of how on-load is currently set up, even briefly removing an element from the DOM can cause the MutationObserver to trigger.

This makes it impossible to reorder lists of elements with onload() listeners attached.

Proposal

I propose we mitigate this by having on-load check if the node is still on the DOM by the time the MutationObserver resolves, so list reordering can be performed. My gut feeling tells me this shouldn't cause any significant performance overhead. Thanks!

See Also

bcomnes commented 6 years ago

This should be fixed in 4.0.0 lets reopen if its not.