Trying to understand the observeOwnChanges option

70 views
Skip to first unread message

Justin Meltzer

unread,
Oct 18, 2012, 7:40:07 PM10/18/12
to mutation-sum...@googlegroups.com
I'm trying to understand the purpose of the observeOwnChanges option. Looking at the Mutation Summary source, it seems that if observeOwnChanges is set to false, then anytime the Mutation Observer API executes the callback, it disconnects the observer after pulling the mutations, creates summaries from them, returns those summaries inside the passed callback, and then reconnects the observer. Why would it be necessary to do this? If observeOwnChanges is set to false, then won't the mutation summary library fail to report any mutations that occurred on the page while it was creating the summaries?

Rafael Weinstein

unread,
Oct 18, 2012, 10:31:35 PM10/18/12
to mutation-sum...@googlegroups.com
Hi Justin,

It's a good question.

On Thu, Oct 18, 2012 at 4:40 PM, Justin Meltzer <jus...@airtimehq.com> wrote:
> I'm trying to understand the purpose of the observeOwnChanges option.
> Looking at the Mutation Summary source, it seems that if observeOwnChanges
> is set to false, then anytime the Mutation Observer API executes the
> callback, it disconnects the observer after pulling the mutations, creates
> summaries from them, returns those summaries inside the passed callback, and
> then reconnects the observer. Why would it be necessary to do this? If

The question is: do you want to be told in *the next* callback about
changes you make in *this callback*? Most frequently the answer is
"no". If you made the changes, you're unlikely to want to hear about
them and that's why it defaults to |false|.

Note that this option has no effect on changes you make *outside* the
callback (for example inside a DOM event handler). Those changes will
always be included in the calculation.

> observeOwnChanges is set to false, then won't the mutation summary library
> fail to report any mutations that occurred on the page while it was creating
> the summaries?

No. Access to the DOM is single-threaded. It's not possible for other
code which mutates the DOM to run while the MutationSummary is doing
its computation for you.

>
> --
> You received this message because you are subscribed to the Google Groups
> "mutation-summary-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mutation-summary-discuss/-/cFA5hJmHFaQJ.
> To post to this group, send email to
> mutation-sum...@googlegroups.com.
> To unsubscribe from this group, send email to
> mutation-summary-d...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mutation-summary-discuss?hl=en.

Justin Meltzer

unread,
Oct 19, 2012, 12:28:53 AM10/19/12
to mutation-sum...@googlegroups.com
Ahh that makes sense. Thanks Rafael!

Does this also apply to any dom mutations that may be triggered inside a callback that is executed by, say, setTimeout?

For example, if you have 

setTimeout(function(){ document.body.appendChild(node); }, 1000);

that happens to execute while mutation summary is creating a summary. Will the browser still wait until the synchronous code is finished and the observer has reconnected to append the node, or will it context switch back and forth?

Rafael Weinstein

unread,
Oct 19, 2012, 3:54:01 PM10/19/12
to mutation-sum...@googlegroups.com
On Fri, Oct 19, 2012 at 6:28 AM, Justin Meltzer <jus...@airtimehq.com> wrote:
> Ahh that makes sense. Thanks Rafael!
>
> Does this also apply to any dom mutations that may be triggered inside a
> callback that is executed by, say, setTimeout?
>
> For example, if you have
>
> setTimeout(function(){ document.body.appendChild(node); }, 1000);
>
> that happens to execute while mutation summary is creating a summary. Will
> the browser still wait until the synchronous code is finished and the
> observer has reconnected to append the node, or will it context switch back
> and forth?

The browser doesn't ever context switch in this way. If there's a
setTimeout callback pending, it has to wait until the current Task is
completed. There's no danger of the current running script being
preempted.
>> > mutation-summary-d...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/mutation-summary-discuss?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "mutation-summary-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mutation-summary-discuss/-/rQhhRZryGZcJ.
>
> To post to this group, send email to
> mutation-sum...@googlegroups.com.
> To unsubscribe from this group, send email to
> mutation-summary-d...@googlegroups.com.

Justin Meltzer

unread,
Oct 19, 2012, 6:10:15 PM10/19/12
to mutation-sum...@googlegroups.com
Interesting. Thanks!

Btw thanks so much for this library. I had actually built my own version of mutation summary/tree mirror (albeit much less robust and clean) before finding out that this existed. So I'm just going with this.
>> > For more options, visit this group at
>> > http://groups.google.com/group/mutation-summary-discuss?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "mutation-summary-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mutation-summary-discuss/-/rQhhRZryGZcJ.
>
> To post to this group, send email to
> mutation-sum...@googlegroups.com.
> To unsubscribe from this group, send email to

Rafael Weinstein

unread,
Oct 20, 2012, 5:13:27 AM10/20/12
to mutation-sum...@googlegroups.com
Terrific! Glad you are finding it useful. Please let me know if you
hit bugs or missing features.

Cheers
R
>> >> > mutation-summary-d...@googlegroups.com.
>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/mutation-summary-discuss?hl=en.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "mutation-summary-discuss" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/mutation-summary-discuss/-/rQhhRZryGZcJ.
>> >
>> > To post to this group, send email to
>> > mutation-sum...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > mutation-summary-d...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/mutation-summary-discuss?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "mutation-summary-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mutation-summary-discuss/-/_4cLc-0w0toJ.
>
> To post to this group, send email to
> mutation-sum...@googlegroups.com.
> To unsubscribe from this group, send email to
> mutation-summary-d...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages