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

Commit

Permalink
create a unique observer for each ShadowRoot or Document so detached …
Browse files Browse the repository at this point in the history
…nodes with shadow roots can be collected
  • Loading branch information
John Messerly committed Sep 23, 2014
1 parent 17e73cd commit 165c32b
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

1 comment on commit 165c32b

@sorvell
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. The takeRecords thing is a bummer, but seems like the price of doing business. We could create a version that manually walks the dom, but it doesn't seem worth it.

Please sign in to comment.