Path: blob/main/core/injected-scripts/NodeTracker.ts
1028 views
// eslint-disable-next-line @typescript-eslint/no-unused-vars1function NodeTrackerStatics(constructor: IStaticNodeTracker) {}23@NodeTrackerStatics4class NodeTracker {5public static nodeIdSymbol = Symbol.for('saNodeId');6private static nextId = 1;7private static watchedNodesById = new Map<number, Node>();89public static has(node: Node): boolean {10return !!node[this.nodeIdSymbol];11}1213public static getNodeId(node: Node): number {14if (!node) return undefined;15return node[this.nodeIdSymbol] ?? undefined;16}1718public static watchNode(node: Node): number {19let id = this.getNodeId(node);20if (!id) {21// extract so we detect any nodes that haven't been extracted yet. Ie, called from jsPath22if ('extractDomChanges' in window) {23// @ts-ignore24window.extractDomChanges();25}26id = this.track(node);27}2829this.watchedNodesById.set(id, node);30return id;31}3233public static track(node: Node): number {34if (!node) return;35if (node[this.nodeIdSymbol]) {36return node[this.nodeIdSymbol];37}38const id = this.nextId;39this.nextId += 1;40node[this.nodeIdSymbol] = id;41return id;42}4344public static getWatchedNodeWithId(id: number, throwIfNotFound = true): Node | undefined {45if (this.watchedNodesById.has(id)) {46return this.watchedNodesById.get(id);47}48if (throwIfNotFound) throw new Error(`Node with id not found -> ${id}`);49}5051public static restore(id: number, node: Node): void {52node[this.nodeIdSymbol] = id;53this.watchedNodesById.set(id, node);54if (id > this.nextId) this.nextId = id;55}56}5758// @ts-ignore59window.NodeTracker = NodeTracker;606162