Skip to content

Commit 8a630c4

Browse files
authored
Update observe-dom.js
1 parent 4709ec0 commit 8a630c4

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/utils/observe-dom.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { assign } from './object';
32
import { isElement } from '../utils/dom';
43

@@ -26,7 +25,7 @@ export default function observeDOM(el, callback, opts) {
2625
// Define a new observer
2726
obs = new MutationObserver(mutations => {
2827
let changed = false;
29-
// A Mutation can contain several changes, so we loop through them to see what has changed
28+
// A Mutation can contain several change records, so we loop through them to see what has changed.
3029
// We break out of the loop early if any "significant" change has been detected
3130
for (let i = 0; i < mutations.length && !changed; i++) {
3231
// The muttion record
@@ -43,11 +42,14 @@ export default function observeDOM(el, callback, opts) {
4342
// So we compare the old value to the new value
4443
changed = true;
4544
} else if (type === 'attributes' && target.getAttribute(mutation.attributeName) !== old) {
46-
// Updating an attribute with the same value still triggers a mutation
47-
// So we compare the old value to the new value
45+
// Updating an attribute with the same value still triggers a mutation, so we compare
46+
// the old value to the new value. For atributes like class or style, the content order
47+
// could change (i.e. classes changed order in string), but have no affect on layout.
48+
// We could pre-process these here but that added overhead might be more than the
49+
// callback's overhead. So we just run callback anyways.
4850
changed = true;
4951
} else if (type === 'childList' && (mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0)) {
50-
// This includes HTMLElement and Text Nodes being added/removed
52+
// This includes HTMLElement and Text Nodes being added/removed/re-arranged
5153
changed = true;
5254
}
5355
}
@@ -65,5 +67,7 @@ export default function observeDOM(el, callback, opts) {
6567
el.addEventListener('DOMNodeRemoved', callback, false);
6668
}
6769

70+
// We return a reference to the observer so that obs.disconnect() can be called if necessary
71+
// To reduce overhead when the root element is hiiden
6872
return obs;
6973
}

0 commit comments

Comments
 (0)