-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[discussion] Async nature of MutationObserver can cause room for error? #399
Comments
The problem with a synchronous library is that you can then put the browser in a state of it not knowing what's going on. We used to have that and mutation observers are the replacement as it caused all kinds of problems. With mutation observers the code that you write needs to be a little more involved. You get the changes from the API and you need to figure out if you just get the state from the tree instead or use the changesets in some way. |
I suppose you're talking about DOM mutation events? If a feature like From the discussion that is linked in the MDN page, the DOM mutation event problems
could possibly be solved with the callbacks (as far as Custom Elements go), as in
The limitation of these new callbacks would be that they only exist on custom elements, so there's no way to arbitrarily watch some other node (f.e. a |
Yeah, notifying after the DOM operation completes is safe. Last time this came up though implementers were somewhat worried about performance if we gave the option to get after-operation notifications. |
I'm going to close this. If you want to propose a synchronous API you could do so via a dedicated thread, though I don't think there would be a high chance of success. Custom elements is probably the way to go. |
A library that I'm working on creates components that contain HTMLElements. The user can run some code like this:
Behind the scenes, the synchronous
removeChild
andaddChild
methods each trigger aMutationRecord
to be recorded for the element referenced byparent
. This means that at the next microtaskparent
element's MutationObserver's callback will be called with two MutationRecords: one forremovedNodes
and the other foraddedNodes
.The problem is this: I wrote
childConnectedCallback
andchildDisconnectedCallback
methods for my components, and those methods are called by aMutationObserver
whenever the observation callback sees items inaddedNodes
andremovedNodes
, respectively. In the above example,parent.removedChild(child)
andparent.addChild(child)
are synchronous in the same turn, so this causes twoMutationRecords
to be handled in a future microtick. So, after both methods are called, thenchildDisconnectedCallback
andchildConnectedCallback
are fired forremovedNodes
andaddedNodes
, respectively, in the same order at theremoveChild
andaddChild
calls. Basically, the order of method calls is this:However, I wrote the
childDisconnectedCallback
andchildConnectedCallback
methods as if they were synchronous, not really considering the less common case when a child is removed and added back to the same parent in the same turn. It turns out that in my case my library gets into an error state due to the out-of-order calling of the methods.Considering that MutationObserver callbacks are not synchronously fired, I can temporarily work around the problem by changing the user code to the following, which causes
removedNodes
andaddedNodes
MutationObserver callbacks to be interleaved with theremoveChild
andaddChild
calls respectively:The method order is then:
Do you get what I mean?
I suppose I am wondering if a synchronous API would be a nice alternative (f.e. a library author might not overlook async behavior like I did). One way in which there could be a synchronous API for this is with
childConnectedCallback
andchildDisconnectedCallback
for the Custom Element API.The text was updated successfully, but these errors were encountered: