From d4d2ccdacd9e26430d043a812a4eb80fa9fc0f6e Mon Sep 17 00:00:00 2001 From: Luke Burns Date: Mon, 12 Mar 2018 23:06:11 -0400 Subject: [PATCH] load only if mounted --- index.js | 4 +++- test.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 1d61a21..d2598b9 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,9 @@ if (window && window.MutationObserver) { eachMutation(mutations[i].removedNodes, function (index, el) { if (!document.documentElement.contains(el)) turnoff(index, el) }) - eachMutation(mutations[i].addedNodes, turnon) + eachMutation(mutations[i].addedNodes, function (index, el) { + if (document.documentElement.contains(el)) turnon(index, el) + }) } }) if (document.body) { diff --git a/test.js b/test.js index 12c104e..9f9a8a2 100644 --- a/test.js +++ b/test.js @@ -8,13 +8,13 @@ test('onload/onunload', function (t) { el.textContent = 'test' onload(el, function () { t.ok(true, 'onload called') + document.body.removeChild(el) }, function () { t.ok(true, 'onunload called') document.body.innerHTML = '' t.end() }) document.body.appendChild(el) - document.body.removeChild(el) }) test('assign key attr', function (t) { @@ -66,13 +66,13 @@ test('nested', function (t) { var e3 = document.createElement('div') onload(e3, function () { t.ok(true, 'onload called') + e2.removeChild(e3) }, function () { t.ok(true, 'onunload called') document.body.innerHTML = '' t.end() }) e2.appendChild(e3) - e2.removeChild(e3) }) test('complex', function (t) {