@mml-io/networked-dom-document 0.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,717 @@
1
+ import {
2
+ ClientMessage,
3
+ Diff,
4
+ NodeDescription,
5
+ PongMessage,
6
+ RemoteEvent,
7
+ ServerMessage,
8
+ SnapshotMessage,
9
+ } from "@mml-io/networked-dom-protocol";
10
+ import {
11
+ LogMessage,
12
+ ObservableDomInterface,
13
+ ObservableDomMessage,
14
+ ObservableDOMParameters,
15
+ StaticVirtualDomElement,
16
+ StaticVirtualDomMutationIdsRecord,
17
+ } from "@mml-io/observable-dom-common";
18
+ import { applyPatch } from "rfc6902";
19
+
20
+ import { StaticVirtualDomMutationRecord, VirtualDOMDiffStruct } from "./common";
21
+ import {
22
+ calculateStaticVirtualDomDiff,
23
+ describeNodeWithChildrenForConnectionId,
24
+ diffFromApplicationOfStaticVirtualDomMutationRecordToConnection,
25
+ findParentNodeOfNodeId,
26
+ virtualDOMDiffToVirtualDOMMutationRecord,
27
+ } from "./diffing";
28
+
29
+ export const networkedDomProtocolSubProtocol_v0_1 = "networked-dom-v0.1";
30
+ export const defaultWebsocketSubProtocol = networkedDomProtocolSubProtocol_v0_1;
31
+
32
+ export type ObservableDomFactory = (
33
+ observableDOMParameters: ObservableDOMParameters,
34
+ callback: (message: ObservableDomMessage) => void,
35
+ ) => ObservableDomInterface;
36
+
37
+ export class NetworkedDOM {
38
+ // First to last in order of preference
39
+ public static SupportedWebsocketSubProtocolsPreferenceOrder = [
40
+ networkedDomProtocolSubProtocol_v0_1,
41
+ ];
42
+
43
+ // Map from the node ids that the DOM uses internally to the node ids that clients refer to.
44
+ private internalNodeIdToClientNodeId = new Map<number, number>();
45
+
46
+ // Map from the node ids that clients refer to to the node ids that the DOM uses internally.
47
+ private clientNodeIdToInternalNodeId = new Map<number, number>();
48
+
49
+ private nextNodeId = 1;
50
+
51
+ private ipcWebsockets = new Set<WebSocket>();
52
+ private currentConnectionId = 1;
53
+ private connectionIdToWebSocketContext = new Map<
54
+ number,
55
+ { webSocket: WebSocket; messageListener: (msg: MessageEvent) => void }
56
+ >();
57
+ private webSocketToConnectionId = new Map<WebSocket, number>();
58
+ private visibleNodeIdsByConnectionId = new Map<number, Set<number>>();
59
+ private initialLoad = true;
60
+ private readonly htmlPath: string;
61
+
62
+ private disposed = false;
63
+ private ignoreTextNodes: boolean;
64
+
65
+ private documentRoot!: StaticVirtualDomElement;
66
+ private nodeIdToNode = new Map<number, StaticVirtualDomElement>();
67
+ private nodeIdToParentNodeId = new Map<number, number>();
68
+
69
+ private observableDom: ObservableDomInterface;
70
+
71
+ private documentEffectiveStartTime = Date.now();
72
+ private latestDocumentTime = 0;
73
+ private pingCounter = 1;
74
+ private maximumNodeId = 0;
75
+
76
+ private logCallback?: (message: LogMessage) => void;
77
+
78
+ constructor(
79
+ observableDomFactory: ObservableDomFactory,
80
+ htmlPath: string,
81
+ htmlContents: string,
82
+ oldInstanceDocumentRoot: StaticVirtualDomElement | null,
83
+ onLoad: (domDiff: VirtualDOMDiffStruct | null) => void,
84
+ params = {},
85
+ ignoreTextNodes = true,
86
+ logCallback?: (message: LogMessage) => void,
87
+ ) {
88
+ this.htmlPath = htmlPath;
89
+ this.ignoreTextNodes = ignoreTextNodes;
90
+
91
+ this.logCallback = logCallback || this.defaultLogCallback;
92
+
93
+ this.observableDom = observableDomFactory(
94
+ {
95
+ htmlPath,
96
+ htmlContents,
97
+ params,
98
+ ignoreTextNodes,
99
+ pingIntervalMilliseconds: 5000,
100
+ },
101
+ (message: ObservableDomMessage) => {
102
+ if (message.documentTime) {
103
+ this.documentEffectiveStartTime = Date.now() - message.documentTime;
104
+ this.latestDocumentTime = message.documentTime;
105
+ }
106
+ if (message.snapshot) {
107
+ this.documentRoot = message.snapshot;
108
+ const clonedSnapshot = JSON.parse(JSON.stringify(message.snapshot));
109
+
110
+ if (!this.initialLoad) {
111
+ throw new Error("Received snapshot after initial load");
112
+ }
113
+ this.initialLoad = false;
114
+
115
+ let domDiff: VirtualDOMDiffStruct | null = null;
116
+ if (oldInstanceDocumentRoot) {
117
+ domDiff = calculateStaticVirtualDomDiff(oldInstanceDocumentRoot, clonedSnapshot);
118
+ for (const remapping of domDiff.nodeIdRemappings) {
119
+ this.addRemappedNodeId(remapping.clientFacingNodeId, remapping.internalNodeId);
120
+ }
121
+ }
122
+
123
+ this.addAndRemapNodeFromInstance(this.documentRoot, -1);
124
+
125
+ onLoad(domDiff);
126
+ } else if (message.mutation) {
127
+ if (this.initialLoad) {
128
+ throw new Error("Received mutation before initial load");
129
+ }
130
+ const mutation = this.addKnownNodesInMutation(message.mutation);
131
+ this.processModification(mutation);
132
+ this.removeKnownNodesInMutation(mutation);
133
+ } else if (message.logMessage) {
134
+ if (this.logCallback) {
135
+ this.logCallback(message.logMessage);
136
+ }
137
+ } else {
138
+ if (message.documentTime) {
139
+ // This is just a regular ping message to update the document time - send the document time to all connected clients
140
+ this.sendPings();
141
+ return;
142
+ }
143
+ console.error("Unknown message type from observableDom", message);
144
+ }
145
+ },
146
+ );
147
+ }
148
+
149
+ private defaultLogCallback(message: LogMessage) {
150
+ const getLogFn = (level: string) => {
151
+ switch (level) {
152
+ case "system":
153
+ return console.error;
154
+ case "error":
155
+ return console.error;
156
+ case "warn":
157
+ return console.warn;
158
+ case "log":
159
+ return console.log;
160
+ case "info":
161
+ return console.info;
162
+ default:
163
+ return console.log;
164
+ }
165
+ };
166
+
167
+ const logFn = getLogFn(message.level);
168
+ logFn(`${message.level.toUpperCase()} (${this.htmlPath}):`, ...message.content);
169
+ }
170
+
171
+ private addRemappedNodeId(clientFacingNodeId: number, internalNodeId: number) {
172
+ this.internalNodeIdToClientNodeId.set(internalNodeId, clientFacingNodeId);
173
+ this.clientNodeIdToInternalNodeId.set(clientFacingNodeId, internalNodeId);
174
+ this.maximumNodeId = Math.max(this.maximumNodeId, Math.max(clientFacingNodeId, internalNodeId));
175
+ }
176
+
177
+ private sendPings() {
178
+ const ping = this.pingCounter++;
179
+ if (this.pingCounter > 1000) {
180
+ this.pingCounter = 1;
181
+ }
182
+ const pingMessage: Array<ServerMessage> = [
183
+ {
184
+ type: "ping",
185
+ ping,
186
+ documentTime: this.getDocumentTime(),
187
+ },
188
+ ];
189
+ const stringified = JSON.stringify(pingMessage);
190
+ this.connectionIdToWebSocketContext.forEach((webSocketContext) => {
191
+ webSocketContext.webSocket.send(stringified);
192
+ });
193
+ }
194
+
195
+ private getInitialSnapshot(
196
+ connectionId: number,
197
+ documentVirtualDomElement: StaticVirtualDomElement,
198
+ ): SnapshotMessage {
199
+ const visibleNodesForConnection = this.visibleNodeIdsByConnectionId.get(connectionId);
200
+ if (!visibleNodesForConnection) {
201
+ const err = new Error(
202
+ `visibleNodesForConnection not found for connectionId in getInitialSnapshot: ${connectionId}`,
203
+ );
204
+ console.error(err);
205
+ throw err;
206
+ }
207
+ const domSnapshot: NodeDescription | null = describeNodeWithChildrenForConnectionId(
208
+ documentVirtualDomElement,
209
+ connectionId,
210
+ visibleNodesForConnection,
211
+ );
212
+ if (!domSnapshot) {
213
+ throw new Error(`domSnapshot was not generated`);
214
+ }
215
+ return {
216
+ type: "snapshot",
217
+ snapshot: domSnapshot,
218
+ documentTime: Date.now() - this.documentEffectiveStartTime,
219
+ };
220
+ }
221
+
222
+ public getDocumentTime(): number {
223
+ return this.latestDocumentTime;
224
+ }
225
+
226
+ public addExistingWebsockets(
227
+ websockets: Array<WebSocket>,
228
+ existingWebsocketMap: Map<WebSocket, number> | null,
229
+ domDiff: VirtualDOMDiffStruct | null,
230
+ ) {
231
+ const connectionIds = [];
232
+ for (const websocket of websockets) {
233
+ let existingId = null;
234
+ if (existingWebsocketMap !== null) {
235
+ existingId = existingWebsocketMap.get(websocket);
236
+ }
237
+ const { connectionId } = this.registerWebsocket(websocket, existingId);
238
+ connectionIds.push(connectionId);
239
+ }
240
+
241
+ if (domDiff) {
242
+ const diffsByConnectionId = new Map<number, Array<Diff>>(
243
+ connectionIds.map((connectionId) => [connectionId, []]),
244
+ );
245
+
246
+ // Each of the websockets needs to have the original state of the document re-applied to it to determine visible
247
+ // nodes, but not sent (they already have the old version of the document as their state).
248
+ for (const connectionId of connectionIds) {
249
+ // Ignore the return value - the side effect is that the visible nodes for the connection are set
250
+ this.getInitialSnapshot(connectionId, domDiff.originalState);
251
+ }
252
+
253
+ for (const virtualDOMDiff of domDiff.virtualDOMDiffs) {
254
+ // Convert the diff of the virtual dom data structure to a MutationRecord-like diff and then handle it as if it were a MutationRecord
255
+ // The difficulty here is that the JSON diff is typed by add/remove/replace of elements of a hierarchy specified by paths, but MutationRecords are specified by type of operation and nodeIds
256
+
257
+ const mutationRecordLikes = virtualDOMDiffToVirtualDOMMutationRecord(
258
+ domDiff.originalState,
259
+ virtualDOMDiff,
260
+ );
261
+
262
+ const patchResults = applyPatch(domDiff.originalState, [virtualDOMDiff]);
263
+ for (const patchResult of patchResults) {
264
+ if (patchResult !== null) {
265
+ console.error("Patching virtual dom structure resulted in error", patchResult);
266
+ throw patchResult;
267
+ }
268
+ }
269
+
270
+ for (const mutationRecordLike of mutationRecordLikes) {
271
+ const targetNodeId = mutationRecordLike.target.nodeId;
272
+ const virtualElementParent = findParentNodeOfNodeId(domDiff.originalState, targetNodeId);
273
+ if (!virtualElementParent) {
274
+ throw new Error(`could not find parent node of nodeId ${targetNodeId}`);
275
+ }
276
+
277
+ diffsByConnectionId.forEach((diffs, connectionId) => {
278
+ const mutationDiff = diffFromApplicationOfStaticVirtualDomMutationRecordToConnection(
279
+ mutationRecordLike,
280
+ virtualElementParent,
281
+ connectionId,
282
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
283
+ this.visibleNodeIdsByConnectionId.get(connectionId)!,
284
+ );
285
+ if (mutationDiff) {
286
+ diffs.push(mutationDiff);
287
+ }
288
+ });
289
+ }
290
+ }
291
+
292
+ diffsByConnectionId.forEach((diffs, connectionId) => {
293
+ if (diffs.length === 0) {
294
+ // Need to add an "empty" message to allow passing the document time to the client
295
+ diffs.push({
296
+ type: "childrenChanged",
297
+ nodeId: this.documentRoot.nodeId,
298
+ previousNodeId: null,
299
+ addedNodes: [],
300
+ removedNodes: [],
301
+ });
302
+ }
303
+ const asServerMessages: Array<ServerMessage> = diffs;
304
+ const firstDiff = diffs[0];
305
+ firstDiff.documentTime = this.getDocumentTime();
306
+ const serializedDiffs = JSON.stringify(asServerMessages, null, 4);
307
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
308
+ if (!webSocketContext) {
309
+ throw new Error(`webSocketContext not found in addExistingWebsockets`);
310
+ }
311
+ webSocketContext.webSocket.send(serializedDiffs);
312
+ });
313
+ } else {
314
+ const documentVirtualDomElement = this.documentRoot;
315
+ if (!documentVirtualDomElement) {
316
+ throw new Error(`documentVirtualDomElement not found in getInitialSnapshot`);
317
+ }
318
+ for (const connectionId of connectionIds) {
319
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
320
+ if (!webSocketContext) {
321
+ throw new Error(`webSocketContext not found in addExistingWebsockets`);
322
+ }
323
+ const asServerMessages: Array<ServerMessage> = [
324
+ this.getInitialSnapshot(connectionId, documentVirtualDomElement),
325
+ ];
326
+ const serializedSnapshotMessage = JSON.stringify(asServerMessages);
327
+ webSocketContext.webSocket.send(serializedSnapshotMessage);
328
+ }
329
+ }
330
+
331
+ for (const connectionId of connectionIds) {
332
+ this.observableDom.addConnectedUserId(connectionId);
333
+ }
334
+ }
335
+
336
+ private findParentNodeOfNodeId(targetNodeId: number): StaticVirtualDomElement | null {
337
+ const parentNodeId = this.nodeIdToParentNodeId.get(targetNodeId);
338
+ if (parentNodeId === undefined) {
339
+ throw new Error("Parent node ID not found");
340
+ }
341
+ return this.getStaticVirtualDomElementByInternalNodeIdOrThrow(parentNodeId);
342
+ }
343
+
344
+ private registerWebsocket(
345
+ webSocket: WebSocket,
346
+ existingConnectionId: number | null = null,
347
+ ): { connectionId: number } {
348
+ let connectionId: number;
349
+ if (existingConnectionId !== null) {
350
+ connectionId = existingConnectionId;
351
+ this.currentConnectionId = Math.max(this.currentConnectionId, connectionId + 1);
352
+ } else {
353
+ connectionId = this.currentConnectionId++;
354
+ }
355
+ const webSocketContext = {
356
+ webSocket,
357
+ messageListener: (msg: MessageEvent) => {
358
+ const string = String(msg.data);
359
+ let parsed;
360
+ try {
361
+ parsed = JSON.parse(string) as ClientMessage;
362
+ } catch (e) {
363
+ console.error(`Error parsing message from websocket: ${string}`);
364
+ console.trace();
365
+ return;
366
+ }
367
+
368
+ if (NetworkedDOM.IsPongMessage(parsed)) {
369
+ // Ignore pongs for now
370
+ return;
371
+ }
372
+
373
+ this.dispatchRemoteEvent(webSocket, parsed);
374
+ },
375
+ };
376
+ this.connectionIdToWebSocketContext.set(connectionId, webSocketContext);
377
+ this.visibleNodeIdsByConnectionId.set(connectionId, new Set());
378
+ this.webSocketToConnectionId.set(webSocket, connectionId);
379
+ webSocket.addEventListener("message", webSocketContext.messageListener);
380
+ return { connectionId };
381
+ }
382
+
383
+ public addIPCWebSocket(webSocket: WebSocket) {
384
+ this.ipcWebsockets.add(webSocket);
385
+ webSocket.addEventListener("close", () => {
386
+ this.ipcWebsockets.delete(webSocket);
387
+ });
388
+
389
+ this.observableDom.addIPCWebsocket(webSocket);
390
+ }
391
+
392
+ public static handleWebsocketSubprotocol(protocols: Set<string> | Array<string>): string | false {
393
+ const protocolsSet = new Set(protocols);
394
+ // Find highest priority (first in the array) protocol that is supported
395
+ for (const protocol of NetworkedDOM.SupportedWebsocketSubProtocolsPreferenceOrder) {
396
+ if (protocolsSet.has(protocol)) {
397
+ return protocol;
398
+ }
399
+ }
400
+ return false;
401
+ }
402
+
403
+ public addWebSocket(webSocket: WebSocket): void {
404
+ if (this.initialLoad) {
405
+ throw new Error("addWebSocket called before initial load - unsupported at this time");
406
+ }
407
+ if (this.disposed) {
408
+ console.error("addWebSocket called on disposed NetworkedDOM");
409
+ throw new Error("This NetworkedDOM has been disposed");
410
+ }
411
+
412
+ if (webSocket.protocol) {
413
+ if (
414
+ NetworkedDOM.SupportedWebsocketSubProtocolsPreferenceOrder.indexOf(webSocket.protocol) ===
415
+ -1
416
+ ) {
417
+ const errorMessageString = `Unsupported websocket subprotocol: ${webSocket.protocol}`;
418
+ const errorMessage: Array<ServerMessage> = [
419
+ {
420
+ type: "error",
421
+ message: errorMessageString,
422
+ },
423
+ ];
424
+ webSocket.send(JSON.stringify(errorMessage));
425
+ webSocket.close();
426
+ return;
427
+ }
428
+ } else {
429
+ // TODO - Revisit the default handling of non-protocol websockets. It is easier to debug if a lack of protocol results in an error.
430
+ // Assume for now that this client is a legacy MML client that doesn't send a protocol, but send a warning to the client to encourage specifying a protocol
431
+ const warningMessageString = `No websocket subprotocol specified. Please specify a subprotocol to ensure compatibility with networked-dom servers. Assuming subprotocol "${defaultWebsocketSubProtocol}" for this connection.`;
432
+ const warningMessage: Array<ServerMessage> = [
433
+ {
434
+ type: "warning",
435
+ message: warningMessageString,
436
+ },
437
+ ];
438
+ webSocket.send(JSON.stringify(warningMessage));
439
+ }
440
+
441
+ const { connectionId } = this.registerWebsocket(webSocket);
442
+ const documentVirtualDomElement = this.documentRoot;
443
+ if (!documentVirtualDomElement) {
444
+ throw new Error(`documentVirtualDomElement not found in getInitialSnapshot`);
445
+ }
446
+ const asServerMessages: Array<ServerMessage> = [
447
+ this.getInitialSnapshot(connectionId, documentVirtualDomElement),
448
+ ];
449
+ const serializedSnapshotMessage = JSON.stringify(asServerMessages);
450
+ webSocket.send(serializedSnapshotMessage);
451
+ this.observableDom.addConnectedUserId(connectionId);
452
+ }
453
+
454
+ public removeWebSocket(webSocket: WebSocket): void {
455
+ const connectionId = this.webSocketToConnectionId.get(webSocket);
456
+ if (!connectionId) {
457
+ return;
458
+ }
459
+ this.observableDom.removeConnectedUserId(connectionId);
460
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
461
+ if (!webSocketContext) {
462
+ throw new Error("Missing context for websocket");
463
+ }
464
+ webSocket.removeEventListener("message", webSocketContext.messageListener);
465
+ this.connectionIdToWebSocketContext.delete(connectionId);
466
+ this.visibleNodeIdsByConnectionId.delete(connectionId);
467
+ this.webSocketToConnectionId.delete(webSocket);
468
+ }
469
+
470
+ public dispose(): void {
471
+ this.disposed = true;
472
+ for (const [, connectionId] of this.webSocketToConnectionId) {
473
+ this.observableDom.removeConnectedUserId(connectionId);
474
+ }
475
+
476
+ // Handle all of the remaining mutations that the disconnections could have caused
477
+ this.observableDom.dispose();
478
+
479
+ for (const [webSocket, connectionId] of this.webSocketToConnectionId) {
480
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
481
+ if (!webSocketContext) {
482
+ throw new Error("Missing context for websocket");
483
+ }
484
+ webSocket.removeEventListener("message", webSocketContext.messageListener);
485
+ this.connectionIdToWebSocketContext.delete(connectionId);
486
+ this.visibleNodeIdsByConnectionId.delete(connectionId);
487
+ this.webSocketToConnectionId.delete(webSocket);
488
+ }
489
+
490
+ for (const ipcWebsocket of this.ipcWebsockets) {
491
+ ipcWebsocket.close();
492
+ }
493
+ }
494
+
495
+ private processModification(mutationRecord: StaticVirtualDomMutationRecord): void {
496
+ const documentVirtualDomElement = this.documentRoot;
497
+ if (!documentVirtualDomElement) {
498
+ throw new Error(`document not created in processModification`);
499
+ }
500
+
501
+ for (const [, visibleNodesForConnection] of this.visibleNodeIdsByConnectionId) {
502
+ visibleNodesForConnection.add(documentVirtualDomElement.nodeId);
503
+ }
504
+
505
+ const diffsByConnectionId = new Map<number, Array<Diff>>(
506
+ Array.from(this.connectionIdToWebSocketContext.keys()).map((connectionId) => [
507
+ connectionId,
508
+ [],
509
+ ]),
510
+ );
511
+
512
+ diffsByConnectionId.forEach((diffs, connectionId) => {
513
+ const parentNode = this.findParentNodeOfNodeId(mutationRecord.target.nodeId);
514
+ if (mutationRecord.type === "attributes" && !parentNode) {
515
+ console.error("parentNode not found for attribute mutationRecord", mutationRecord);
516
+ console.error("this.documentRoot", JSON.stringify(this.documentRoot, null, 2));
517
+ }
518
+ const diff = diffFromApplicationOfStaticVirtualDomMutationRecordToConnection(
519
+ mutationRecord,
520
+ parentNode,
521
+ connectionId,
522
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
523
+ this.visibleNodeIdsByConnectionId.get(connectionId)!,
524
+ );
525
+ if (diff) {
526
+ diffs.push(diff);
527
+ }
528
+ });
529
+
530
+ diffsByConnectionId.forEach((diffs, connectionId) => {
531
+ if (diffs.length > 0) {
532
+ const asServerMessages: Array<ServerMessage> = diffs;
533
+ const serializedDiffs = JSON.stringify(asServerMessages, null, 4);
534
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
535
+ if (!webSocketContext) {
536
+ throw new Error(`webSocketContext not found in processModificationList`);
537
+ }
538
+ webSocketContext.webSocket.send(serializedDiffs);
539
+ }
540
+ });
541
+ }
542
+
543
+ private removeKnownNodesInMutation(mutation: StaticVirtualDomMutationRecord): void {
544
+ const virtualDomElement = mutation.target;
545
+ if (mutation.type === "childList") {
546
+ mutation.removedNodes.forEach((childDomElement: StaticVirtualDomElement) => {
547
+ this.removeVirtualDomElement(childDomElement);
548
+ const index = virtualDomElement.childNodes.indexOf(childDomElement);
549
+ virtualDomElement.childNodes.splice(index, 1);
550
+ });
551
+ return;
552
+ }
553
+ }
554
+
555
+ private removeVirtualDomElement(virtualDomElement: StaticVirtualDomElement): void {
556
+ this.nodeIdToNode.delete(virtualDomElement.nodeId);
557
+ this.nodeIdToParentNodeId.delete(virtualDomElement.nodeId);
558
+ for (const child of virtualDomElement.childNodes) {
559
+ this.removeVirtualDomElement(child);
560
+ }
561
+ }
562
+
563
+ static IsPongMessage(message: ClientMessage): message is PongMessage {
564
+ return (message as PongMessage).type === "pong";
565
+ }
566
+
567
+ private dispatchRemoteEvent(webSocket: WebSocket, remoteEvent: RemoteEvent): void {
568
+ if (this.disposed) {
569
+ console.error("Cannot dispatch remote event after dispose");
570
+ throw new Error("This NetworkedDOM has been disposed");
571
+ }
572
+
573
+ const connectionId = this.webSocketToConnectionId.get(webSocket);
574
+ if (!connectionId) {
575
+ console.error("Unknown web socket dispatched event:", webSocket);
576
+ return;
577
+ }
578
+
579
+ const remappedNode = this.clientNodeIdToInternalNodeId.get(remoteEvent.nodeId);
580
+ if (remappedNode) {
581
+ remoteEvent.nodeId = remappedNode;
582
+ }
583
+
584
+ const visibleNodes = this.visibleNodeIdsByConnectionId.get(connectionId);
585
+ if (!visibleNodes) {
586
+ console.error("No visible nodes for connection: " + connectionId);
587
+ return;
588
+ }
589
+
590
+ if (!visibleNodes.has(remoteEvent.nodeId)) {
591
+ // TODO - do a pass through the hierarchy to determine if this node should be visible to this connection id to prevent clients submitting events for nodes they can't (currently) see
592
+ console.error("Node not visible for connection: " + remoteEvent.nodeId);
593
+ return;
594
+ }
595
+
596
+ this.observableDom.dispatchRemoteEventFromConnectionId(connectionId, remoteEvent);
597
+ }
598
+
599
+ private getStaticVirtualDomElementByInternalNodeIdOrThrow(
600
+ internalNodeId: number,
601
+ ): StaticVirtualDomElement {
602
+ const remappedId = this.internalNodeIdToClientNodeId.get(internalNodeId);
603
+ if (remappedId !== undefined) {
604
+ const node = this.nodeIdToNode.get(remappedId);
605
+ if (!node) {
606
+ throw new Error("Remapped node not found with nodeId " + remappedId);
607
+ }
608
+ return node;
609
+ }
610
+ const node = this.nodeIdToNode.get(internalNodeId);
611
+ if (!node) {
612
+ throw new Error("Node not found with nodeId:" + internalNodeId);
613
+ }
614
+ return node;
615
+ }
616
+
617
+ private addKnownNodesInMutation(
618
+ mutation: StaticVirtualDomMutationIdsRecord,
619
+ ): StaticVirtualDomMutationRecord {
620
+ const target = this.getStaticVirtualDomElementByInternalNodeIdOrThrow(mutation.targetId);
621
+
622
+ // TODO - avoid mutation in this conversion - use the attribute pair in the handling (would require changing StaticVirtualDomMutationRecord.attributeName to be the key/value pair).
623
+ if (mutation.attribute) {
624
+ if (mutation.attribute.value !== null) {
625
+ target.attributes[mutation.attribute.attributeName] = mutation.attribute.value;
626
+ } else {
627
+ delete target.attributes[mutation.attribute.attributeName];
628
+ }
629
+ }
630
+
631
+ const previousSibling = mutation.previousSiblingId
632
+ ? this.getStaticVirtualDomElementByInternalNodeIdOrThrow(mutation.previousSiblingId)
633
+ : null;
634
+
635
+ if (mutation.type === "childList") {
636
+ let index = 0;
637
+ if (previousSibling) {
638
+ index = target.childNodes.indexOf(previousSibling);
639
+ if (index === -1) {
640
+ throw new Error("Previous sibling is not currently a child of the parent element");
641
+ }
642
+ index += 1;
643
+ }
644
+ mutation.addedNodes.forEach((childVirtualDomElement: StaticVirtualDomElement) => {
645
+ this.addAndRemapNodeFromInstance(childVirtualDomElement, target.nodeId);
646
+
647
+ if (target.childNodes.indexOf(childVirtualDomElement) === -1) {
648
+ target.childNodes.splice(index, 0, childVirtualDomElement);
649
+ index++;
650
+ }
651
+ });
652
+ } else if (mutation.type === "attributes") {
653
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
654
+ const attributePair = mutation.attribute!;
655
+ if (attributePair.value === null) {
656
+ delete target.attributes[attributePair.attributeName];
657
+ } else {
658
+ target.attributes[attributePair.attributeName] = attributePair.value;
659
+ }
660
+ } else if (mutation.type === "characterData") {
661
+ // TODO - reimplement characterData
662
+ throw new Error("characterData not supported");
663
+ // virtualDomElement.textContent = targetNode.textContent ? targetNode.textContent : undefined;
664
+ }
665
+
666
+ const record: StaticVirtualDomMutationRecord = {
667
+ type: mutation.type,
668
+ target,
669
+ addedNodes: mutation.addedNodes,
670
+ removedNodes: mutation.removedNodeIds.map((nodeId) => {
671
+ return this.getStaticVirtualDomElementByInternalNodeIdOrThrow(nodeId);
672
+ }),
673
+ previousSibling: mutation.previousSiblingId
674
+ ? this.getStaticVirtualDomElementByInternalNodeIdOrThrow(mutation.previousSiblingId)
675
+ : null,
676
+ attributeName: mutation.attribute ? mutation.attribute.attributeName : null,
677
+ };
678
+
679
+ return record;
680
+ }
681
+
682
+ getSnapshot(): StaticVirtualDomElement {
683
+ return this.documentRoot;
684
+ }
685
+
686
+ private addAndRemapNodeFromInstance(node: StaticVirtualDomElement, parentNodeId: number) {
687
+ const remappedNodeId = this.internalNodeIdToClientNodeId.get(node.nodeId);
688
+ if (remappedNodeId !== undefined) {
689
+ node.nodeId = remappedNodeId;
690
+ } else {
691
+ // This id might already refer to a node in this client's view. If so, we need to remap it to a new id.
692
+ const existingClientReference = this.clientNodeIdToInternalNodeId.get(node.nodeId);
693
+ if (existingClientReference) {
694
+ const newNodeId = ++this.maximumNodeId;
695
+ this.addRemappedNodeId(newNodeId, node.nodeId);
696
+ node.nodeId = newNodeId;
697
+ }
698
+ }
699
+
700
+ if (this.nodeIdToNode.has(node.nodeId)) {
701
+ throw new Error("Node already exists with id " + node.nodeId);
702
+ }
703
+
704
+ this.nodeIdToNode.set(node.nodeId, node);
705
+ this.nodeIdToParentNodeId.set(node.nodeId, parentNodeId);
706
+ this.maximumNodeId = Math.max(this.maximumNodeId, node.nodeId);
707
+
708
+ for (const childNode of node.childNodes) {
709
+ this.addAndRemapNodeFromInstance(childNode, node.nodeId);
710
+ }
711
+ }
712
+
713
+ public getWebsocketConnectionIdMap() {
714
+ // return a clone of the map
715
+ return new Map(this.webSocketToConnectionId);
716
+ }
717
+ }