MCPcopy Create free account
hub / github.com/adobe/react-spectrum / trackMutations

Function trackMutations

packages/react-aria/src/tokenfield/useTokenField.ts:801–834  ·  view source on GitHub ↗
(element: Element)

Source from the content-addressed store, hash-verified

799// Tracks mutations to the DOM until the returned function is called,
800// at which point the mutations are reverted.
801function trackMutations(element: Element) {
802 let mutations: MutationRecord[] = [];
803 let observer = new MutationObserver(records => {
804 mutations.push(...records);
805 });
806
807 observer.observe(element, {
808 childList: true,
809 subtree: true,
810 characterData: true,
811 characterDataOldValue: true
812 });
813
814 return () => {
815 mutations.push(...observer.takeRecords());
816 observer.disconnect();
817
818 for (let record of mutations.reverse()) {
819 switch (record.type) {
820 case 'childList':
821 for (let node of record.removedNodes) {
822 record.target.insertBefore(node, record.nextSibling);
823 }
824 for (let node of record.addedNodes) {
825 record.target.removeChild(node);
826 }
827 break;
828 case 'characterData':
829 record.target.nodeValue = record.oldValue;
830 break;
831 }
832 }
833 };
834}

Callers 1

startFunction · 0.85

Calls 3

pushMethod · 0.80
insertBeforeMethod · 0.65
removeChildMethod · 0.45

Tested by

no test coverage detected