Skip to content

Commit 458fdff

Browse files
committed
content improvements
1 parent 47d1865 commit 458fdff

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

2-ui/99-ui-misc/01-mutation-observer/article.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
`MutationObserver` is a built-in object that observes a DOM element and fires a callback in case of changes.
55

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.
77

88
## Syntax
99

@@ -65,7 +65,7 @@ observer.observe(elem, {
6565
</script>
6666
```
6767

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:
6969

7070
```js
7171
mutationRecords = [{
@@ -76,7 +76,7 @@ mutationRecords = [{
7676
}];
7777
```
7878

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:
8080

8181
```js
8282
mutationRecords = [{
@@ -101,15 +101,15 @@ So, `MutationObserver` allows to react on any changes within DOM subtree.
101101

102102
When such thing may be useful?
103103

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>`.
105105

106106
Naturally, the third-party script provides no mechanisms to remove it.
107107

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.
109109

110110
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.
111111

112-
`MutationObserver` can easily handle this.
112+
`MutationObserver` allows to implement this.
113113

114114
## Usage for architecture
115115

@@ -213,7 +213,7 @@ Please run the previous code (above, observes that element), and then the code b
213213

214214
<p id="highlight-demo" style="border: 1px solid #ddd">A demo-element with <code>id="highlight-demo"</code>, run the code above to observe it.</p>
215215

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:
217217

218218
```js run
219219
let demoElem = document.getElementById('highlight-demo');
@@ -236,22 +236,26 @@ There's a method to stop observing the node:
236236

237237
- `observer.disconnect()` -- stops the observation.
238238

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.
240240

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:
242244

243245
```js
244246
// we'd like to stop tracking changes
245247
observer.disconnect();
246248

247-
// it might have not yet handled some mutations
249+
// handle unprocessed some mutations
248250
let mutationRecords = observer.takeRecords();
249-
// process mutationRecords
251+
...
250252
```
251253

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.
253256
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.
258+
```
255259

256260
## Summary
257261

0 commit comments

Comments
 (0)