Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 13e7a35

Browse files
authored
MutationObserver API (#139)
* MutationObserver API * Updated MutationObserverInit
1 parent a0729ea commit 13e7a35

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed

src/DOM/Node/MutationObserver.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
exports.mutationObserver = function (cb) {
4+
return function () {
5+
return new MutationObserver(function (mr, mo) {
6+
return cb(mr)(mo)();
7+
});
8+
};
9+
};
10+
11+
exports._observe = function (node) {
12+
return function (config) {
13+
return function (mo) {
14+
return function () {
15+
return mo.observe(node, config);
16+
};
17+
};
18+
};
19+
};
20+
21+
exports.disconnect = function (mo) {
22+
return function () {
23+
return mo.disconnect();
24+
};
25+
};
26+
27+
exports.takeRecords = function (mo) {
28+
return function () {
29+
return mo.takeRecords();
30+
};
31+
};

src/DOM/Node/MutationObserver.purs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module DOM.Node.MutationObserver
2+
( MutationObserver
3+
, MutationObserverInitFields
4+
, mutationObserver
5+
, observe
6+
, disconnect
7+
, takeRecords
8+
) where
9+
10+
import Prelude
11+
12+
import Control.Monad.Eff (Eff, kind Effect)
13+
import DOM (DOM)
14+
import DOM.Node.MutationRecord (MutationRecord)
15+
import DOM.Node.Types (Node)
16+
17+
foreign import data MutationObserver :: Type
18+
19+
type MutationObserverInitFields =
20+
( childList :: Boolean
21+
, attributes :: Boolean
22+
, characterData :: Boolean
23+
, subtree :: Boolean
24+
, attributeOldValue :: Boolean
25+
, characterDataOldValue :: Boolean
26+
, attributeFilter :: Array String
27+
)
28+
29+
foreign import mutationObserver
30+
:: forall eff
31+
. (MutationRecord -> MutationObserver -> Eff (dom :: DOM | eff) Unit)
32+
-> Eff (dom :: DOM | eff) MutationObserver
33+
34+
foreign import _observe :: forall eff r. Node -> Record r -> MutationObserver -> Eff (dom :: DOM | eff) Unit
35+
36+
observe
37+
:: forall eff r rx
38+
. Union r rx MutationObserverInitFields
39+
=> Node
40+
-> Record r
41+
-> MutationObserver
42+
-> Eff (dom :: DOM | eff) Unit
43+
observe = _observe
44+
45+
foreign import disconnect :: forall eff. MutationObserver -> Eff (dom :: DOM | eff) Unit
46+
47+
foreign import takeRecords :: forall eff. MutationObserver -> Eff (dom :: DOM | eff) (Array MutationRecord)

src/DOM/Node/MutationRecord.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"use strict";
2+
3+
exports.typeString = function (mr) {
4+
return function () {
5+
return mr.type;
6+
};
7+
};
8+
9+
exports.target = function (mr) {
10+
return function () {
11+
return mr.target;
12+
};
13+
};
14+
15+
exports.addedNodes = function (mr) {
16+
return function () {
17+
return mr.addedNodes;
18+
};
19+
};
20+
21+
exports.removedNodes = function (mr) {
22+
return function () {
23+
return mr.removedNodes;
24+
};
25+
};
26+
27+
exports._nextSibling = function (mr) {
28+
return function () {
29+
return mr.nextSibling;
30+
};
31+
};
32+
33+
exports._previousSibling = function (mr) {
34+
return function () {
35+
return mr.previousSibling;
36+
};
37+
};
38+
39+
exports._attributeName = function (mr) {
40+
return function () {
41+
return mr.attributeName;
42+
};
43+
};
44+
45+
exports._attributeNamespace = function (mr) {
46+
return function () {
47+
return mr.attributeNamespace;
48+
};
49+
};
50+
51+
exports._oldValue = function (mr) {
52+
return function () {
53+
return mr.oldValue;
54+
};
55+
};

src/DOM/Node/MutationRecord.purs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module DOM.Node.MutationRecord
2+
( MutationRecord
3+
, MutationRecordType(..)
4+
, typeString
5+
, type_
6+
, target
7+
, addedNodes
8+
, removedNodes
9+
, nextSibling
10+
, previousSibling
11+
, attributeName
12+
, attributeNamespace
13+
, oldValue
14+
) where
15+
16+
import Prelude
17+
18+
import Control.Monad.Eff (Eff)
19+
import DOM (DOM)
20+
import DOM.Node.Types (Node, NodeList)
21+
import Data.Maybe (Maybe)
22+
import Data.Nullable (Nullable, toMaybe)
23+
24+
foreign import data MutationRecord :: Type
25+
26+
data MutationRecordType
27+
= MutationRecordAttributes
28+
| MutationRecordCharacterData
29+
| MutationRecordChildList
30+
31+
foreign import typeString :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) String
32+
33+
type_ :: forall eff. Partial => MutationRecord -> Eff (dom :: DOM | eff) MutationRecordType
34+
type_ = map stringToType <<< typeString
35+
where
36+
stringToType = case _ of
37+
"attributes" -> MutationRecordAttributes
38+
"characterData" -> MutationRecordCharacterData
39+
"childList" -> MutationRecordChildList
40+
41+
foreign import target :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) Node
42+
43+
foreign import addedNodes :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) NodeList
44+
45+
foreign import removedNodes :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) NodeList
46+
47+
foreign import _nextSibling :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable Node)
48+
49+
nextSibling :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe Node)
50+
nextSibling = map toMaybe <<< _nextSibling
51+
52+
foreign import _previousSibling :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable Node)
53+
54+
previousSibling :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe Node)
55+
previousSibling = map toMaybe <<< _previousSibling
56+
57+
foreign import _attributeName :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable String)
58+
59+
attributeName :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe String)
60+
attributeName = map toMaybe <<< _attributeName
61+
62+
foreign import _attributeNamespace :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable String)
63+
64+
attributeNamespace :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe String)
65+
attributeNamespace = map toMaybe <<< _attributeNamespace
66+
67+
foreign import _oldValue :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable String)
68+
69+
oldValue :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe String)
70+
oldValue = map toMaybe <<< _oldValue

0 commit comments

Comments
 (0)