shama / on-load

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

unload doesn't fire for independently-updated subtree #11

Open anandthakker opened 8 years ago

anandthakker commented 8 years ago

This is a pretty specific edge case.

Here's the setup: a parent view renders most of the tree, but stops at a certain node (using onBeforeElChildrenUpdated), and a separate "child" view is rendered/updated into that subtree. The render step of the app currently doesn't "care" that these views are nested in this way -- it's basically just:

views.forEach((view) => yo.update(view.tree, view.render(state), opts)

Here's the problem behavior: when the parent view ceases to render the child view's container element, the child view is (correctly) no longer in the document.body tree -- it's detached. However, the child's onunload handler does not fire.

Here's why I think it's happening. When the parent view removes the child view's container element, on-load's mutation observer doesn't fire until after the child view has rendered again -- but the re-render invoked on-load again, incrementing the onload id value on the child element. This would be okay, except that I think (a) the attribute mutation that associates the new id with the correct handler doesn't happen in time and (b) the turnon handler never fires at the new id, because it's rendered into a detached tree.

I'll add a skipped test in a PR.