W3C home > Mailing lists > Public > www-dom@w3.org > January to March 2012

Re: Details of MutationObserver delivery order

From: Jonas Sicking <jonas@sicking.cc>
Date: Wed, 7 Mar 2012 12:35:40 -0800
Message-ID: <CA+c2ei9Ch1aqOw4STqbdyoy3H74JXuC+dJe+1qwp68xq7tCgww@mail.gmail.com>
To: Adam Klein <adamk@chromium.org>
Cc: www-dom@w3.org, Anne van Kesteren <annevk@opera.com>, Olli@pettay.fi
On Wed, Mar 7, 2012 at 8:52 AM, Adam Klein <adamk@chromium.org> wrote:
> http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-mo-invoke
> describes 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
> about:
>
> 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
> queue.
>
> 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).
>
> Thoughts? Also, if the above is too hard to follow, I have test-cases that
> demonstrate these cases, and could point at those instead.

This sounds good to me, though I'm happy to defer to Olli.

/ Jonas
Received on Wednesday, 7 March 2012 20:36:44 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:14:09 GMT