Skip to content

Commit ce21130

Browse files
committed
Add MutationObserverAPI
1 parent cf878fa commit ce21130

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed

src/MutationObserverAPI.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MutationObserverAPI.res

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
open DOMAPI
2+
3+
/**
4+
[See MutationObserver on MDN](https://developer.mozilla.org/docs/Web/API/MutationObserver)
5+
*/
6+
@editor.completeFrom(MutationObserver)
7+
type mutationObserver
8+
9+
type mutationObserverInit = {
10+
mutable childList?: bool,
11+
mutable attributes?: bool,
12+
mutable characterData?: bool,
13+
mutable subtree?: bool,
14+
mutable attributeOldValue?: bool,
15+
mutable characterDataOldValue?: bool,
16+
mutable attributeFilter?: array<string>,
17+
}
18+
19+
type mutationObserverCallback = (array<mutationRecord>, mutationObserver) => unit

src/MutationObserverAPI/MutationObserver.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
open DOMAPI
2+
open MutationObserverAPI
3+
4+
/**
5+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/MutationObserver)
6+
*/
7+
@new
8+
external make: mutationObserverCallback => mutationObserver = "MutationObserver"
9+
10+
/**
11+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/MutationObserver/observe)
12+
*/
13+
@send
14+
external observe: (mutationObserver, ~target: node, ~options: mutationObserverInit=?) => unit =
15+
"observe"
16+
17+
/**
18+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/MutationObserver/disconnect)
19+
*/
20+
@send
21+
external disconnect: mutationObserver => unit = "disconnect"
22+
23+
/**
24+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/MutationObserver/takeRecords)
25+
*/
26+
@send
27+
external takeRecords: mutationObserver => array<mutationRecord> = "takeRecords"

tests/MutationObserverAPI/MutationObserver__test.js

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let observer = MutationObserver.make((mutations, obs) => {
2+
let button = Global.document->Document.querySelector("button")
3+
switch button->Null.toOption {
4+
| Some(button) => {
5+
Console.log(button)
6+
obs->MutationObserver.disconnect
7+
}
8+
| None => ()
9+
}
10+
Console.log(mutations)
11+
})
12+
13+
observer->MutationObserver.observe(
14+
~target=Global.document->Document.asNode,
15+
~options={childList: true, subtree: true},
16+
)
17+
18+
let records = observer->MutationObserver.takeRecords
19+
Console.log(records->Array.length)
20+
21+
observer->MutationObserver.disconnect

0 commit comments

Comments
 (0)