From 16a6b373d1921cad80db1fab975ee1435b575a0c Mon Sep 17 00:00:00 2001 From: Steve Orvell Date: Mon, 16 Sep 2013 17:29:42 -0700 Subject: [PATCH] Defer entered/leftView on platforms without MutationObserver (IE). The polyfill for MO is flakey and since entered/leftView rely on it, they are too. To make these callbacks less flakey, we use a defer strategy to make sure the callbacks fire in the common case where an element is created and immediately inserted into dom. --- src/Observer.js | 61 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/src/Observer.js b/src/Observer.js index e0b7676..05f0f59 100644 --- a/src/Observer.js +++ b/src/Observer.js @@ -30,7 +30,7 @@ function findAll(node, find, data) { // walk all shadowRoots on a given node. function forRoots(node, cb) { - var root = node.webkitShadowRoot; + var root = node.shadowRoot; while(root) { forSubtree(root, cb); root = root.olderShadowRoot; @@ -97,9 +97,50 @@ function insertedNode(node) { } } -// TODO(sjmiles): if there are descents into trees that can never have inDocument(*) true, fix this + +// TODO(sorvell): on platforms without MutationObserver, mutations may not be +// reliable and therefore entered/leftView are not reliable. +// To make these callbacks less likely to fail, we defer all inserts and removes +// to give a chance for elements to be inserted into dom. +// This ensures enteredViewCallback fires for elements that are created and +// immediately added to dom. +var hasPolyfillMutations = (!window.MutationObserver || + (window.MutationObserver === window.JsMutationObserver)); +scope.hasPolyfillMutations = hasPolyfillMutations; + +var isPendingMutations = false; +var pendingMutations = []; +function deferMutation(fn) { + pendingMutations.push(fn); + if (!isPendingMutations) { + isPendingMutations = true; + var async = (window.Platform && window.Platform.endOfMicrotask) || + setTimeout; + async(takeMutations); + } +} + +function takeMutations() { + isPendingMutations = false; + var $p = pendingMutations; + for (var i=0, l=$p.length, p; (i