| 190 | } |
| 191 | |
| 192 | export class Mirror implements IMirror<Node> { |
| 193 | private idNodeMap: idNodeMap = new Map(); |
| 194 | private nodeMetaMap: nodeMetaMap = new WeakMap(); |
| 195 | |
| 196 | getId(n: Node | undefined | null): number { |
| 197 | if (!n) return -1; |
| 198 | |
| 199 | const id = this.getMeta(n)?.id; |
| 200 | |
| 201 | // if n is not a serialized Node, use -1 as its id. |
| 202 | return id ?? -1; |
| 203 | } |
| 204 | |
| 205 | getNode(id: number): Node | null { |
| 206 | return this.idNodeMap.get(id) || null; |
| 207 | } |
| 208 | |
| 209 | getIds(): number[] { |
| 210 | return Array.from(this.idNodeMap.keys()); |
| 211 | } |
| 212 | |
| 213 | getMeta(n: Node): serializedNodeWithId | null { |
| 214 | return this.nodeMetaMap.get(n) || null; |
| 215 | } |
| 216 | |
| 217 | // removes the node from idNodeMap |
| 218 | // doesn't remove the node from nodeMetaMap |
| 219 | removeNodeFromMap(n: Node) { |
| 220 | const id = this.getId(n); |
| 221 | this.idNodeMap.delete(id); |
| 222 | |
| 223 | if (n.childNodes) { |
| 224 | n.childNodes.forEach((childNode) => |
| 225 | this.removeNodeFromMap(childNode as unknown as Node), |
| 226 | ); |
| 227 | } |
| 228 | } |
| 229 | has(id: number): boolean { |
| 230 | return this.idNodeMap.has(id); |
| 231 | } |
| 232 | |
| 233 | hasNode(node: Node): boolean { |
| 234 | return this.nodeMetaMap.has(node); |
| 235 | } |
| 236 | |
| 237 | add(n: Node, meta: serializedNodeWithId) { |
| 238 | const id = meta.id; |
| 239 | this.idNodeMap.set(id, n); |
| 240 | this.nodeMetaMap.set(n, meta); |
| 241 | } |
| 242 | |
| 243 | replace(id: number, n: Node) { |
| 244 | const oldNode = this.getNode(id); |
| 245 | if (oldNode) { |
| 246 | const meta = this.nodeMetaMap.get(oldNode); |
| 247 | if (meta) this.nodeMetaMap.set(n, meta); |
| 248 | } |
| 249 | this.idNodeMap.set(id, n); |
nothing calls this directly
no outgoing calls
no test coverage detected