Revert on-load attribute special case behaviour #65
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR reverts the special case behaviour added in "morph onload tags on root": so the root element on-load attribute value is not maintained from
oldTree
tonewTree
.Why this change?
What's the issue with onUnload callbacks not firing?
This explanation requires understanding of the on-load library's source code..
For a given element, the on-load library updates the onloadid attribute value on every call to
onload(el, on, off, caller)
, say from 'o1' -> 'o2'. This attribute value change is seen by on-load's MutationObserver, which then calls theoff
(onUnload) function associated with 'o1' (and theon
(onLoad) function associated with 'o2').The special case behaviour copies the onloadid attribute value from
oldTree's
root element tonewTree's
root element ('o1' -> 'o1'). IfnewTree
already has an onloadid attribute (e.g. 'o2'), this is overwritten (with 'o1'). Since the root element's attribute value remains the same (always 'o1'), the MutationObserver doesn't fire, and the onUnload callback isn't called.Thoughts on html components
I'm not sure why this special case was added in the first place. My guess is that it aims to help with the lifecycle of an html component that re-renders on data changes:
renderedElement
, and added to the dom.onload
is called as we want the onLoad callback to fire:onload
called as part of the process. But we don't want any on-load callbacks to fire as we're just re-rendering the same component. So we introduce the special case of maintaining the current onloadid attribute value between old and new versions ofcomponent
, ignoring the new value fromrenderedElement
:If the special case was to avoid on-load callbacks on component re-renders, then there is a better way: for all
onload(renderedElement, on, off, caller)
calls made for the component, use the same unique-id forcaller
. The on-load library skips callbacks when the onloadid attribute value changes for an element, but the associatedcaller
value stays the same. See sameOrigin function at: https://github.com/shama/on-load/blob/master/index.js#L55Hope this makes sense!