canjs / can-zone

A context for tracking asynchronous activity in JavaScript applications.
https://v4.canjs.com/doc/can-zone.html
MIT License
92 stars 4 forks source link

Implement MutationObservers #38

Closed matthewp closed 8 years ago

matthewp commented 8 years ago

This is how one Promise library does it:

function initMutationObserver(MutationObserver) {
        var scheduled;
        var node = document.createTextNode('');
        var o = new MutationObserver(run);
        o.observe(node, { characterData: true });

        function run() {
            var f = scheduled;
            scheduled = void 0;
            f();
        }

        var i = 0;
        return function (f) {
            scheduled = f;
            node.data = (i ^= 1);
        };
    }
matthewp commented 8 years ago

Important thing here is we want to only detect when MutationObserver is being used for this reason. If we say:

  1. Observing an empty TextNode
  2. That is not in the DOM

If that's safe enough to implement.

matthewp commented 8 years ago

Closed by #63