1
-
2
1
import { assign } from './object' ;
3
2
import { isElement } from '../utils/dom' ;
4
3
@@ -26,7 +25,7 @@ export default function observeDOM(el, callback, opts) {
26
25
// Define a new observer
27
26
obs = new MutationObserver ( mutations => {
28
27
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.
30
29
// We break out of the loop early if any "significant" change has been detected
31
30
for ( let i = 0 ; i < mutations . length && ! changed ; i ++ ) {
32
31
// The muttion record
@@ -43,11 +42,14 @@ export default function observeDOM(el, callback, opts) {
43
42
// So we compare the old value to the new value
44
43
changed = true ;
45
44
} 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.
48
50
changed = true ;
49
51
} 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
51
53
changed = true ;
52
54
}
53
55
}
@@ -65,5 +67,7 @@ export default function observeDOM(el, callback, opts) {
65
67
el . addEventListener ( 'DOMNodeRemoved' , callback , false ) ;
66
68
}
67
69
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
68
72
return obs ;
69
73
}
0 commit comments