[whatwg/dom] Simply MutationObserver callback handling with optional reference property in MutationObserverInit object (#539)

When registering observers on multiple different nodes using the same **_MutationObserver_** object it would be very useful to define some kind of custom reference (i.e. an id or label) as part of the **_MutationObserverInit_ options** object, https://dom.spec.whatwg.org/#dictdef-mutationobserverinit.

Currently, without this ability, the **_MutationObserver_**'s callback function needs to include tricky and unnecessary logic in order to determine which exact observer was responsible for the specific **_MutationRecord_**.

For example:
```
function onMutation(mutations) {
  for (var i = 0, x = mutations.length; i < x; i++) {
    switch (mutations[i].ref) {
      case 'A':
       // Do something without first having to include logic 
       // to determine which observer we're dealing with.
       break;
      case 'B':
        // Ditto
        break;
      case 'C':
        // Ditto
        break;
    }
  }
} 

var obs = new MutationObserver(onMutation);

var obsCfgA = { ref: 'A', attributes: true, attributeFilter: ['title', 'name'] };
var obsCfgB = { ref: 'B', attributes: true, childList: true, characterData: true };
var obsCfgC = { ref: 'C', subtree: true, childList: true };

obs.observe(nodeA, obsCfgA);
obs.observe(nodeB, obsCfgB);
obs.observe(nodeC, obsCfgC);

```

I'm 100% not sure this is the correct forum to submit such a suggestion, or if I have done so in the expected format, or indeed if what I'm suggesting is actually possible some other way. I'd certainly appreciate some pointers if I'm out of whack with anything.

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

Received on Tuesday, 21 November 2017 21:19:04 UTC