Skip to content

Allow MutationObserver to observe connectedness and add unobserve() - #1508

Open
annevk wants to merge 2 commits into
mainfrom
annevk/observe-connectedness
Open

annevk wants to merge 2 commits into
mainfrom
annevk/observe-connectedness

Conversation

@annevk

@annevk annevk commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Add a connectedness member to MutationObserverInit. When it is true,
the observer gets a "connected" or "disconnected" record each time the
target becomes connected or stops being connected, including when that
happens because an ancestor or shadow host is inserted or removed. The
subtree member does not apply to connectedness, as that would require
a record for each node of an inserted or removed subtree.

These records are queued where the connectedCallback and
disconnectedCallback custom element reactions are enqueued. That puts
them before the tree mutation record for the insertion or removal that
caused them, also when that record is queued later, such as for
replaceChildren(). The move algorithm does not queue them as it cannot
change whether a node is connected.

As this makes it natural to observe many individual nodes with a
single MutationObserver object, also add unobserve(target), which
stops observing target as requested through observe() without
affecting other targets. Unlike disconnect(), it does not empty the
record queue, as a record can be the result of multiple registered
observers. This matches IntersectionObserver's unobserve().

Fixes #126. Fixes #533.

Tests: web-platform-tests/wpt#63396

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

@WebReflection

Copy link
Copy Markdown

this is quite possibly the best thing ever in a while, I can't wait to make all my libraries deprecated once this land, huge thanks to anyone involved 🙏

@annevk
annevk force-pushed the annevk/transient-registered-observers branch from f5f5f6e to ada81c8 Compare October 8, 2026 09:46
@annevk
annevk force-pushed the annevk/observe-connectedness branch from 0dbc504 to 7bdca97 Compare October 8, 2026 09:47
@annevk annevk added addition/proposal New features or enhancements topic: mutation observers agenda+ To be discussed at a triage meeting labels Oct 8, 2026
Base automatically changed from annevk/transient-registered-observers to main October 9, 2026 13:59
annevk added 2 commits October 9, 2026 16:11
Add a connectedness member to MutationObserverInit. When it is true,
the observer gets a "connected" or "disconnected" record each time the
target becomes connected or stops being connected, including when that
happens because an ancestor or shadow host is inserted or removed. The
subtree member does not apply to connectedness, as that would require
a record for each node of an inserted or removed subtree.

These records are queued where the connectedCallback and
disconnectedCallback custom element reactions are enqueued. That puts
them before the tree mutation record for the insertion or removal that
caused them, also when that record is queued later, such as for
replaceChildren(). The move algorithm does not queue them as it cannot
change whether a node is connected.

As this makes it natural to observe many individual nodes with a
single MutationObserver object, also add unobserve(target), which
stops observing target as requested through observe() without
affecting other targets. Unlike disconnect(), it does not empty the
record queue, as a record can be the result of multiple registered
observers. This matches IntersectionObserver's unobserve().

Fixes #126. Fixes #533.
@annevk
annevk force-pushed the annevk/observe-connectedness branch from 7e8a5c8 to 811f16d Compare October 9, 2026 14:11
@FrameMuse

Copy link
Copy Markdown

@WebReflection Well, not all libraries, we will need a polyfill in the first years :)

@jstenback jstenback removed the agenda+ To be discussed at a triage meeting label Nov 4, 2026
@noamr

noamr commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

We've looked at the use cases for this posted in the issue and it seems useful.
You can mark chromium as supportive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

Make it possible to observe connected-ness of a node Disconnect single target instead of all

5 participants