Repository navigation
Lifecycle auto destroy - #1309
Merged
Merged
Conversation
ADR 0001: Destroy patterns automatically when they are removed from the DOM, using a single MutationObserver. ADR 0002: Self managed containers, which opt out of the automatic destroy and manage the lifecycle of their patterns themselves. Assisted-by: Claude Opus 5.5
…rom the DOM. A single MutationObserver on the document (src/core/lifecycle.js) tracks BasePattern instances after init. For each mutation batch with removed nodes, instances whose element is not connected to the document anymore are destroyed. Elements moved within the same task are kept; elements detached and re-inserted in a later task are destroyed. Instances on detached DOM are not tracked. destroy() is now idempotent and sets this.destroyed. A pattern destroyed before init is not initialized and throws not-init. A pattern whose element was removed during init is destroyed after init. destroy() only removes the pattern reference on the element if it points to the destroyed instance - before, destroying a not-initialized second instance removed the reference to the initialized first one. pat-tooltip: Do not access the Tippy instance after destroy. Async content loading continued after the pattern was destroyed and failed on the destroyed Tippy instance. Included here, as automatic destroy makes this happen. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
Patterns inside a detached container marked with the data-pat-lifecycle-managed attribute (lifecycle.MANAGED_ATTRIBUTE) are not destroyed automatically. Patterns removed from within such a container are destroyed as usual. This is for components which detach and re-attach their own DOM, like Tippy's popper in pat-tooltip. lifecycle.destroy_patterns(root) destroys all tracked patterns within a connected or detached subtree. Components using managed containers use it to destroy the patterns in their container. Ref: ADR 0002 Assisted-by: Claude Opus 5.5
this.remove_event_listener(el, name) removes a listener registered with this.add_event_listener under the same name, without building the instance namespaced id by hand. Without an element nothing is removed, instead of removing the listener from all elements. Assisted-by: Claude Opus 5.5
…ring init. If a pattern was destroyed while its asynchronous init was still running, listeners registered with add_event_listener later in init were never removed. Remove them after init. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
Assisted-by: Claude Opus 5.5
…egistry. event_listener_map is now a WeakMap. Elements with registered listeners are garbage collected when they are not referenced otherwise, instead of being kept in memory until their listeners are removed explicitly. As a WeakMap cannot be iterated, listeners can only be removed per element. remove_event_listener without an element logs a warning and removes nothing. Removing by id, by wildcard id and all listeners of an element keeps working. BasePattern remembers the elements it registered listeners on and removes its listeners from them on destroy. Ref: ADR 0003 Assisted-by: Claude Opus 5.5
Assisted-by: Claude Opus 5.5
events.listener_scope(namespace) returns a ListenerScope, which registers listeners via add_event_listener under the id "<namespace>--<name>" and remembers their elements and names. scope.remove(el, name) removes listeners of the scope from an element, also with wildcard names. scope.remove_all() removes all listeners of the scope on all elements. This allows removing a whole namespace of listeners, which is not possible with remove_event_listener alone since the event listener registry is a WeakMap. A scope only removes the listeners it registered itself, so scopes with overlapping namespaces do not interfere. Ref: ADR 0004 Assisted-by: Claude Opus 5.5
…eners. BasePattern now manages the listeners registered with this.add_event_listener with an event listener scope instead of its own element tracking. The listener ids stay the same. The scope is stored in a private field, so that it cannot collide with properties of subclasses - pat-bind, for example, has its own this._listeners. this.remove_event_listener(el, name) now also accepts wildcard names, and without a name removes all listeners of the instance on the element. Add documentation for the event listener helpers and scopes in src/core/events.md. Ref: ADR 0004 Assisted-by: Claude Opus 5.5
…nd show. Tippy removes the popper from the document while the tooltip is hidden. Mark the popper as self managed, so that patterns in static tooltip content are not destroyed on hide and initialized again on show. Destroy the patterns in the popper when the tooltip is destroyed, and for ajax content on hide before the content is replaced. Ref: ADR 0002 Assisted-by: Claude Opus 5.5
The reset handler only created a bound function of set_state without calling it, so dependent elements were not updated after a form reset. Assisted-by: Claude Opus 5.5
Register the listeners on the dependency inputs, their forms and the anchor click blocker with the instance scoped add_event_listener. The listeners on inputs outside the pattern element were never removed, so a destroyed pattern kept updating its removed element on input. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
The pat-update handler on the document was never removed. A destroyed pattern kept reacting to pat-update events and could focus its removed element. Register the handler under an instance specific event namespace and remove it on destroy. It stays a jQuery handler, as pat-update is mostly triggered via jQuery, which does not reach native listeners. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
The open and close trigger handlers delegated from the document and the handlers on an external trigger were never removed, so a destroyed pattern kept toggling its removed element. Register them under an instance specific event namespace and remove them on destroy. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
The observer was never disconnected. The observed content elements usually stay in the page, so a destroyed scroll marker kept updating the classes on its removed links and on the content elements. Now destroy disconnects the observer, removes the current class from the content elements and pending debounced updates do not run anymore. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
…iner. The scroll end listener was registered on the scroll container or the document, but removed from the pattern element. So it was never removed and stayed on the document. All instances also shared the same listener id, so they replaced each other's listener. Register and remove the listener with the instance scoped add_event_listener and remove_event_listener on the scroll container. This also removes it when the pattern is destroyed. A delayed scroll does not run anymore if the pattern was destroyed in the meantime. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
All instances registered their scroll listener under the same id on the shared scroll container or the document. As registering under the same id replaces the previous listener, only the last bumper of a scroll container was updated. Register the listener with the instance scoped add_event_listener. This also removes it when the pattern is destroyed. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
Register the click listener and the temporary scroll listener on the scroll container with the instance scoped add_event_listener, so that they are removed when the pattern is destroyed. The scroll listener was registered under a fixed id, shared by all instances. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
Register the submit and input listeners with the instance scoped add_event_listener, so that they are removed when the pattern is destroyed. Before, the listener registry kept the removed form and its inputs referenced. The input listener names do not contain the input's position anymore. When re-initializing after pat-update, an input which changed its position got a second set of listeners. Listeners of inputs which are gone after re-initializing are removed. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
Register the click blocker and the close-panel listener on the popper with the instance scoped add_event_listener, so that they are removed when the pattern is destroyed. Before, the listener registry kept the removed element and the popper referenced. Ref: ADR 0001 Assisted-by: Claude Opus 5.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 adds the ability to call
destroyand unregister all event listeners or observers for a Pattern, after an element has been removed from the DOMMore info: Read the ADRs in this PR.