@antv/g-dom-mutation-observer-api
Advanced tools
Comparing version 1.0.38 to 1.1.0-alpha.1
export * from './MutationObserver'; | ||
export * from './MutationRecord'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -36,2 +36,1 @@ import type { DisplayObject, IElement } from '@antv/g-lite'; | ||
} | ||
//# sourceMappingURL=MutationObserver.d.ts.map |
@@ -15,2 +15,1 @@ import type { IElement } from '@antv/g-lite'; | ||
} | ||
//# sourceMappingURL=MutationRecord.d.ts.map |
export * from './dom'; | ||
//# sourceMappingURL=index.d.ts.map |
import { ElementEvent, MutationEvent, runtime } from '@antv/g-lite'; | ||
var MutationRecord = /*#__PURE__*/function () { | ||
MutationRecord.copy = function copy(original) { | ||
var record = new MutationRecord(original.type, original.target); | ||
record.addedNodes = original.addedNodes.slice(); | ||
record.removedNodes = original.removedNodes.slice(); | ||
record.previousSibling = original.previousSibling; | ||
record.nextSibling = original.nextSibling; | ||
record.attributeName = original.attributeName; | ||
record.attributeNamespace = original.attributeNamespace; | ||
record.oldValue = original.oldValue; | ||
return record; | ||
}; | ||
function MutationRecord(type, target) { | ||
this.type = void 0; | ||
this.target = void 0; | ||
this.addedNodes = []; | ||
this.attributeName = null; | ||
this.attributeNamespace = null; | ||
this.nextSibling = null; | ||
this.oldValue = null; | ||
this.previousSibling = null; | ||
this.removedNodes = []; | ||
this.type = type; | ||
this.target = target; | ||
} | ||
return MutationRecord; | ||
}(); | ||
var MutationRecord = /** @class */ (function () { | ||
function MutationRecord(type, target) { | ||
this.type = type; | ||
this.target = target; | ||
this.addedNodes = []; | ||
this.attributeName = null; | ||
this.attributeNamespace = null; | ||
this.nextSibling = null; | ||
this.oldValue = null; | ||
this.previousSibling = null; | ||
this.removedNodes = []; | ||
} | ||
MutationRecord.copy = function (original) { | ||
var record = new MutationRecord(original.type, original.target); | ||
record.addedNodes = original.addedNodes.slice(); | ||
record.removedNodes = original.removedNodes.slice(); | ||
record.previousSibling = original.previousSibling; | ||
record.nextSibling = original.nextSibling; | ||
record.attributeName = original.attributeName; | ||
record.attributeNamespace = original.attributeNamespace; | ||
record.oldValue = original.oldValue; | ||
return record; | ||
}; | ||
return MutationRecord; | ||
}()); | ||
var uidCounter = 0; | ||
var registrationsTable = new WeakMap(); | ||
var Registration = /*#__PURE__*/function () { | ||
function Registration(observer, target, options) { | ||
this.observer = void 0; | ||
this.target = void 0; | ||
this.options = void 0; | ||
this.transientObservedNodes = []; | ||
this.observer = observer; | ||
this.target = target; | ||
this.options = options; | ||
} | ||
var _proto = Registration.prototype; | ||
_proto.enqueue = function enqueue(record) { | ||
var records = this.observer.records; | ||
var length = records.length; | ||
// There are cases where we replace the last record with the new record. | ||
// For example if the record represents the same mutation we need to use | ||
// the one with the oldValue. If we get same record (this can happen as we | ||
// walk up the tree) we ignore the new record. | ||
if (records.length > 0) { | ||
var lastRecord = records[length - 1]; | ||
var recordToReplaceLast = selectRecord(lastRecord, record); | ||
if (recordToReplaceLast) { | ||
records[length - 1] = recordToReplaceLast; | ||
return; | ||
} | ||
} else { | ||
scheduleCallback(this.observer); | ||
var Registration = /** @class */ (function () { | ||
function Registration(observer, target, options) { | ||
this.observer = observer; | ||
this.target = target; | ||
this.options = options; | ||
this.transientObservedNodes = []; | ||
} | ||
records[length] = record; | ||
}; | ||
_proto.addListeners = function addListeners() { | ||
this.addListeners_(this.target); | ||
}; | ||
_proto.addListeners_ = function addListeners_(node) { | ||
var options = this.options; | ||
if (options.attributes) node.addEventListener(ElementEvent.ATTR_MODIFIED, this, true); | ||
// if (options.characterData) node.addEventListener('DOMCharacterDataModified', this, true); | ||
if (options.childList) node.addEventListener(ElementEvent.INSERTED, this, true); | ||
if (options.childList || options.subtree) node.addEventListener(ElementEvent.REMOVED, this, true); | ||
}; | ||
_proto.removeListeners = function removeListeners() { | ||
this.removeListeners_(this.target); | ||
}; | ||
_proto.removeListeners_ = function removeListeners_(node) { | ||
var options = this.options; | ||
if (options.attributes) node.removeEventListener(ElementEvent.ATTR_MODIFIED, this, true); | ||
// if (options.characterData) node.removeEventListener('DOMCharacterDataModified', this, true); | ||
if (options.childList) node.removeEventListener(ElementEvent.INSERTED, this, true); | ||
if (options.childList || options.subtree) node.removeEventListener(ElementEvent.REMOVED, this, true); | ||
} | ||
/** | ||
* Adds a transient observer on node. The transient observer gets removed | ||
* next time we deliver the change records. | ||
*/ | ||
// addTransientObserver(node: IElement) { | ||
// // Don't add transient observers on the target itself. We already have all | ||
// // the required listeners set up on the target. | ||
// if (node === this.target) return; | ||
// this.addListeners_(node); | ||
// this.transientObservedNodes.push(node); | ||
// let registrations = registrationsTable.get(node); | ||
// if (!registrations) registrationsTable.set(node, (registrations = [])); | ||
// // We know that registrations does not contain this because we already | ||
// // checked if node === this.target. | ||
// registrations.push(this); | ||
// } | ||
; | ||
_proto.removeTransientObservers = function removeTransientObservers() { | ||
var transientObservedNodes = this.transientObservedNodes; | ||
this.transientObservedNodes = []; | ||
transientObservedNodes.forEach(function (node) { | ||
// Transient observers are never added to the target. | ||
this.removeListeners_(node); | ||
var registrations = registrationsTable.get(node); | ||
for (var i = 0; i < registrations.length; i++) { | ||
if (registrations[i] === this) { | ||
registrations.splice(i, 1); | ||
// Each node can only have one registered observer associated with | ||
// this observer. | ||
break; | ||
Registration.prototype.enqueue = function (record) { | ||
var records = this.observer.records; | ||
var length = records.length; | ||
// There are cases where we replace the last record with the new record. | ||
// For example if the record represents the same mutation we need to use | ||
// the one with the oldValue. If we get same record (this can happen as we | ||
// walk up the tree) we ignore the new record. | ||
if (records.length > 0) { | ||
var lastRecord = records[length - 1]; | ||
var recordToReplaceLast = selectRecord(lastRecord, record); | ||
if (recordToReplaceLast) { | ||
records[length - 1] = recordToReplaceLast; | ||
return; | ||
} | ||
} | ||
} | ||
}, this); | ||
}; | ||
_proto.handleEvent = function handleEvent(e) { | ||
// Stop propagation since we are managing the propagation manually. | ||
// This means that other mutation events on the page will not work | ||
// correctly but that is by design. | ||
e.stopImmediatePropagation(); | ||
var record; | ||
var target; | ||
switch (e.type) { | ||
case ElementEvent.ATTR_MODIFIED: | ||
// http://dom.spec.whatwg.org/#concept-mo-queue-attributes | ||
var name = e.attrName; | ||
// @ts-ignore | ||
var namespace = e.relatedNode.namespaceURI; | ||
target = e.target; | ||
// 1. | ||
record = getRecord('attributes', target); | ||
record.attributeName = name; | ||
record.attributeNamespace = namespace; | ||
// 2. | ||
var oldValue = e.attrChange === MutationEvent.ADDITION ? null : e.prevValue; | ||
forEachAncestorAndObserverEnqueueRecord(target, function (options) { | ||
// 3.1, 4.2 | ||
if (!options.attributes) return; | ||
// 3.2, 4.3 | ||
if (options.attributeFilter && options.attributeFilter.length && options.attributeFilter.indexOf(name) === -1 && options.attributeFilter.indexOf(namespace) === -1) { | ||
return; | ||
} | ||
// 3.3, 4.4 | ||
if (options.attributeOldValue) return getRecordWithOldValue(oldValue); | ||
// 3.4, 4.5 | ||
return record; | ||
}); | ||
break; | ||
// case 'DOMCharacterDataModified': | ||
// // http://dom.spec.whatwg.org/#concept-mo-queue-characterdata | ||
// var target = e.target; | ||
// // 1. | ||
// var record = getRecord('characterData', target); | ||
// // 2. | ||
// var oldValue = e.prevValue; | ||
// forEachAncestorAndObserverEnqueueRecord(target, function(options) { | ||
// // 3.1, 4.2 | ||
// if (!options.characterData) | ||
// return; | ||
// // 3.2, 4.3 | ||
// if (options.characterDataOldValue) | ||
// return getRecordWithOldValue(oldValue); | ||
// // 3.3, 4.4 | ||
// return record; | ||
// }); | ||
// break; | ||
case ElementEvent.REMOVED: | ||
// this.addTransientObserver(e.target as IElement); | ||
// Fall through. | ||
case ElementEvent.INSERTED: | ||
// http://dom.spec.whatwg.org/#concept-mo-queue-childlist | ||
target = e.relatedNode; | ||
var changedNode = e.target; | ||
var addedNodes; | ||
var removedNodes; | ||
if (e.type === ElementEvent.INSERTED) { | ||
addedNodes = [changedNode]; | ||
removedNodes = []; | ||
} else { | ||
addedNodes = []; | ||
removedNodes = [changedNode]; | ||
else { | ||
scheduleCallback(this.observer); | ||
} | ||
var previousSibling = changedNode.previousSibling; | ||
var nextSibling = changedNode.nextSibling; | ||
// 1. | ||
record = getRecord('childList', target); | ||
record.addedNodes = addedNodes; | ||
record.removedNodes = removedNodes; | ||
record.previousSibling = previousSibling; | ||
record.nextSibling = nextSibling; | ||
forEachAncestorAndObserverEnqueueRecord(target, function (options) { | ||
// 2.1, 3.2 | ||
if (!options.childList) return; | ||
// 2.2, 3.3 | ||
return record; | ||
}); | ||
} | ||
clearRecords(); | ||
}; | ||
return Registration; | ||
}(); | ||
records[length] = record; | ||
}; | ||
Registration.prototype.addListeners = function () { | ||
this.addListeners_(this.target); | ||
}; | ||
Registration.prototype.addListeners_ = function (node) { | ||
var options = this.options; | ||
if (options.attributes) | ||
node.addEventListener(ElementEvent.ATTR_MODIFIED, this, true); | ||
// if (options.characterData) node.addEventListener('DOMCharacterDataModified', this, true); | ||
if (options.childList) | ||
node.addEventListener(ElementEvent.INSERTED, this, true); | ||
if (options.childList || options.subtree) | ||
node.addEventListener(ElementEvent.REMOVED, this, true); | ||
}; | ||
Registration.prototype.removeListeners = function () { | ||
this.removeListeners_(this.target); | ||
}; | ||
Registration.prototype.removeListeners_ = function (node) { | ||
var options = this.options; | ||
if (options.attributes) | ||
node.removeEventListener(ElementEvent.ATTR_MODIFIED, this, true); | ||
// if (options.characterData) node.removeEventListener('DOMCharacterDataModified', this, true); | ||
if (options.childList) | ||
node.removeEventListener(ElementEvent.INSERTED, this, true); | ||
if (options.childList || options.subtree) | ||
node.removeEventListener(ElementEvent.REMOVED, this, true); | ||
}; | ||
/** | ||
* Adds a transient observer on node. The transient observer gets removed | ||
* next time we deliver the change records. | ||
*/ | ||
// addTransientObserver(node: IElement) { | ||
// // Don't add transient observers on the target itself. We already have all | ||
// // the required listeners set up on the target. | ||
// if (node === this.target) return; | ||
// this.addListeners_(node); | ||
// this.transientObservedNodes.push(node); | ||
// let registrations = registrationsTable.get(node); | ||
// if (!registrations) registrationsTable.set(node, (registrations = [])); | ||
// // We know that registrations does not contain this because we already | ||
// // checked if node === this.target. | ||
// registrations.push(this); | ||
// } | ||
Registration.prototype.removeTransientObservers = function () { | ||
var transientObservedNodes = this.transientObservedNodes; | ||
this.transientObservedNodes = []; | ||
transientObservedNodes.forEach(function (node) { | ||
// Transient observers are never added to the target. | ||
this.removeListeners_(node); | ||
var registrations = registrationsTable.get(node); | ||
for (var i = 0; i < registrations.length; i++) { | ||
if (registrations[i] === this) { | ||
registrations.splice(i, 1); | ||
// Each node can only have one registered observer associated with | ||
// this observer. | ||
break; | ||
} | ||
} | ||
}, this); | ||
}; | ||
Registration.prototype.handleEvent = function (e) { | ||
// Stop propagation since we are managing the propagation manually. | ||
// This means that other mutation events on the page will not work | ||
// correctly but that is by design. | ||
e.stopImmediatePropagation(); | ||
var record; | ||
var target; | ||
switch (e.type) { | ||
case ElementEvent.ATTR_MODIFIED: | ||
// http://dom.spec.whatwg.org/#concept-mo-queue-attributes | ||
var name_1 = e.attrName; | ||
// @ts-ignore | ||
var namespace_1 = e.relatedNode.namespaceURI; | ||
target = e.target; | ||
// 1. | ||
record = getRecord('attributes', target); | ||
record.attributeName = name_1; | ||
record.attributeNamespace = namespace_1; | ||
// 2. | ||
var oldValue_1 = e.attrChange === MutationEvent.ADDITION ? null : e.prevValue; | ||
forEachAncestorAndObserverEnqueueRecord(target, function (options) { | ||
// 3.1, 4.2 | ||
if (!options.attributes) | ||
return; | ||
// 3.2, 4.3 | ||
if (options.attributeFilter && | ||
options.attributeFilter.length && | ||
options.attributeFilter.indexOf(name_1) === -1 && | ||
options.attributeFilter.indexOf(namespace_1) === -1) { | ||
return; | ||
} | ||
// 3.3, 4.4 | ||
if (options.attributeOldValue) | ||
return getRecordWithOldValue(oldValue_1); | ||
// 3.4, 4.5 | ||
return record; | ||
}); | ||
break; | ||
// case 'DOMCharacterDataModified': | ||
// // http://dom.spec.whatwg.org/#concept-mo-queue-characterdata | ||
// var target = e.target; | ||
// // 1. | ||
// var record = getRecord('characterData', target); | ||
// // 2. | ||
// var oldValue = e.prevValue; | ||
// forEachAncestorAndObserverEnqueueRecord(target, function(options) { | ||
// // 3.1, 4.2 | ||
// if (!options.characterData) | ||
// return; | ||
// // 3.2, 4.3 | ||
// if (options.characterDataOldValue) | ||
// return getRecordWithOldValue(oldValue); | ||
// // 3.3, 4.4 | ||
// return record; | ||
// }); | ||
// break; | ||
case ElementEvent.REMOVED: | ||
// this.addTransientObserver(e.target as IElement); | ||
// Fall through. | ||
case ElementEvent.INSERTED: | ||
// http://dom.spec.whatwg.org/#concept-mo-queue-childlist | ||
target = e.relatedNode; | ||
var changedNode = e.target; | ||
var addedNodes = void 0; | ||
var removedNodes = void 0; | ||
if (e.type === ElementEvent.INSERTED) { | ||
addedNodes = [changedNode]; | ||
removedNodes = []; | ||
} | ||
else { | ||
addedNodes = []; | ||
removedNodes = [changedNode]; | ||
} | ||
var previousSibling = changedNode.previousSibling; | ||
var nextSibling = changedNode.nextSibling; | ||
// 1. | ||
record = getRecord('childList', target); | ||
record.addedNodes = addedNodes; | ||
record.removedNodes = removedNodes; | ||
record.previousSibling = previousSibling; | ||
record.nextSibling = nextSibling; | ||
forEachAncestorAndObserverEnqueueRecord(target, function (options) { | ||
// 2.1, 3.2 | ||
if (!options.childList) | ||
return; | ||
// 2.2, 3.3 | ||
return record; | ||
}); | ||
} | ||
clearRecords(); | ||
}; | ||
return Registration; | ||
}()); | ||
/** | ||
@@ -207,73 +214,74 @@ * @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver | ||
*/ | ||
var MutationObserver = /*#__PURE__*/function () { | ||
function MutationObserver(callback) { | ||
this.callback = void 0; | ||
this.nodes = []; | ||
this.records = []; | ||
this.uid = uidCounter++; | ||
this.callback = callback; | ||
} | ||
var _proto2 = MutationObserver.prototype; | ||
_proto2.observe = function observe(target, options) { | ||
// 1.1 | ||
if (!options.childList && !options.attributes && !options.characterData || | ||
// 1.2 | ||
options.attributeOldValue && !options.attributes || | ||
// 1.3 | ||
options.attributeFilter && options.attributeFilter.length && !options.attributes || | ||
// 1.4 | ||
options.characterDataOldValue && !options.characterData) { | ||
throw new SyntaxError(); | ||
var MutationObserver = /** @class */ (function () { | ||
function MutationObserver(callback) { | ||
this.callback = callback; | ||
this.nodes = []; | ||
this.records = []; | ||
this.uid = uidCounter++; | ||
} | ||
var registrations = registrationsTable.get(target); | ||
if (!registrations) registrationsTable.set(target, registrations = []); | ||
// 2 | ||
// If target's list of registered observers already includes a registered | ||
// observer associated with the context object, replace that registered | ||
// observer's options with options. | ||
var registration; | ||
for (var i = 0; i < registrations.length; i++) { | ||
if (registrations[i].observer === this) { | ||
registration = registrations[i]; | ||
registration.removeListeners(); | ||
registration.options = options; | ||
break; | ||
} | ||
} | ||
// 3. | ||
// Otherwise, add a new registered observer to target's list of registered | ||
// observers with the context object as the observer and options as the | ||
// options, and add target to context object's list of nodes on which it | ||
// is registered. | ||
if (!registration) { | ||
registration = new Registration(this, target, options); | ||
registrations.push(registration); | ||
this.nodes.push(target); | ||
} | ||
registration.addListeners(); | ||
}; | ||
_proto2.disconnect = function disconnect() { | ||
var _this = this; | ||
this.nodes.forEach(function (node) { | ||
var registrations = registrationsTable.get(node); | ||
for (var i = 0; i < registrations.length; i++) { | ||
var registration = registrations[i]; | ||
if (registration.observer === _this) { | ||
registration.removeListeners(); | ||
registrations.splice(i, 1); | ||
// Each node can only have one registered observer associated with | ||
// this observer. | ||
break; | ||
MutationObserver.prototype.observe = function (target, options) { | ||
// 1.1 | ||
if ((!options.childList && !options.attributes && !options.characterData) || | ||
// 1.2 | ||
(options.attributeOldValue && !options.attributes) || | ||
// 1.3 | ||
(options.attributeFilter && | ||
options.attributeFilter.length && | ||
!options.attributes) || | ||
// 1.4 | ||
(options.characterDataOldValue && !options.characterData)) { | ||
throw new SyntaxError(); | ||
} | ||
} | ||
}, this); | ||
this.records = []; | ||
}; | ||
_proto2.takeRecords = function takeRecords() { | ||
var copyOfRecords = this.records; | ||
this.records = []; | ||
return copyOfRecords; | ||
}; | ||
return MutationObserver; | ||
}(); | ||
var registrations = registrationsTable.get(target); | ||
if (!registrations) | ||
registrationsTable.set(target, (registrations = [])); | ||
// 2 | ||
// If target's list of registered observers already includes a registered | ||
// observer associated with the context object, replace that registered | ||
// observer's options with options. | ||
var registration; | ||
for (var i = 0; i < registrations.length; i++) { | ||
if (registrations[i].observer === this) { | ||
registration = registrations[i]; | ||
registration.removeListeners(); | ||
registration.options = options; | ||
break; | ||
} | ||
} | ||
// 3. | ||
// Otherwise, add a new registered observer to target's list of registered | ||
// observers with the context object as the observer and options as the | ||
// options, and add target to context object's list of nodes on which it | ||
// is registered. | ||
if (!registration) { | ||
registration = new Registration(this, target, options); | ||
registrations.push(registration); | ||
this.nodes.push(target); | ||
} | ||
registration.addListeners(); | ||
}; | ||
MutationObserver.prototype.disconnect = function () { | ||
var _this = this; | ||
this.nodes.forEach(function (node) { | ||
var registrations = registrationsTable.get(node); | ||
for (var i = 0; i < registrations.length; i++) { | ||
var registration = registrations[i]; | ||
if (registration.observer === _this) { | ||
registration.removeListeners(); | ||
registrations.splice(i, 1); | ||
// Each node can only have one registered observer associated with | ||
// this observer. | ||
break; | ||
} | ||
} | ||
}, this); | ||
this.records = []; | ||
}; | ||
MutationObserver.prototype.takeRecords = function () { | ||
var copyOfRecords = this.records; | ||
this.records = []; | ||
return copyOfRecords; | ||
}; | ||
return MutationObserver; | ||
}()); | ||
// We keep track of the two (possibly one) records used in a single mutation. | ||
@@ -287,3 +295,3 @@ var currentRecord; | ||
function getRecord(type, target) { | ||
return currentRecord = new MutationRecord(type, target); | ||
return (currentRecord = new MutationRecord(type, target)); | ||
} | ||
@@ -294,9 +302,10 @@ /** | ||
function getRecordWithOldValue(oldValue) { | ||
if (recordWithOldValue) return recordWithOldValue; | ||
recordWithOldValue = MutationRecord.copy(currentRecord); | ||
recordWithOldValue.oldValue = oldValue; | ||
return recordWithOldValue; | ||
if (recordWithOldValue) | ||
return recordWithOldValue; | ||
recordWithOldValue = MutationRecord.copy(currentRecord); | ||
recordWithOldValue.oldValue = oldValue; | ||
return recordWithOldValue; | ||
} | ||
function clearRecords() { | ||
currentRecord = recordWithOldValue = undefined; | ||
currentRecord = recordWithOldValue = undefined; | ||
} | ||
@@ -308,3 +317,3 @@ /** | ||
function recordRepresentsCurrentMutation(record) { | ||
return record === recordWithOldValue || record === currentRecord; | ||
return record === recordWithOldValue || record === currentRecord; | ||
} | ||
@@ -316,16 +325,20 @@ /** | ||
function selectRecord(lastRecord, newRecord) { | ||
if (lastRecord === newRecord) return lastRecord; | ||
// Check if the the record we are adding represents the same record. If | ||
// so, we keep the one with the oldValue in it. | ||
if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord)) return recordWithOldValue; | ||
return null; | ||
if (lastRecord === newRecord) | ||
return lastRecord; | ||
// Check if the the record we are adding represents the same record. If | ||
// so, we keep the one with the oldValue in it. | ||
if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord)) | ||
return recordWithOldValue; | ||
return null; | ||
} | ||
function removeTransientObserversFor(observer) { | ||
observer.nodes.forEach(function (node) { | ||
var registrations = registrationsTable.get(node); | ||
if (!registrations) return; | ||
registrations.forEach(function (registration) { | ||
if (registration.observer === observer) registration.removeTransientObservers(); | ||
observer.nodes.forEach(function (node) { | ||
var registrations = registrationsTable.get(node); | ||
if (!registrations) | ||
return; | ||
registrations.forEach(function (registration) { | ||
if (registration.observer === observer) | ||
registration.removeTransientObservers(); | ||
}); | ||
}); | ||
}); | ||
} | ||
@@ -345,15 +358,17 @@ /** | ||
function forEachAncestorAndObserverEnqueueRecord(target, callback) { | ||
for (var node = target; node; node = node.parentNode) { | ||
var registrations = registrationsTable.get(node); | ||
if (registrations) { | ||
for (var j = 0; j < registrations.length; j++) { | ||
var registration = registrations[j]; | ||
var options = registration.options; | ||
// Only target ignores subtree. | ||
if (node !== target && !options.subtree) continue; | ||
var record = callback(options); | ||
if (record) registration.enqueue(record); | ||
} | ||
for (var node = target; node; node = node.parentNode) { | ||
var registrations = registrationsTable.get(node); | ||
if (registrations) { | ||
for (var j = 0; j < registrations.length; j++) { | ||
var registration = registrations[j]; | ||
var options = registration.options; | ||
// Only target ignores subtree. | ||
if (node !== target && !options.subtree) | ||
continue; | ||
var record = callback(options); | ||
if (record) | ||
registration.enqueue(record); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -368,39 +383,42 @@ // This is used to ensure that we never schedule 2 callas to setImmediate | ||
function scheduleCallback(observer) { | ||
scheduledObservers.push(observer); | ||
if (!isScheduled) { | ||
isScheduled = true; | ||
// setImmediate(dispatchCallbacks); | ||
if (typeof runtime.globalThis !== 'undefined') { | ||
runtime.globalThis.setTimeout(dispatchCallbacks); | ||
} else { | ||
dispatchCallbacks(); | ||
scheduledObservers.push(observer); | ||
if (!isScheduled) { | ||
isScheduled = true; | ||
// setImmediate(dispatchCallbacks); | ||
if (typeof runtime.globalThis !== 'undefined') { | ||
runtime.globalThis.setTimeout(dispatchCallbacks); | ||
} | ||
else { | ||
dispatchCallbacks(); | ||
} | ||
} | ||
} | ||
} | ||
function dispatchCallbacks() { | ||
// http://dom.spec.whatwg.org/#mutation-observers | ||
isScheduled = false; // Used to allow a new setImmediate call above. | ||
var observers = scheduledObservers; | ||
scheduledObservers = []; | ||
// Sort observers based on their creation UID (incremental). | ||
observers.sort(function (o1, o2) { | ||
return o1.uid - o2.uid; | ||
}); | ||
var anyNonEmpty = false; | ||
observers.forEach(function (observer) { | ||
// 2.1, 2.2 | ||
var queue = observer.takeRecords(); | ||
// 2.3. Remove all transient registered observers whose observer is mo. | ||
removeTransientObserversFor(observer); | ||
// 2.4 | ||
if (queue.length) { | ||
// @ts-ignore | ||
observer.callback(queue, observer); | ||
anyNonEmpty = true; | ||
} | ||
}); | ||
// 3. | ||
if (anyNonEmpty) dispatchCallbacks(); | ||
// http://dom.spec.whatwg.org/#mutation-observers | ||
isScheduled = false; // Used to allow a new setImmediate call above. | ||
var observers = scheduledObservers; | ||
scheduledObservers = []; | ||
// Sort observers based on their creation UID (incremental). | ||
observers.sort(function (o1, o2) { | ||
return o1.uid - o2.uid; | ||
}); | ||
var anyNonEmpty = false; | ||
observers.forEach(function (observer) { | ||
// 2.1, 2.2 | ||
var queue = observer.takeRecords(); | ||
// 2.3. Remove all transient registered observers whose observer is mo. | ||
removeTransientObserversFor(observer); | ||
// 2.4 | ||
if (queue.length) { | ||
// @ts-ignore | ||
observer.callback(queue, observer); | ||
anyNonEmpty = true; | ||
} | ||
}); | ||
// 3. | ||
if (anyNonEmpty) | ||
dispatchCallbacks(); | ||
} | ||
export { MutationObserver, MutationRecord, Registration }; | ||
//# sourceMappingURL=index.esm.js.map |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var gLite = require('@antv/g-lite'); | ||
var MutationRecord = /*#__PURE__*/function () { | ||
MutationRecord.copy = function copy(original) { | ||
var record = new MutationRecord(original.type, original.target); | ||
record.addedNodes = original.addedNodes.slice(); | ||
record.removedNodes = original.removedNodes.slice(); | ||
record.previousSibling = original.previousSibling; | ||
record.nextSibling = original.nextSibling; | ||
record.attributeName = original.attributeName; | ||
record.attributeNamespace = original.attributeNamespace; | ||
record.oldValue = original.oldValue; | ||
return record; | ||
}; | ||
function MutationRecord(type, target) { | ||
this.type = void 0; | ||
this.target = void 0; | ||
this.addedNodes = []; | ||
this.attributeName = null; | ||
this.attributeNamespace = null; | ||
this.nextSibling = null; | ||
this.oldValue = null; | ||
this.previousSibling = null; | ||
this.removedNodes = []; | ||
this.type = type; | ||
this.target = target; | ||
} | ||
return MutationRecord; | ||
}(); | ||
var MutationRecord = /** @class */ (function () { | ||
function MutationRecord(type, target) { | ||
this.type = type; | ||
this.target = target; | ||
this.addedNodes = []; | ||
this.attributeName = null; | ||
this.attributeNamespace = null; | ||
this.nextSibling = null; | ||
this.oldValue = null; | ||
this.previousSibling = null; | ||
this.removedNodes = []; | ||
} | ||
MutationRecord.copy = function (original) { | ||
var record = new MutationRecord(original.type, original.target); | ||
record.addedNodes = original.addedNodes.slice(); | ||
record.removedNodes = original.removedNodes.slice(); | ||
record.previousSibling = original.previousSibling; | ||
record.nextSibling = original.nextSibling; | ||
record.attributeName = original.attributeName; | ||
record.attributeNamespace = original.attributeNamespace; | ||
record.oldValue = original.oldValue; | ||
return record; | ||
}; | ||
return MutationRecord; | ||
}()); | ||
var uidCounter = 0; | ||
var registrationsTable = new WeakMap(); | ||
var Registration = /*#__PURE__*/function () { | ||
function Registration(observer, target, options) { | ||
this.observer = void 0; | ||
this.target = void 0; | ||
this.options = void 0; | ||
this.transientObservedNodes = []; | ||
this.observer = observer; | ||
this.target = target; | ||
this.options = options; | ||
} | ||
var _proto = Registration.prototype; | ||
_proto.enqueue = function enqueue(record) { | ||
var records = this.observer.records; | ||
var length = records.length; | ||
// There are cases where we replace the last record with the new record. | ||
// For example if the record represents the same mutation we need to use | ||
// the one with the oldValue. If we get same record (this can happen as we | ||
// walk up the tree) we ignore the new record. | ||
if (records.length > 0) { | ||
var lastRecord = records[length - 1]; | ||
var recordToReplaceLast = selectRecord(lastRecord, record); | ||
if (recordToReplaceLast) { | ||
records[length - 1] = recordToReplaceLast; | ||
return; | ||
} | ||
} else { | ||
scheduleCallback(this.observer); | ||
var Registration = /** @class */ (function () { | ||
function Registration(observer, target, options) { | ||
this.observer = observer; | ||
this.target = target; | ||
this.options = options; | ||
this.transientObservedNodes = []; | ||
} | ||
records[length] = record; | ||
}; | ||
_proto.addListeners = function addListeners() { | ||
this.addListeners_(this.target); | ||
}; | ||
_proto.addListeners_ = function addListeners_(node) { | ||
var options = this.options; | ||
if (options.attributes) node.addEventListener(gLite.ElementEvent.ATTR_MODIFIED, this, true); | ||
// if (options.characterData) node.addEventListener('DOMCharacterDataModified', this, true); | ||
if (options.childList) node.addEventListener(gLite.ElementEvent.INSERTED, this, true); | ||
if (options.childList || options.subtree) node.addEventListener(gLite.ElementEvent.REMOVED, this, true); | ||
}; | ||
_proto.removeListeners = function removeListeners() { | ||
this.removeListeners_(this.target); | ||
}; | ||
_proto.removeListeners_ = function removeListeners_(node) { | ||
var options = this.options; | ||
if (options.attributes) node.removeEventListener(gLite.ElementEvent.ATTR_MODIFIED, this, true); | ||
// if (options.characterData) node.removeEventListener('DOMCharacterDataModified', this, true); | ||
if (options.childList) node.removeEventListener(gLite.ElementEvent.INSERTED, this, true); | ||
if (options.childList || options.subtree) node.removeEventListener(gLite.ElementEvent.REMOVED, this, true); | ||
} | ||
/** | ||
* Adds a transient observer on node. The transient observer gets removed | ||
* next time we deliver the change records. | ||
*/ | ||
// addTransientObserver(node: IElement) { | ||
// // Don't add transient observers on the target itself. We already have all | ||
// // the required listeners set up on the target. | ||
// if (node === this.target) return; | ||
// this.addListeners_(node); | ||
// this.transientObservedNodes.push(node); | ||
// let registrations = registrationsTable.get(node); | ||
// if (!registrations) registrationsTable.set(node, (registrations = [])); | ||
// // We know that registrations does not contain this because we already | ||
// // checked if node === this.target. | ||
// registrations.push(this); | ||
// } | ||
; | ||
_proto.removeTransientObservers = function removeTransientObservers() { | ||
var transientObservedNodes = this.transientObservedNodes; | ||
this.transientObservedNodes = []; | ||
transientObservedNodes.forEach(function (node) { | ||
// Transient observers are never added to the target. | ||
this.removeListeners_(node); | ||
var registrations = registrationsTable.get(node); | ||
for (var i = 0; i < registrations.length; i++) { | ||
if (registrations[i] === this) { | ||
registrations.splice(i, 1); | ||
// Each node can only have one registered observer associated with | ||
// this observer. | ||
break; | ||
Registration.prototype.enqueue = function (record) { | ||
var records = this.observer.records; | ||
var length = records.length; | ||
// There are cases where we replace the last record with the new record. | ||
// For example if the record represents the same mutation we need to use | ||
// the one with the oldValue. If we get same record (this can happen as we | ||
// walk up the tree) we ignore the new record. | ||
if (records.length > 0) { | ||
var lastRecord = records[length - 1]; | ||
var recordToReplaceLast = selectRecord(lastRecord, record); | ||
if (recordToReplaceLast) { | ||
records[length - 1] = recordToReplaceLast; | ||
return; | ||
} | ||
} | ||
} | ||
}, this); | ||
}; | ||
_proto.handleEvent = function handleEvent(e) { | ||
// Stop propagation since we are managing the propagation manually. | ||
// This means that other mutation events on the page will not work | ||
// correctly but that is by design. | ||
e.stopImmediatePropagation(); | ||
var record; | ||
var target; | ||
switch (e.type) { | ||
case gLite.ElementEvent.ATTR_MODIFIED: | ||
// http://dom.spec.whatwg.org/#concept-mo-queue-attributes | ||
var name = e.attrName; | ||
// @ts-ignore | ||
var namespace = e.relatedNode.namespaceURI; | ||
target = e.target; | ||
// 1. | ||
record = getRecord('attributes', target); | ||
record.attributeName = name; | ||
record.attributeNamespace = namespace; | ||
// 2. | ||
var oldValue = e.attrChange === gLite.MutationEvent.ADDITION ? null : e.prevValue; | ||
forEachAncestorAndObserverEnqueueRecord(target, function (options) { | ||
// 3.1, 4.2 | ||
if (!options.attributes) return; | ||
// 3.2, 4.3 | ||
if (options.attributeFilter && options.attributeFilter.length && options.attributeFilter.indexOf(name) === -1 && options.attributeFilter.indexOf(namespace) === -1) { | ||
return; | ||
} | ||
// 3.3, 4.4 | ||
if (options.attributeOldValue) return getRecordWithOldValue(oldValue); | ||
// 3.4, 4.5 | ||
return record; | ||
}); | ||
break; | ||
// case 'DOMCharacterDataModified': | ||
// // http://dom.spec.whatwg.org/#concept-mo-queue-characterdata | ||
// var target = e.target; | ||
// // 1. | ||
// var record = getRecord('characterData', target); | ||
// // 2. | ||
// var oldValue = e.prevValue; | ||
// forEachAncestorAndObserverEnqueueRecord(target, function(options) { | ||
// // 3.1, 4.2 | ||
// if (!options.characterData) | ||
// return; | ||
// // 3.2, 4.3 | ||
// if (options.characterDataOldValue) | ||
// return getRecordWithOldValue(oldValue); | ||
// // 3.3, 4.4 | ||
// return record; | ||
// }); | ||
// break; | ||
case gLite.ElementEvent.REMOVED: | ||
// this.addTransientObserver(e.target as IElement); | ||
// Fall through. | ||
case gLite.ElementEvent.INSERTED: | ||
// http://dom.spec.whatwg.org/#concept-mo-queue-childlist | ||
target = e.relatedNode; | ||
var changedNode = e.target; | ||
var addedNodes; | ||
var removedNodes; | ||
if (e.type === gLite.ElementEvent.INSERTED) { | ||
addedNodes = [changedNode]; | ||
removedNodes = []; | ||
} else { | ||
addedNodes = []; | ||
removedNodes = [changedNode]; | ||
else { | ||
scheduleCallback(this.observer); | ||
} | ||
var previousSibling = changedNode.previousSibling; | ||
var nextSibling = changedNode.nextSibling; | ||
// 1. | ||
record = getRecord('childList', target); | ||
record.addedNodes = addedNodes; | ||
record.removedNodes = removedNodes; | ||
record.previousSibling = previousSibling; | ||
record.nextSibling = nextSibling; | ||
forEachAncestorAndObserverEnqueueRecord(target, function (options) { | ||
// 2.1, 3.2 | ||
if (!options.childList) return; | ||
// 2.2, 3.3 | ||
return record; | ||
}); | ||
} | ||
clearRecords(); | ||
}; | ||
return Registration; | ||
}(); | ||
records[length] = record; | ||
}; | ||
Registration.prototype.addListeners = function () { | ||
this.addListeners_(this.target); | ||
}; | ||
Registration.prototype.addListeners_ = function (node) { | ||
var options = this.options; | ||
if (options.attributes) | ||
node.addEventListener(gLite.ElementEvent.ATTR_MODIFIED, this, true); | ||
// if (options.characterData) node.addEventListener('DOMCharacterDataModified', this, true); | ||
if (options.childList) | ||
node.addEventListener(gLite.ElementEvent.INSERTED, this, true); | ||
if (options.childList || options.subtree) | ||
node.addEventListener(gLite.ElementEvent.REMOVED, this, true); | ||
}; | ||
Registration.prototype.removeListeners = function () { | ||
this.removeListeners_(this.target); | ||
}; | ||
Registration.prototype.removeListeners_ = function (node) { | ||
var options = this.options; | ||
if (options.attributes) | ||
node.removeEventListener(gLite.ElementEvent.ATTR_MODIFIED, this, true); | ||
// if (options.characterData) node.removeEventListener('DOMCharacterDataModified', this, true); | ||
if (options.childList) | ||
node.removeEventListener(gLite.ElementEvent.INSERTED, this, true); | ||
if (options.childList || options.subtree) | ||
node.removeEventListener(gLite.ElementEvent.REMOVED, this, true); | ||
}; | ||
/** | ||
* Adds a transient observer on node. The transient observer gets removed | ||
* next time we deliver the change records. | ||
*/ | ||
// addTransientObserver(node: IElement) { | ||
// // Don't add transient observers on the target itself. We already have all | ||
// // the required listeners set up on the target. | ||
// if (node === this.target) return; | ||
// this.addListeners_(node); | ||
// this.transientObservedNodes.push(node); | ||
// let registrations = registrationsTable.get(node); | ||
// if (!registrations) registrationsTable.set(node, (registrations = [])); | ||
// // We know that registrations does not contain this because we already | ||
// // checked if node === this.target. | ||
// registrations.push(this); | ||
// } | ||
Registration.prototype.removeTransientObservers = function () { | ||
var transientObservedNodes = this.transientObservedNodes; | ||
this.transientObservedNodes = []; | ||
transientObservedNodes.forEach(function (node) { | ||
// Transient observers are never added to the target. | ||
this.removeListeners_(node); | ||
var registrations = registrationsTable.get(node); | ||
for (var i = 0; i < registrations.length; i++) { | ||
if (registrations[i] === this) { | ||
registrations.splice(i, 1); | ||
// Each node can only have one registered observer associated with | ||
// this observer. | ||
break; | ||
} | ||
} | ||
}, this); | ||
}; | ||
Registration.prototype.handleEvent = function (e) { | ||
// Stop propagation since we are managing the propagation manually. | ||
// This means that other mutation events on the page will not work | ||
// correctly but that is by design. | ||
e.stopImmediatePropagation(); | ||
var record; | ||
var target; | ||
switch (e.type) { | ||
case gLite.ElementEvent.ATTR_MODIFIED: | ||
// http://dom.spec.whatwg.org/#concept-mo-queue-attributes | ||
var name_1 = e.attrName; | ||
// @ts-ignore | ||
var namespace_1 = e.relatedNode.namespaceURI; | ||
target = e.target; | ||
// 1. | ||
record = getRecord('attributes', target); | ||
record.attributeName = name_1; | ||
record.attributeNamespace = namespace_1; | ||
// 2. | ||
var oldValue_1 = e.attrChange === gLite.MutationEvent.ADDITION ? null : e.prevValue; | ||
forEachAncestorAndObserverEnqueueRecord(target, function (options) { | ||
// 3.1, 4.2 | ||
if (!options.attributes) | ||
return; | ||
// 3.2, 4.3 | ||
if (options.attributeFilter && | ||
options.attributeFilter.length && | ||
options.attributeFilter.indexOf(name_1) === -1 && | ||
options.attributeFilter.indexOf(namespace_1) === -1) { | ||
return; | ||
} | ||
// 3.3, 4.4 | ||
if (options.attributeOldValue) | ||
return getRecordWithOldValue(oldValue_1); | ||
// 3.4, 4.5 | ||
return record; | ||
}); | ||
break; | ||
// case 'DOMCharacterDataModified': | ||
// // http://dom.spec.whatwg.org/#concept-mo-queue-characterdata | ||
// var target = e.target; | ||
// // 1. | ||
// var record = getRecord('characterData', target); | ||
// // 2. | ||
// var oldValue = e.prevValue; | ||
// forEachAncestorAndObserverEnqueueRecord(target, function(options) { | ||
// // 3.1, 4.2 | ||
// if (!options.characterData) | ||
// return; | ||
// // 3.2, 4.3 | ||
// if (options.characterDataOldValue) | ||
// return getRecordWithOldValue(oldValue); | ||
// // 3.3, 4.4 | ||
// return record; | ||
// }); | ||
// break; | ||
case gLite.ElementEvent.REMOVED: | ||
// this.addTransientObserver(e.target as IElement); | ||
// Fall through. | ||
case gLite.ElementEvent.INSERTED: | ||
// http://dom.spec.whatwg.org/#concept-mo-queue-childlist | ||
target = e.relatedNode; | ||
var changedNode = e.target; | ||
var addedNodes = void 0; | ||
var removedNodes = void 0; | ||
if (e.type === gLite.ElementEvent.INSERTED) { | ||
addedNodes = [changedNode]; | ||
removedNodes = []; | ||
} | ||
else { | ||
addedNodes = []; | ||
removedNodes = [changedNode]; | ||
} | ||
var previousSibling = changedNode.previousSibling; | ||
var nextSibling = changedNode.nextSibling; | ||
// 1. | ||
record = getRecord('childList', target); | ||
record.addedNodes = addedNodes; | ||
record.removedNodes = removedNodes; | ||
record.previousSibling = previousSibling; | ||
record.nextSibling = nextSibling; | ||
forEachAncestorAndObserverEnqueueRecord(target, function (options) { | ||
// 2.1, 3.2 | ||
if (!options.childList) | ||
return; | ||
// 2.2, 3.3 | ||
return record; | ||
}); | ||
} | ||
clearRecords(); | ||
}; | ||
return Registration; | ||
}()); | ||
/** | ||
@@ -211,73 +216,74 @@ * @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver | ||
*/ | ||
var MutationObserver = /*#__PURE__*/function () { | ||
function MutationObserver(callback) { | ||
this.callback = void 0; | ||
this.nodes = []; | ||
this.records = []; | ||
this.uid = uidCounter++; | ||
this.callback = callback; | ||
} | ||
var _proto2 = MutationObserver.prototype; | ||
_proto2.observe = function observe(target, options) { | ||
// 1.1 | ||
if (!options.childList && !options.attributes && !options.characterData || | ||
// 1.2 | ||
options.attributeOldValue && !options.attributes || | ||
// 1.3 | ||
options.attributeFilter && options.attributeFilter.length && !options.attributes || | ||
// 1.4 | ||
options.characterDataOldValue && !options.characterData) { | ||
throw new SyntaxError(); | ||
var MutationObserver = /** @class */ (function () { | ||
function MutationObserver(callback) { | ||
this.callback = callback; | ||
this.nodes = []; | ||
this.records = []; | ||
this.uid = uidCounter++; | ||
} | ||
var registrations = registrationsTable.get(target); | ||
if (!registrations) registrationsTable.set(target, registrations = []); | ||
// 2 | ||
// If target's list of registered observers already includes a registered | ||
// observer associated with the context object, replace that registered | ||
// observer's options with options. | ||
var registration; | ||
for (var i = 0; i < registrations.length; i++) { | ||
if (registrations[i].observer === this) { | ||
registration = registrations[i]; | ||
registration.removeListeners(); | ||
registration.options = options; | ||
break; | ||
} | ||
} | ||
// 3. | ||
// Otherwise, add a new registered observer to target's list of registered | ||
// observers with the context object as the observer and options as the | ||
// options, and add target to context object's list of nodes on which it | ||
// is registered. | ||
if (!registration) { | ||
registration = new Registration(this, target, options); | ||
registrations.push(registration); | ||
this.nodes.push(target); | ||
} | ||
registration.addListeners(); | ||
}; | ||
_proto2.disconnect = function disconnect() { | ||
var _this = this; | ||
this.nodes.forEach(function (node) { | ||
var registrations = registrationsTable.get(node); | ||
for (var i = 0; i < registrations.length; i++) { | ||
var registration = registrations[i]; | ||
if (registration.observer === _this) { | ||
registration.removeListeners(); | ||
registrations.splice(i, 1); | ||
// Each node can only have one registered observer associated with | ||
// this observer. | ||
break; | ||
MutationObserver.prototype.observe = function (target, options) { | ||
// 1.1 | ||
if ((!options.childList && !options.attributes && !options.characterData) || | ||
// 1.2 | ||
(options.attributeOldValue && !options.attributes) || | ||
// 1.3 | ||
(options.attributeFilter && | ||
options.attributeFilter.length && | ||
!options.attributes) || | ||
// 1.4 | ||
(options.characterDataOldValue && !options.characterData)) { | ||
throw new SyntaxError(); | ||
} | ||
} | ||
}, this); | ||
this.records = []; | ||
}; | ||
_proto2.takeRecords = function takeRecords() { | ||
var copyOfRecords = this.records; | ||
this.records = []; | ||
return copyOfRecords; | ||
}; | ||
return MutationObserver; | ||
}(); | ||
var registrations = registrationsTable.get(target); | ||
if (!registrations) | ||
registrationsTable.set(target, (registrations = [])); | ||
// 2 | ||
// If target's list of registered observers already includes a registered | ||
// observer associated with the context object, replace that registered | ||
// observer's options with options. | ||
var registration; | ||
for (var i = 0; i < registrations.length; i++) { | ||
if (registrations[i].observer === this) { | ||
registration = registrations[i]; | ||
registration.removeListeners(); | ||
registration.options = options; | ||
break; | ||
} | ||
} | ||
// 3. | ||
// Otherwise, add a new registered observer to target's list of registered | ||
// observers with the context object as the observer and options as the | ||
// options, and add target to context object's list of nodes on which it | ||
// is registered. | ||
if (!registration) { | ||
registration = new Registration(this, target, options); | ||
registrations.push(registration); | ||
this.nodes.push(target); | ||
} | ||
registration.addListeners(); | ||
}; | ||
MutationObserver.prototype.disconnect = function () { | ||
var _this = this; | ||
this.nodes.forEach(function (node) { | ||
var registrations = registrationsTable.get(node); | ||
for (var i = 0; i < registrations.length; i++) { | ||
var registration = registrations[i]; | ||
if (registration.observer === _this) { | ||
registration.removeListeners(); | ||
registrations.splice(i, 1); | ||
// Each node can only have one registered observer associated with | ||
// this observer. | ||
break; | ||
} | ||
} | ||
}, this); | ||
this.records = []; | ||
}; | ||
MutationObserver.prototype.takeRecords = function () { | ||
var copyOfRecords = this.records; | ||
this.records = []; | ||
return copyOfRecords; | ||
}; | ||
return MutationObserver; | ||
}()); | ||
// We keep track of the two (possibly one) records used in a single mutation. | ||
@@ -291,3 +297,3 @@ var currentRecord; | ||
function getRecord(type, target) { | ||
return currentRecord = new MutationRecord(type, target); | ||
return (currentRecord = new MutationRecord(type, target)); | ||
} | ||
@@ -298,9 +304,10 @@ /** | ||
function getRecordWithOldValue(oldValue) { | ||
if (recordWithOldValue) return recordWithOldValue; | ||
recordWithOldValue = MutationRecord.copy(currentRecord); | ||
recordWithOldValue.oldValue = oldValue; | ||
return recordWithOldValue; | ||
if (recordWithOldValue) | ||
return recordWithOldValue; | ||
recordWithOldValue = MutationRecord.copy(currentRecord); | ||
recordWithOldValue.oldValue = oldValue; | ||
return recordWithOldValue; | ||
} | ||
function clearRecords() { | ||
currentRecord = recordWithOldValue = undefined; | ||
currentRecord = recordWithOldValue = undefined; | ||
} | ||
@@ -312,3 +319,3 @@ /** | ||
function recordRepresentsCurrentMutation(record) { | ||
return record === recordWithOldValue || record === currentRecord; | ||
return record === recordWithOldValue || record === currentRecord; | ||
} | ||
@@ -320,16 +327,20 @@ /** | ||
function selectRecord(lastRecord, newRecord) { | ||
if (lastRecord === newRecord) return lastRecord; | ||
// Check if the the record we are adding represents the same record. If | ||
// so, we keep the one with the oldValue in it. | ||
if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord)) return recordWithOldValue; | ||
return null; | ||
if (lastRecord === newRecord) | ||
return lastRecord; | ||
// Check if the the record we are adding represents the same record. If | ||
// so, we keep the one with the oldValue in it. | ||
if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord)) | ||
return recordWithOldValue; | ||
return null; | ||
} | ||
function removeTransientObserversFor(observer) { | ||
observer.nodes.forEach(function (node) { | ||
var registrations = registrationsTable.get(node); | ||
if (!registrations) return; | ||
registrations.forEach(function (registration) { | ||
if (registration.observer === observer) registration.removeTransientObservers(); | ||
observer.nodes.forEach(function (node) { | ||
var registrations = registrationsTable.get(node); | ||
if (!registrations) | ||
return; | ||
registrations.forEach(function (registration) { | ||
if (registration.observer === observer) | ||
registration.removeTransientObservers(); | ||
}); | ||
}); | ||
}); | ||
} | ||
@@ -349,15 +360,17 @@ /** | ||
function forEachAncestorAndObserverEnqueueRecord(target, callback) { | ||
for (var node = target; node; node = node.parentNode) { | ||
var registrations = registrationsTable.get(node); | ||
if (registrations) { | ||
for (var j = 0; j < registrations.length; j++) { | ||
var registration = registrations[j]; | ||
var options = registration.options; | ||
// Only target ignores subtree. | ||
if (node !== target && !options.subtree) continue; | ||
var record = callback(options); | ||
if (record) registration.enqueue(record); | ||
} | ||
for (var node = target; node; node = node.parentNode) { | ||
var registrations = registrationsTable.get(node); | ||
if (registrations) { | ||
for (var j = 0; j < registrations.length; j++) { | ||
var registration = registrations[j]; | ||
var options = registration.options; | ||
// Only target ignores subtree. | ||
if (node !== target && !options.subtree) | ||
continue; | ||
var record = callback(options); | ||
if (record) | ||
registration.enqueue(record); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -372,37 +385,39 @@ // This is used to ensure that we never schedule 2 callas to setImmediate | ||
function scheduleCallback(observer) { | ||
scheduledObservers.push(observer); | ||
if (!isScheduled) { | ||
isScheduled = true; | ||
// setImmediate(dispatchCallbacks); | ||
if (typeof gLite.runtime.globalThis !== 'undefined') { | ||
gLite.runtime.globalThis.setTimeout(dispatchCallbacks); | ||
} else { | ||
dispatchCallbacks(); | ||
scheduledObservers.push(observer); | ||
if (!isScheduled) { | ||
isScheduled = true; | ||
// setImmediate(dispatchCallbacks); | ||
if (typeof gLite.runtime.globalThis !== 'undefined') { | ||
gLite.runtime.globalThis.setTimeout(dispatchCallbacks); | ||
} | ||
else { | ||
dispatchCallbacks(); | ||
} | ||
} | ||
} | ||
} | ||
function dispatchCallbacks() { | ||
// http://dom.spec.whatwg.org/#mutation-observers | ||
isScheduled = false; // Used to allow a new setImmediate call above. | ||
var observers = scheduledObservers; | ||
scheduledObservers = []; | ||
// Sort observers based on their creation UID (incremental). | ||
observers.sort(function (o1, o2) { | ||
return o1.uid - o2.uid; | ||
}); | ||
var anyNonEmpty = false; | ||
observers.forEach(function (observer) { | ||
// 2.1, 2.2 | ||
var queue = observer.takeRecords(); | ||
// 2.3. Remove all transient registered observers whose observer is mo. | ||
removeTransientObserversFor(observer); | ||
// 2.4 | ||
if (queue.length) { | ||
// @ts-ignore | ||
observer.callback(queue, observer); | ||
anyNonEmpty = true; | ||
} | ||
}); | ||
// 3. | ||
if (anyNonEmpty) dispatchCallbacks(); | ||
// http://dom.spec.whatwg.org/#mutation-observers | ||
isScheduled = false; // Used to allow a new setImmediate call above. | ||
var observers = scheduledObservers; | ||
scheduledObservers = []; | ||
// Sort observers based on their creation UID (incremental). | ||
observers.sort(function (o1, o2) { | ||
return o1.uid - o2.uid; | ||
}); | ||
var anyNonEmpty = false; | ||
observers.forEach(function (observer) { | ||
// 2.1, 2.2 | ||
var queue = observer.takeRecords(); | ||
// 2.3. Remove all transient registered observers whose observer is mo. | ||
removeTransientObserversFor(observer); | ||
// 2.4 | ||
if (queue.length) { | ||
// @ts-ignore | ||
observer.callback(queue, observer); | ||
anyNonEmpty = true; | ||
} | ||
}); | ||
// 3. | ||
if (anyNonEmpty) | ||
dispatchCallbacks(); | ||
} | ||
@@ -413,1 +428,2 @@ | ||
exports.Registration = Registration; | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@antv/g-lite")):"function"==typeof define&&define.amd?define(["exports","@antv/g-lite"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).G=e.G||{},e.G.DOMMutationObserverAPI={}),e.window.G)}(this,(function(e,t){"use strict";var i,r,n=function(){function e(e,t){this.type=void 0,this.target=void 0,this.addedNodes=[],this.attributeName=null,this.attributeNamespace=null,this.nextSibling=null,this.oldValue=null,this.previousSibling=null,this.removedNodes=[],this.type=e,this.target=t}return e.copy=function(t){var i=new e(t.type,t.target);return i.addedNodes=t.addedNodes.slice(),i.removedNodes=t.removedNodes.slice(),i.previousSibling=t.previousSibling,i.nextSibling=t.nextSibling,i.attributeName=t.attributeName,i.attributeNamespace=t.attributeNamespace,i.oldValue=t.oldValue,i},e}(),s=0,a=new WeakMap,o=function(){function e(e,t,i){this.observer=void 0,this.target=void 0,this.options=void 0,this.transientObservedNodes=[],this.observer=e,this.target=t,this.options=i}var s=e.prototype;return s.enqueue=function(e){var n=this.observer.records,s=n.length;if(n.length>0){var a=function(e,t){return e===t?e:r&&function(e){return e===r||e===i}(e)?r:null}(n[s-1],e);if(a)return void(n[s-1]=a)}else v.push(this.observer),l||(l=!0,void 0!==t.runtime.globalThis?t.runtime.globalThis.setTimeout(c):c());n[s]=e},s.addListeners=function(){this.addListeners_(this.target)},s.addListeners_=function(e){var i=this.options;i.attributes&&e.addEventListener(t.ElementEvent.ATTR_MODIFIED,this,!0),i.childList&&e.addEventListener(t.ElementEvent.INSERTED,this,!0),(i.childList||i.subtree)&&e.addEventListener(t.ElementEvent.REMOVED,this,!0)},s.removeListeners=function(){this.removeListeners_(this.target)},s.removeListeners_=function(e){var i=this.options;i.attributes&&e.removeEventListener(t.ElementEvent.ATTR_MODIFIED,this,!0),i.childList&&e.removeEventListener(t.ElementEvent.INSERTED,this,!0),(i.childList||i.subtree)&&e.removeEventListener(t.ElementEvent.REMOVED,this,!0)},s.removeTransientObservers=function(){var e=this.transientObservedNodes;this.transientObservedNodes=[],e.forEach((function(e){this.removeListeners_(e);for(var t=a.get(e),i=0;t.length>i;i++)if(t[i]===this){t.splice(i,1);break}}),this)},s.handleEvent=function(e){var s,a;switch(e.stopImmediatePropagation(),e.type){case t.ElementEvent.ATTR_MODIFIED:var o=e.attrName,l=e.relatedNode.namespaceURI;(s=u("attributes",a=e.target)).attributeName=o,s.attributeNamespace=l;var v=e.attrChange===t.MutationEvent.ADDITION?null:e.prevValue;d(a,(function(e){if(e.attributes&&(!e.attributeFilter||!e.attributeFilter.length||-1!==e.attributeFilter.indexOf(o)||-1!==e.attributeFilter.indexOf(l)))return e.attributeOldValue?function(e){return r||((r=n.copy(i)).oldValue=e,r)}(v):s}));break;case t.ElementEvent.REMOVED:case t.ElementEvent.INSERTED:var c,h,f=e.target;e.type===t.ElementEvent.INSERTED?(c=[f],h=[]):(c=[],h=[f]);var b=f.previousSibling,E=f.nextSibling;(s=u("childList",a=e.relatedNode)).addedNodes=c,s.removedNodes=h,s.previousSibling=b,s.nextSibling=E,d(a,(function(e){if(e.childList)return s}))}i=r=void 0},e}();function u(e,t){return i=new n(e,t)}function d(e,t){for(var i=e;i;i=i.parentNode){var r=a.get(i);if(r)for(var n=0;r.length>n;n++){var s=r[n],o=s.options;if(i===e||o.subtree){var u=t(o);u&&s.enqueue(u)}}}}var l=!1,v=[];function c(){l=!1;var e=v;v=[],e.sort((function(e,t){return e.uid-t.uid}));var t=!1;e.forEach((function(e){var i=e.takeRecords();!function(e){e.nodes.forEach((function(t){var i=a.get(t);i&&i.forEach((function(t){t.observer===e&&t.removeTransientObservers()}))}))}(e),i.length&&(e.callback(i,e),t=!0)})),t&&c()}e.MutationObserver=function(){function e(e){this.callback=void 0,this.nodes=[],this.records=[],this.uid=s++,this.callback=e}var t=e.prototype;return t.observe=function(e,t){if(!t.childList&&!t.attributes&&!t.characterData||t.attributeOldValue&&!t.attributes||t.attributeFilter&&t.attributeFilter.length&&!t.attributes||t.characterDataOldValue&&!t.characterData)throw new SyntaxError;var i,r=a.get(e);r||a.set(e,r=[]);for(var n=0;r.length>n;n++)if(r[n].observer===this){(i=r[n]).removeListeners(),i.options=t;break}i||(i=new o(this,e,t),r.push(i),this.nodes.push(e)),i.addListeners()},t.disconnect=function(){var e=this;this.nodes.forEach((function(t){for(var i=a.get(t),r=0;i.length>r;r++){var n=i[r];if(n.observer===e){n.removeListeners(),i.splice(r,1);break}}}),this),this.records=[]},t.takeRecords=function(){var e=this.records;return this.records=[],e},e}(),e.MutationRecord=n,e.Registration=o,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@antv/g-lite")):"function"==typeof define&&define.amd?define(["exports","@antv/g-lite"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).G=e.G||{},e.G.DOMMutationObserverAPI={}),e.window.G)}(this,(function(e,t){"use strict";var i,r,n=function(){function e(e,t){this.type=e,this.target=t,this.addedNodes=[],this.attributeName=null,this.attributeNamespace=null,this.nextSibling=null,this.oldValue=null,this.previousSibling=null,this.removedNodes=[]}return e.copy=function(t){var i=new e(t.type,t.target);return i.addedNodes=t.addedNodes.slice(),i.removedNodes=t.removedNodes.slice(),i.previousSibling=t.previousSibling,i.nextSibling=t.nextSibling,i.attributeName=t.attributeName,i.attributeNamespace=t.attributeNamespace,i.oldValue=t.oldValue,i},e}(),s=0,o=new WeakMap,a=function(){function e(e,t,i){this.observer=e,this.target=t,this.options=i,this.transientObservedNodes=[]}return e.prototype.enqueue=function(e){var n=this.observer.records,s=n.length;if(n.length>0){var o=function(e,t){return e===t?e:r&&function(e){return e===r||e===i}(e)?r:null}(n[s-1],e);if(o)return void(n[s-1]=o)}else c.push(this.observer),v||(v=!0,void 0!==t.runtime.globalThis?t.runtime.globalThis.setTimeout(h):h());n[s]=e},e.prototype.addListeners=function(){this.addListeners_(this.target)},e.prototype.addListeners_=function(e){var i=this.options;i.attributes&&e.addEventListener(t.ElementEvent.ATTR_MODIFIED,this,!0),i.childList&&e.addEventListener(t.ElementEvent.INSERTED,this,!0),(i.childList||i.subtree)&&e.addEventListener(t.ElementEvent.REMOVED,this,!0)},e.prototype.removeListeners=function(){this.removeListeners_(this.target)},e.prototype.removeListeners_=function(e){var i=this.options;i.attributes&&e.removeEventListener(t.ElementEvent.ATTR_MODIFIED,this,!0),i.childList&&e.removeEventListener(t.ElementEvent.INSERTED,this,!0),(i.childList||i.subtree)&&e.removeEventListener(t.ElementEvent.REMOVED,this,!0)},e.prototype.removeTransientObservers=function(){var e=this.transientObservedNodes;this.transientObservedNodes=[],e.forEach((function(e){this.removeListeners_(e);for(var t=o.get(e),i=0;t.length>i;i++)if(t[i]===this){t.splice(i,1);break}}),this)},e.prototype.handleEvent=function(e){var s,o;switch(e.stopImmediatePropagation(),e.type){case t.ElementEvent.ATTR_MODIFIED:var a=e.attrName,u=e.relatedNode.namespaceURI;(s=d("attributes",o=e.target)).attributeName=a,s.attributeNamespace=u;var v=e.attrChange===t.MutationEvent.ADDITION?null:e.prevValue;l(o,(function(e){var t;if(e.attributes&&(!e.attributeFilter||!e.attributeFilter.length||-1!==e.attributeFilter.indexOf(a)||-1!==e.attributeFilter.indexOf(u)))return e.attributeOldValue?(t=v,r||((r=n.copy(i)).oldValue=t,r)):s}));break;case t.ElementEvent.REMOVED:case t.ElementEvent.INSERTED:var c=e.target,h=void 0,f=void 0;e.type===t.ElementEvent.INSERTED?(h=[c],f=[]):(h=[],f=[c]);var b=c.previousSibling,p=c.nextSibling;(s=d("childList",o=e.relatedNode)).addedNodes=h,s.removedNodes=f,s.previousSibling=b,s.nextSibling=p,l(o,(function(e){if(e.childList)return s}))}i=r=void 0},e}(),u=function(){function e(e){this.callback=e,this.nodes=[],this.records=[],this.uid=s++}return e.prototype.observe=function(e,t){if(!t.childList&&!t.attributes&&!t.characterData||t.attributeOldValue&&!t.attributes||t.attributeFilter&&t.attributeFilter.length&&!t.attributes||t.characterDataOldValue&&!t.characterData)throw new SyntaxError;var i,r=o.get(e);r||o.set(e,r=[]);for(var n=0;r.length>n;n++)if(r[n].observer===this){(i=r[n]).removeListeners(),i.options=t;break}i||(i=new a(this,e,t),r.push(i),this.nodes.push(e)),i.addListeners()},e.prototype.disconnect=function(){var e=this;this.nodes.forEach((function(t){for(var i=o.get(t),r=0;i.length>r;r++){var n=i[r];if(n.observer===e){n.removeListeners(),i.splice(r,1);break}}}),this),this.records=[]},e.prototype.takeRecords=function(){var e=this.records;return this.records=[],e},e}();function d(e,t){return i=new n(e,t)}function l(e,t){for(var i=e;i;i=i.parentNode){var r=o.get(i);if(r)for(var n=0;r.length>n;n++){var s=r[n],a=s.options;if(i===e||a.subtree){var u=t(a);u&&s.enqueue(u)}}}}var v=!1,c=[];function h(){v=!1;var e=c;c=[],e.sort((function(e,t){return e.uid-t.uid}));var t=!1;e.forEach((function(e){var i=e.takeRecords();!function(e){e.nodes.forEach((function(t){var i=o.get(t);i&&i.forEach((function(t){t.observer===e&&t.removeTransientObservers()}))}))}(e),i.length&&(e.callback(i,e),t=!0)})),t&&h()}e.MutationObserver=u,e.MutationRecord=n,e.Registration=a})); | ||
//# sourceMappingURL=index.umd.min.js.map |
{ | ||
"name": "@antv/g-dom-mutation-observer-api", | ||
"version": "1.0.38", | ||
"version": "1.1.0-alpha.1", | ||
"description": "A simple implementation of DOM MutationObserver API.", | ||
@@ -20,2 +20,7 @@ "keywords": [ | ||
"author": "https://github.com/orgs/antvis/people", | ||
"exports": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.esm.js", | ||
"default": "./dist/index.js" | ||
}, | ||
"main": "dist/index.js", | ||
@@ -32,11 +37,13 @@ "unpkg": "dist/index.umd.min.js", | ||
"scripts": { | ||
"sync": "tnpm sync" | ||
"build": "npm run clean && rollup -c", | ||
"clean": "rimraf dist", | ||
"sync": "tnpm sync", | ||
"watch": "rollup -c -w" | ||
}, | ||
"peerDependencies": { | ||
"@antv/g-lite": "^1.0.0" | ||
"dependencies": { | ||
"@antv/g-lite": "workspace:*" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "53932c66ed9f2cbc28993e1e359c8df27703fc52" | ||
} | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
70525
13
902
2
+ Added@antv/g-lite@workspace:*
- Removed@antv/g-lite@1.2.24(transitive)
- Removed@antv/g-math@2.0.2(transitive)
- Removed@antv/util@3.3.10(transitive)
- Removedd3-color@1.4.1(transitive)
- Removedeventemitter3@5.0.1(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedgl-matrix@3.4.3(transitive)
- Removedquickselect@2.0.0(transitive)
- Removedrbush@3.0.1(transitive)
- Removedtslib@2.8.1(transitive)