You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/99-ui-misc/01-mutation-observer/article.md
+17-13Lines changed: 17 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
4
4
`MutationObserver` is a built-in object that observes a DOM element and fires a callback in case of changes.
5
5
6
-
We'll first take a look at the syntax, and then explore a real-world use case.
6
+
We'll first take a look at the syntax, and then explore a real-world use case, to see where such thing may be useful.
7
7
8
8
## Syntax
9
9
@@ -65,7 +65,7 @@ observer.observe(elem, {
65
65
</script>
66
66
```
67
67
68
-
Now if we change the text inside `<b>edit</b>`, we'll get a single mutation:
68
+
If we run this code in the browser, then focus on the given `<div>` and change the text inside `<b>edit</b>`, `console.log` will show one mutation:
69
69
70
70
```js
71
71
mutationRecords = [{
@@ -76,7 +76,7 @@ mutationRecords = [{
76
76
}];
77
77
```
78
78
79
-
If we select and remove the `<b>edit</b>` altogether, we'll get multiple mutations:
79
+
If we make more complex editing operations, e.g. remove the `<b>edit</b>`, the mutation event may contain multiple mutation records:
80
80
81
81
```js
82
82
mutationRecords = [{
@@ -101,15 +101,15 @@ So, `MutationObserver` allows to react on any changes within DOM subtree.
101
101
102
102
When such thing may be useful?
103
103
104
-
Imagine the situation when you attach a third-party script that adds useful functionality on the page, but also does something unwanted, e.g. shows ads `<div class="ads">Unwanted ads</div>`.
104
+
Imagine the situation when you need to add a third-party script that contains useful functionality, but also does something unwanted, e.g. shows ads `<div class="ads">Unwanted ads</div>`.
105
105
106
106
Naturally, the third-party script provides no mechanisms to remove it.
107
107
108
-
Using `MutationObserver`, we can detect when such element appears in our DOM and remove it. While leaving the useful functionality intact. Surely though, creators of that script won't be happy that you took their useful stuff and removed the ads.
108
+
Using `MutationObserver`, we can detect when the unwanted element appears in our DOM and remove it.
109
109
110
110
There are other situations when a third-party script adds something into our document, and we'd like to detect, when it happens, to adapt our page, dynamically resize something etc.
111
111
112
-
`MutationObserver`can easily handle this.
112
+
`MutationObserver`allows to implement this.
113
113
114
114
## Usage for architecture
115
115
@@ -213,7 +213,7 @@ Please run the previous code (above, observes that element), and then the code b
213
213
214
214
<pid="highlight-demo"style="border: 1pxsolid#ddd">A demo-element with <code>id="highlight-demo"</code>, run the code above to observe it.</p>
215
215
216
-
The following code populates its `innerHTML`. Please run the code above first, it will watch and highlight the new content:
216
+
The following code populates its `innerHTML`, that causes the `MutationObserver` to react and highlight its contents:
217
217
218
218
```js run
219
219
let demoElem =document.getElementById('highlight-demo');
@@ -236,22 +236,26 @@ There's a method to stop observing the node:
236
236
237
237
-`observer.disconnect()` -- stops the observation.
238
238
239
-
Another method often used with it:
239
+
When we stop the observing, it might be possible that some changes were not processed by the observer yet.
240
240
241
-
-`mutationRecords = observer.takeRecords()` -- gets a list of unprocessed mutation records, those that happened, but the callback did not handle them.
241
+
-`observer.takeRecords()` -- gets a list of unprocessed mutation records, those that happened, but the callback did not handle them.
242
+
243
+
These methods can be used together, like this:
242
244
243
245
```js
244
246
// we'd like to stop tracking changes
245
247
observer.disconnect();
246
248
247
-
//it might have not yet handled some mutations
249
+
//handle unprocessed some mutations
248
250
let mutationRecords =observer.takeRecords();
249
-
// process mutationRecords
251
+
...
250
252
```
251
253
252
-
## Garbage collection
254
+
```smart header="Garbage collection interaction"
255
+
Observers use weak references to nodes internally. That is: if a node is removed from DOM, and becomes unreachable, then it becomes garbage collected.
253
256
254
-
Observers use weak references to nodes internally. That is: if a node is removed from DOM, and becomes unreachable, then it becomes garbage collected, an observer doesn't prevent that.
257
+
The mere fact that a DOM node is observed doesn't prevent the garbage collection.
0 commit comments