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 #505 from Polymer/observer_leak
Browse files Browse the repository at this point in the history
fixes #504, avoids global list of all MutationObservers
  • Loading branch information
John Messerly committed Sep 9, 2014
2 parents 565b978 + 7c8b908 commit 740d3d3
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/MutationObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
var globalMutationObservers = [];
var isScheduled = false;

function scheduleCallback(observer) {
function scheduleCallback() {
if (isScheduled)
return;
setEndOfMicrotask(notifyObservers);
Expand All @@ -26,21 +26,25 @@
function notifyObservers() {
isScheduled = false;

do {
var notifyList = globalMutationObservers.slice();
var anyNonEmpty = false;
while (globalMutationObservers.length) {
var notifyList = globalMutationObservers;
globalMutationObservers = [];

// Deliver changes in birth order of the MutationObservers.
notifyList.sort(function(x, y) { return x.uid_ - y.uid_; });

for (var i = 0; i < notifyList.length; i++) {
var mo = notifyList[i];
var queue = mo.takeRecords();
removeTransientObserversFor(mo);
if (queue.length) {
mo.callback_(queue, mo);
anyNonEmpty = true;
}
}
} while (anyNonEmpty);
}
}


/**
* @param {string} type
* @param {Node} target
Expand Down Expand Up @@ -146,7 +150,7 @@
}
}

var anyRecordsEnqueued = false;
var anyObserversEnqueued = false;

// 4.
for (var uid in interestedObservers) {
Expand Down Expand Up @@ -180,12 +184,14 @@
record.oldValue = associatedStrings[uid];

// 8.
if (!observer.records_.length) {
globalMutationObservers.push(observer);
anyObserversEnqueued = true;
}
observer.records_.push(record);

anyRecordsEnqueued = true;
}

if (anyRecordsEnqueued)
if (anyObserversEnqueued)
scheduleCallback();
}

Expand Down Expand Up @@ -249,9 +255,6 @@
this.nodes_ = [];
this.records_ = [];
this.uid_ = ++uidCounter;

// This will leak. There is no way to implement this without WeakRefs :'(
globalMutationObservers.push(this);
}

MutationObserver.prototype = {
Expand Down

0 comments on commit 740d3d3

Please sign in to comment.