MCPcopy Create free account
hub / github.com/WebReflection/neverland / startObserving

Function startObserving

index.js:162–235  ·  view source on GitHub ↗
(document)

Source from the content-addressed store, hash-verified

160 };
161
162 function startObserving(document) {
163 var connected = new WeakSet();
164 var disconnected = new WeakSet();
165
166 try {
167 new MutationObserver(changes).observe(document, {
168 subtree: true,
169 childList: true
170 });
171 } catch (o_O) {
172 var timer = 0;
173 var records = [];
174
175 var reschedule = function reschedule(record) {
176 records.push(record);
177 clearTimeout(timer);
178 timer = setTimeout(function () {
179 changes(records.splice(timer = 0, records.length));
180 }, 0);
181 };
182
183 document.addEventListener('DOMNodeRemoved', function (event) {
184 reschedule({
185 addedNodes: [],
186 removedNodes: [event.target]
187 });
188 }, true);
189 document.addEventListener('DOMNodeInserted', function (event) {
190 reschedule({
191 addedNodes: [event.target],
192 removedNodes: []
193 });
194 }, true);
195 }
196
197 function changes(records) {
198 for (var record, length = records.length, i = 0; i < length; i++) {
199 record = records[i];
200 dispatchAll(record.removedNodes, 'disconnected', disconnected, connected);
201 dispatchAll(record.addedNodes, 'connected', connected, disconnected);
202 }
203 }
204
205 function dispatchAll(nodes, type, wsin, wsout) {
206 for (var node, event = new Event(type), length = nodes.length, i = 0; i < length; (node = nodes[i++]).nodeType === 1 && dispatchTarget(node, event, type, wsin, wsout)) {
207 }
208 }
209
210 function dispatchTarget(node, event, type, wsin, wsout) {
211 if (observer.has(node) && !wsin.has(node)) {
212 wsout["delete"](node);
213 wsin.add(node);
214 node.dispatchEvent(event);
215 /*
216 // The event is not bubbling (perf reason: should it?),
217 // hence there's no way to know if
218 // stop/Immediate/Propagation() was called.
219 // Should DOM Level 0 work at all?

Callers 1

disconnectedFunction · 0.70

Calls 1

rescheduleFunction · 0.70

Tested by

no test coverage detected