Bug 16392 - MutationObserver invocation order should be fixed at the beginning of each loop
Summary: MutationObserver invocation order should be fixed at the beginning of each loop
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
Depends on:
Reported: 2012-03-15 22:03 UTC by Adam Klein
Modified: 2012-03-20 10:50 UTC (History)
3 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Adam Klein 2012-03-15 22:03:38 UTC
From www-dom:

an algorithm for delivering MutationRecords to MutationObservers.
In particular, it describes an order of delivery, and I wonder if tweaking
it a little bit would make it simpler to implement. Note that I don't think
the particular order is of much importance: it's just important that there
is a well-defined order.  In particular, there are two cases I'm worried

1. Assume observers A, B, and C (created in the order A, B, C). Say that at
the beginning of the algorithm, only A and C have non-empty queues. But
during A's callback, it mutates DOM that causes a record to be added to B's

2. Assume an observer A with a non-empty queue. During its callback, it
creates a new observer B, starts B observing, and mutates DOM that adds a
record to both A's and B's queue.

By the spec, case (1) would result in the delivery order A-B-C. And (2)
would be A-B-A.

In the WebKit implementation, though, only the "active" observers (those
with records in their queues) are kept in a list (this makes it fast in the
common case that there's no delivery necessary). This makes our algorithm
more like this:

I. Make a copy of the existing "active" list, clear the list, and then
iterate over the copy.
II. When that iteration is complete, the active list is checked again; if
it's non-empty, go back to step I.

When applied to the cases above, (1) results in the order A-C-B (B doesn't
get notified until the next time around the loop), and (2) results in A-A-B
(again, the newly-added observer doesn't get notified until the second time
through the loop).

I've implemented the aforementioned algorithm in WebKit. Per the thread, http://lists.w3.org/Archives/Public/www-dom/2012JanMar/0145.html, it sounds like Olli is fine with that algorithm too (CCing him just in case)