Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #131 from Polymer/fix_observer_leak
Browse files Browse the repository at this point in the history
create a unique observer for each tree so detached nodes can be GC'd
  • Loading branch information
John Messerly committed Sep 24, 2014
2 parents 17e73cd + 165c32b commit 1f5e073
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/Observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ function watchShadow(node) {
}

function watchRoot(root) {
if (!root.__watched) {
observe(root);
root.__watched = true;
}
observe(root);
}

function handler(mutations) {
Expand Down Expand Up @@ -294,18 +291,32 @@ function handler(mutations) {
logFlags.dom && console.groupEnd();
};

var observer = new MutationObserver(handler);
function takeRecords(node) {
// If the optional node is not supplied, assume we mean the whole document.
if (node === undefined) node = document;

// Find the root of the tree, which will be an Document or ShadowRoot.
while (node.parentNode) {
node = node.parentNode;
}

function takeRecords() {
// TODO(sjmiles): ask Raf why we have to call handler ourselves
handler(observer.takeRecords());
takeMutations();
var observer = node.__observer;
if (observer) {
handler(observer.takeRecords());
takeMutations();
}
}

var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach);

function observe(inRoot) {
if (inRoot.__observer) return;

// For each ShadowRoot, we create a new MutationObserver, so the root can be
// garbage collected once all references to the `inRoot` node are gone.
var observer = new MutationObserver(handler);
observer.observe(inRoot, {childList: true, subtree: true});
inRoot.__observer = observer;
}

function observeDocument(doc) {
Expand Down

0 comments on commit 1f5e073

Please sign in to comment.