Skip to content
This repository was archived by the owner on Sep 20, 2019. It is now read-only.

Commit 8d83a2d

Browse files
committed
Merge pull request #417 from webcomponents/fix-416
Fixes #416
2 parents 1a28425 + 22df81f commit 8d83a2d

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/CustomElements/observe.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,23 @@ function addedSubtree(node, isAttached) {
5353
}
5454

5555
// On platforms without MutationObserver, mutations may not be
56-
// reliable and therefore attached/detached are not reliable.
57-
// To make these callbacks less likely to fail, we defer all inserts and removes
56+
// reliable and therefore attached/detached are not reliable. We think this
57+
// occurs sometimes under heavy DOM operation load, but it is not easy to
58+
// reproduce.
59+
// To make these callbacks less likely to fail in this scenario,
60+
// we *optionally* defer all inserts and removes
5861
// to give a chance for elements to be attached into dom.
59-
// This ensures attachedCallback fires for elements that are created and
62+
// This helps ensure attachedCallback fires for elements that are created and
6063
// immediately added to dom.
61-
var hasPolyfillMutations = (!window.MutationObserver ||
62-
(window.MutationObserver === window.JsMutationObserver));
63-
scope.hasPolyfillMutations = hasPolyfillMutations;
64+
// This change can significantly alter the performance characteristics
65+
// of attaching elements and therefore we only enable it if the user has
66+
// explicitly provided the `throttle-attached` flag.
67+
var hasThrottledAttached = (window.MutationObserver._isPolyfilled &&
68+
flags['throttle-attached']);
69+
// bc
70+
scope.hasPolyfillMutations = hasThrottledAttached;
71+
// exposed for testing
72+
scope.hasThrottledAttached = hasThrottledAttached;
6473

6574
var isPendingMutations = false;
6675
var pendingMutations = [];
@@ -82,7 +91,7 @@ function takeMutations() {
8291
}
8392

8493
function attached(element) {
85-
if (hasPolyfillMutations) {
94+
if (hasThrottledAttached) {
8695
deferMutation(function() {
8796
_attached(element);
8897
});
@@ -118,7 +127,7 @@ function detachedNode(node) {
118127
}
119128

120129
function detached(element) {
121-
if (hasPolyfillMutations) {
130+
if (hasThrottledAttached) {
122131
deferMutation(function() {
123132
_detached(element);
124133
});

src/MutationObserver/MutationObserver.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
(function(global) {
1212

13+
// Don't allow this object to be redefined.
14+
if (global.JsMutationObserver) {
15+
return;
16+
}
17+
1318
var registrationsTable = new WeakMap();
1419

1520
var setImmediate;
@@ -561,8 +566,10 @@
561566

562567
global.JsMutationObserver = JsMutationObserver;
563568

564-
if (!global.MutationObserver)
569+
if (!global.MutationObserver) {
565570
global.MutationObserver = JsMutationObserver;
566-
571+
// Explicltly mark MO as polyfilled for user reference.
572+
JsMutationObserver._isPolyfilled = true;
573+
}
567574

568575
})(self);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
5+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8+
Code distributed by Google as part of the polymer project is also
9+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10+
-->
11+
<html>
12+
<head>
13+
<title>Custom Elements: throttle attached</title>
14+
<script src="../../tools/htmltest.js"></script>
15+
<script src="../../tools/chai/chai.js"></script>
16+
<script src="../../../src/CustomElements/CustomElements.js?wc-throttle-attached"></script>
17+
</head>
18+
<body>
19+
<script>
20+
addEventListener('WebComponentsReady', function() {
21+
chai.assert.equal(MutationObserver.__isPolyfilled, CustomElements.hasThrottledAttached);
22+
done();
23+
});
24+
</script>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)