Skip to content

Commit 3cc17b0

Browse files
committed
Simplify MutationBasedRemovalTracker
Stateless! It #137-ready seamlessly.
1 parent d927d0e commit 3cc17b0

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

src/decorators/trackRemoval.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,40 @@ class MutationBasedRemovalTracker {
4747

4848
this.observer = null
4949
this.inited = false
50-
this.trackedElements = null
5150
}
5251

5352
init () {
5453
if (this.inited) {
5554
this.unbind()
5655
}
57-
58-
this.trackedElements = []
56+
this.inited = true
5957

6058
const MutationObserver = getMutationObserverClass()
61-
if (MutationObserver) {
62-
this.observer = new MutationObserver(() => {
63-
for (const element of this.trackedElements) {
64-
if (this.isDetached(element) && element === this.tooltip.state.currentTarget) {
59+
if (!MutationObserver) {
60+
return
61+
}
62+
63+
this.observer = new MutationObserver((mutations) => {
64+
for (const mutation of mutations) {
65+
for (const element of mutation.removedNodes) {
66+
if (element === this.tooltip.state.currentTarget) {
6567
this.tooltip.hideTooltip()
68+
return
6669
}
6770
}
68-
})
69-
this.observer.observe(window.document, { childList: true, subtree: true })
70-
}
71+
}
72+
})
7173

72-
this.inited = true
74+
this.observer.observe(window.document, { childList: true, subtree: true })
7375
}
7476

7577
unbind () {
7678
if (this.observer) {
7779
this.observer.disconnect()
7880
this.observer = null
79-
this.trackedElements = null
8081
}
8182
this.inited = false
8283
}
83-
84-
attach (element) {
85-
this.trackedElements.push(element)
86-
}
87-
88-
// http://stackoverflow.com/a/32726412/7571078
89-
isDetached (element) {
90-
if (element.parentNode === window.document) {
91-
return false
92-
}
93-
if (element.parentNode == null) return true
94-
return this.isDetached(element.parentNode)
95-
}
9684
}
9785

9886
export default function (target) {
@@ -106,7 +94,9 @@ export default function (target) {
10694
}
10795

10896
target.prototype.attachRemovalTracker = function (element) {
109-
this.removalTracker.attach(element)
97+
if (this.removalTracker.attach) {
98+
this.removalTracker.attach(element)
99+
}
110100
}
111101

112102
target.prototype.unbindRemovalTracker = function () {

0 commit comments

Comments
 (0)