1 | "use strict";
|
2 |
|
3 | module.exports = function MutationObserver(callback) {
|
4 | let node;
|
5 |
|
6 | const observer = {
|
7 | disconnect,
|
8 | observe
|
9 | };
|
10 |
|
11 | return observer;
|
12 |
|
13 | function disconnect() {
|
14 | if (!node) return;
|
15 | node._emitter.removeListener("_insert", onMutation);
|
16 | node._emitter.removeListener("_attributeChange", onAttributeChange);
|
17 | node = undefined;
|
18 | }
|
19 |
|
20 | function observe(targetNode, options) {
|
21 | node = targetNode;
|
22 | node._emitter.on("_insert", onMutation);
|
23 | if (options.attributes) {
|
24 | node._emitter.on("_attributeChange", onAttributeChange);
|
25 | }
|
26 | }
|
27 |
|
28 | function onMutation() {
|
29 | return callback.call(observer, [MutationRecord("childList")]);
|
30 | }
|
31 |
|
32 | function onAttributeChange(attributeName, target) {
|
33 | return callback.call(observer, [MutationRecord("attributes", {attributeName, target})]);
|
34 | }
|
35 |
|
36 | function MutationRecord(type, record = {}) {
|
37 | return {type, ...record};
|
38 | }
|
39 | };
|