UNPKG

950 BJavaScriptView Raw
1"use strict";
2
3module.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 if (options.childList) {
23 node._emitter.on("_insert", onMutation);
24 }
25 if (options.attributes) {
26 node._emitter.on("_attributeChange", onAttributeChange);
27 }
28 }
29
30 function onMutation() {
31 return callback.call(observer, [MutationRecord("childList")]);
32 }
33
34 function onAttributeChange(attributeName, target) {
35 return callback.call(observer, [MutationRecord("attributes", {attributeName, target})]);
36 }
37
38 function MutationRecord(type, record = {}) {
39 return {type, ...record};
40 }
41};