why does onmutation disconnect after first mutation?

207 views
Skip to first unread message

Seth Ladd

unread,
Oct 10, 2013, 2:06:09 PM10/10/13
to polymer-dev
Hi all,

I see onMutation (a nice helper!) here: https://github.com/Polymer/polymer/commit/528129c  Why does it disconnect after the first mutation?

If we expect that there will be many mutations to a node, should we just use the raw MutationObserver ?

I like onMutation... it's easier to use, but I'd like it to not disconnect (unless I'm missing a subtle part of the API)

Thanks for the context,
Seth

Scott Miles

unread,
Oct 10, 2013, 2:16:48 PM10/10/13
to Seth Ladd, polymer-dev
It's tempting for users to use `async` or `timeout` to try to wait for DOM updates after modifying some data. As we know, it's easy to get into a race-condition this way, and so it's an anti-pattern.

Going through our examples, I found some usage like this and wanted to replace it with a mutation observer. In all those use cases, these were one-off situations. I change this, then when the DOM updates, change that. For this reason, I coded `onMutation` as a one-time use API.

There are clearly use-cases that want continuous monitoring. It's possible to do this using onMutation, but it's not ideal (see example). We are working on a modification to make this easier, or to allow declarative monitoring of shadow-roots, light-dom, or possibly individual nodes.

Example of using onMutation for monitoring:

      watchMutation: function() {
        var change = function() {
          this.presentationChanged();
          this.onMutation(this, change);
        }.bind(this);
        this.onMutation(this, change);
      }

Scott


Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Seth Ladd

unread,
Oct 10, 2013, 2:24:37 PM10/10/13
to Scott Miles, polymer-dev
Ah ha, makes sense. And helpful.

Warning! Bikeshedding!  Perhaps a name that better conveys this is a one-time callback?

* onFirstMutation
* whenMutates

Thanks for the explanation,
Seth

Scott Miles

unread,
Oct 10, 2013, 2:29:08 PM10/10/13
to Seth Ladd, polymer-dev
I think there's universal agreement the name (`onMutation`) is not good.

So, thanks for the suggestions!

Scott

sepand...@gmail.com

unread,
Oct 22, 2014, 1:27:07 PM10/22/14
to polym...@googlegroups.com, seth...@google.com
I came across the same problem. In my case I want to run some code when a new element is dropped into a container element in the designer tool. Just wanted to check if there is a better way of doing this rather than using the 'not ideal' watchMutation function.

Thanks
Sepand
Reply all
Reply to author
Forward
0 new messages