[whatwg/dom] MutationObserver callbacks happen in wrong order. (Issue #1105)

Trying to port from DOM Mutation Events, or from parent dis/connectedCallbacks to a `MutationObserver` pattern that initializes or cleans up code based on when children are connected then disconnected, is nearly impossible without immense effort. I've been using `MutationObserver` for **years** now, and am discovering bugs in certain cases that I didn't notice before.

In the following example, the connected and disconnected events fire in the wrong order, which leave connected children erroneously cleaned up:

https://codepen.io/trusktr/pen/oNqrNXE/3aad3bb7315877d00c7c42e3d77fed5a?editors=1010

In this one, the behavior is a bit different, but still not as desired:

https://codepen.io/trusktr/pen/MWVMWKe/091e6f303bd773f2754304fb1c9bff30?editors=1000

With standard `connectedCallback` and `disconnectedCallback`, they always fire in this order per each custom element when an element disconnected and re-connected:

```
disconnectedCallback()
connectedCallback()
```

However, with this naive `MutationObserver` usage, the order (in these two scenarios) when a child is removed then re-connected is

```
child connected
child removed
```

which is obviously going to cause bugs.

---

DOM Mutation Events are infinitely easier to use. `MutationObserver` is incredibly difficult to work with in certain cases.

I believe that all the performance issues of Mutation Events could have been fixed, or a new synchronous alternative could have been created that would be no worse than today's `connectedCallback`/`disconnectedCallback`, which happens to be synchronous.

There's nothing about an event pattern that makes it inherently bad, and just because vendors implemented them with bugs, doesn't mean the spec needed to be removed.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1105
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1105@github.com>

Received on Friday, 26 August 2022 22:35:09 UTC