@mml-io/networked-dom-document 0.2.0 → 0.4.0

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,18 @@
1
+ import { LogMessage } from "@mml-io/observable-dom-common";
2
+ import { ObservableDOMFactory } from "./NetworkedDOM";
3
+ export declare class EditableNetworkedDOM {
4
+ private htmlPath;
5
+ private params;
6
+ private websockets;
7
+ private loadedState;
8
+ private observableDOMFactory;
9
+ private ignoreTextNodes;
10
+ private logCallback?;
11
+ constructor(htmlPath: string, observableDOMFactory: ObservableDOMFactory, ignoreTextNodes?: boolean, logCallback?: (message: LogMessage) => void);
12
+ isLoaded(): boolean;
13
+ load(htmlContents: string, params?: object): void;
14
+ reload(): void;
15
+ dispose(): void;
16
+ addWebSocket(webSocket: WebSocket): void;
17
+ removeWebSocket(webSocket: WebSocket): void;
18
+ }
@@ -0,0 +1,51 @@
1
+ import { ClientMessage, PongMessage } from "@mml-io/networked-dom-protocol";
2
+ import { LogMessage, ObservableDOMInterface, ObservableDOMMessage, ObservableDOMParameters, StaticVirtualDOMElement } from "@mml-io/observable-dom-common";
3
+ import { VirtualDOMDiffStruct } from "./common";
4
+ export declare const networkedDOMProtocolSubProtocol_v0_1 = "networked-dom-v0.1";
5
+ export declare const defaultWebsocketSubProtocol = "networked-dom-v0.1";
6
+ export type ObservableDOMFactory = (observableDOMParameters: ObservableDOMParameters, callback: (message: ObservableDOMMessage, observableDOM: ObservableDOMInterface) => void) => ObservableDOMInterface;
7
+ export declare class NetworkedDOM {
8
+ static SupportedWebsocketSubProtocolsPreferenceOrder: string[];
9
+ private internalNodeIdToClientNodeId;
10
+ private clientNodeIdToInternalNodeId;
11
+ private currentConnectionId;
12
+ private connectionIdToWebSocketContext;
13
+ private webSocketToConnectionId;
14
+ private visibleNodeIdsByConnectionId;
15
+ private initialLoad;
16
+ private readonly htmlPath;
17
+ private disposed;
18
+ private ignoreTextNodes;
19
+ private documentRoot;
20
+ private nodeIdToNode;
21
+ private nodeIdToParentNodeId;
22
+ private observableDOM;
23
+ private documentEffectiveStartTime;
24
+ private latestDocumentTime;
25
+ private pingCounter;
26
+ private maximumNodeId;
27
+ private logCallback?;
28
+ constructor(observableDOMFactory: ObservableDOMFactory, htmlPath: string, htmlContents: string, oldInstanceDocumentRoot: StaticVirtualDOMElement | null, onLoad: (domDiff: VirtualDOMDiffStruct | null, networkedDOM: NetworkedDOM) => void, params?: {}, ignoreTextNodes?: boolean, logCallback?: (message: LogMessage) => void);
29
+ private defaultLogCallback;
30
+ private addRemappedNodeId;
31
+ private sendPings;
32
+ private getInitialSnapshot;
33
+ getDocumentTime(): number;
34
+ addExistingWebsockets(websockets: Array<WebSocket>, existingWebsocketMap: Map<WebSocket, number> | null, domDiff: VirtualDOMDiffStruct | null): void;
35
+ private findParentNodeOfNodeId;
36
+ private registerWebsocket;
37
+ static handleWebsocketSubprotocol(protocols: Set<string> | Array<string>): string | false;
38
+ addWebSocket(webSocket: WebSocket): void;
39
+ removeWebSocket(webSocket: WebSocket): void;
40
+ dispose(): void;
41
+ private processModification;
42
+ private removeKnownNodesInMutation;
43
+ private removeVirtualDOMElement;
44
+ static IsPongMessage(message: ClientMessage): message is PongMessage;
45
+ private dispatchRemoteEvent;
46
+ private getStaticVirtualDOMElementByInternalNodeIdOrThrow;
47
+ private addKnownNodesInMutation;
48
+ getSnapshot(): StaticVirtualDOMElement;
49
+ private addAndRemapNodeFromInstance;
50
+ getWebsocketConnectionIdMap(): Map<WebSocket, number>;
51
+ }
@@ -0,0 +1,19 @@
1
+ import { StaticVirtualDOMElement } from "@mml-io/observable-dom-common";
2
+ import * as rfc6902 from "rfc6902";
3
+ export type NodeMapping = {
4
+ clientFacingNodeId: number;
5
+ internalNodeId: number;
6
+ };
7
+ export type VirtualDOMDiffStruct = {
8
+ originalState: StaticVirtualDOMElement;
9
+ nodeIdRemappings: Array<NodeMapping>;
10
+ virtualDOMDiffs: Array<rfc6902.Operation>;
11
+ };
12
+ export type StaticVirtualDOMMutationRecord = {
13
+ type: "attributes" | "characterData" | "childList" | "snapshot";
14
+ target: StaticVirtualDOMElement;
15
+ addedNodes: Array<StaticVirtualDOMElement>;
16
+ removedNodes: Array<StaticVirtualDOMElement>;
17
+ previousSibling: StaticVirtualDOMElement | null;
18
+ attributeName: string | null;
19
+ };
@@ -0,0 +1,11 @@
1
+ import { Diff, NodeDescription } from "@mml-io/networked-dom-protocol";
2
+ import { StaticVirtualDOMElement } from "@mml-io/observable-dom-common";
3
+ import * as rfc6902 from "rfc6902";
4
+ import { StaticVirtualDOMMutationRecord, VirtualDOMDiffStruct } from "./common";
5
+ export declare const visibleToAttrName = "visible-to";
6
+ export declare const hiddenFromAttrName = "hidden-from";
7
+ export declare function diffFromApplicationOfStaticVirtualDOMMutationRecordToConnection(mutation: StaticVirtualDOMMutationRecord, parentNode: StaticVirtualDOMElement | null, connectionId: number, visibleNodesForConnection: Set<number>): Diff | null;
8
+ export declare function describeNodeWithChildrenForConnectionId(virtualDOMElement: StaticVirtualDOMElement, connectionId: number, visibleNodesForConnection: Set<number>): NodeDescription | null;
9
+ export declare function findParentNodeOfNodeId(virtualDOMElement: StaticVirtualDOMElement, targetNodeId: number): StaticVirtualDOMElement | null;
10
+ export declare function virtualDOMDiffToVirtualDOMMutationRecord(virtualStructure: StaticVirtualDOMElement, domDiff: rfc6902.Operation): Array<StaticVirtualDOMMutationRecord>;
11
+ export declare function calculateStaticVirtualDOMDiff(originalState: StaticVirtualDOMElement, latestState: StaticVirtualDOMElement): VirtualDOMDiffStruct;
package/build/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export * from "../src/index";
1
+ export * from "./NetworkedDOM";
2
+ export * from "./EditableNetworkedDOM";
package/build/index.js CHANGED
@@ -45,125 +45,143 @@ var visibleToAttrName = "visible-to";
45
45
  var hiddenFromAttrName = "hidden-from";
46
46
  function diffFromApplicationOfStaticVirtualDOMMutationRecordToConnection(mutation, parentNode, connectionId, visibleNodesForConnection) {
47
47
  const virtualDOMElement = mutation.target;
48
- if (mutation.type === "attributes") {
49
- const visible = visibleNodesForConnection.has(virtualDOMElement.nodeId);
50
- if (!parentNode) {
51
- throw new Error("Node has no parent");
52
- }
53
- const parentNodeId = parentNode.nodeId;
54
- const shouldBeVisible = shouldShowNodeToConnectionId(virtualDOMElement, connectionId) && visibleNodesForConnection.has(parentNodeId);
55
- const attributeName = mutation.attributeName;
56
- if (visible && shouldBeVisible) {
57
- let newValue = null;
58
- if (virtualDOMElement.attributes[attributeName] !== void 0) {
59
- newValue = virtualDOMElement.attributes[attributeName];
60
- }
61
- const diff = {
62
- type: "attributeChange",
63
- nodeId: virtualDOMElement.nodeId,
64
- attribute: attributeName,
65
- newValue
66
- };
67
- return diff;
68
- } else if (!visible && shouldBeVisible) {
69
- visibleNodesForConnection.add(virtualDOMElement.nodeId);
70
- const index = parentNode.childNodes.indexOf(virtualDOMElement);
71
- if (index === -1) {
72
- throw new Error("Node not found in parent's children");
73
- }
74
- let previousNodeId = null;
75
- if (index > 0) {
76
- previousNodeId = getNodeIdOfPreviousVisibleSibling(
77
- parentNode,
78
- index - 1,
79
- visibleNodesForConnection
80
- );
81
- }
48
+ switch (mutation.type) {
49
+ case "snapshot": {
82
50
  const nodeDescription = describeNodeWithChildrenForConnectionId(
83
51
  virtualDOMElement,
84
52
  connectionId,
85
53
  visibleNodesForConnection
86
54
  );
87
55
  if (!nodeDescription) {
88
- throw new Error("Node description not found");
56
+ return null;
89
57
  }
90
58
  const diff = {
91
- type: "childrenChanged",
92
- nodeId: parentNodeId,
93
- previousNodeId,
94
- addedNodes: [nodeDescription],
95
- removedNodes: []
59
+ type: "snapshot",
60
+ snapshot: nodeDescription,
61
+ documentTime: 0
96
62
  };
97
63
  return diff;
98
- } else if (visible && !shouldBeVisible) {
99
- removeNodeAndChildrenFromVisibleNodes(virtualDOMElement, visibleNodesForConnection);
64
+ }
65
+ case "attributes": {
66
+ const visible = visibleNodesForConnection.has(virtualDOMElement.nodeId);
67
+ if (!parentNode) {
68
+ throw new Error("Node has no parent");
69
+ }
70
+ const parentNodeId = parentNode.nodeId;
71
+ const shouldBeVisible = shouldShowNodeToConnectionId(virtualDOMElement, connectionId) && visibleNodesForConnection.has(parentNodeId);
72
+ const attributeName = mutation.attributeName;
73
+ if (visible && shouldBeVisible) {
74
+ let newValue = null;
75
+ if (virtualDOMElement.attributes[attributeName] !== void 0) {
76
+ newValue = virtualDOMElement.attributes[attributeName];
77
+ }
78
+ const diff = {
79
+ type: "attributeChange",
80
+ nodeId: virtualDOMElement.nodeId,
81
+ attribute: attributeName,
82
+ newValue
83
+ };
84
+ return diff;
85
+ } else if (!visible && shouldBeVisible) {
86
+ visibleNodesForConnection.add(virtualDOMElement.nodeId);
87
+ const index = parentNode.childNodes.indexOf(virtualDOMElement);
88
+ if (index === -1) {
89
+ throw new Error("Node not found in parent's children");
90
+ }
91
+ let previousNodeId = null;
92
+ if (index > 0) {
93
+ previousNodeId = getNodeIdOfPreviousVisibleSibling(
94
+ parentNode,
95
+ index - 1,
96
+ visibleNodesForConnection
97
+ );
98
+ }
99
+ const nodeDescription = describeNodeWithChildrenForConnectionId(
100
+ virtualDOMElement,
101
+ connectionId,
102
+ visibleNodesForConnection
103
+ );
104
+ if (!nodeDescription) {
105
+ throw new Error("Node description not found");
106
+ }
107
+ const diff = {
108
+ type: "childrenChanged",
109
+ nodeId: parentNodeId,
110
+ previousNodeId,
111
+ addedNodes: [nodeDescription],
112
+ removedNodes: []
113
+ };
114
+ return diff;
115
+ } else if (visible && !shouldBeVisible) {
116
+ removeNodeAndChildrenFromVisibleNodes(virtualDOMElement, visibleNodesForConnection);
117
+ const diff = {
118
+ type: "childrenChanged",
119
+ nodeId: parentNodeId,
120
+ previousNodeId: null,
121
+ addedNodes: [],
122
+ removedNodes: [virtualDOMElement.nodeId]
123
+ };
124
+ return diff;
125
+ } else if (!visible && !shouldBeVisible) {
126
+ return null;
127
+ }
128
+ break;
129
+ }
130
+ case "characterData": {
100
131
  const diff = {
101
- type: "childrenChanged",
102
- nodeId: parentNodeId,
103
- previousNodeId: null,
104
- addedNodes: [],
105
- removedNodes: [virtualDOMElement.nodeId]
132
+ type: "textChanged",
133
+ nodeId: virtualDOMElement.nodeId,
134
+ text: virtualDOMElement.textContent || ""
106
135
  };
107
136
  return diff;
108
- } else if (!visible && !shouldBeVisible) {
109
- return null;
110
137
  }
111
- }
112
- if (!visibleNodesForConnection.has(virtualDOMElement.nodeId)) {
113
- return null;
114
- }
115
- if (mutation.type === "characterData") {
116
- const diff = {
117
- type: "textChanged",
118
- nodeId: virtualDOMElement.nodeId,
119
- text: virtualDOMElement.textContent || ""
120
- };
121
- return diff;
122
- }
123
- if (mutation.type === "childList") {
124
- let previousSibling = mutation.previousSibling;
125
- let previousNodeId = null;
126
- if (previousSibling) {
127
- let previousIndex = virtualDOMElement.childNodes.indexOf(previousSibling);
128
- while (previousIndex !== -1) {
129
- previousSibling = virtualDOMElement.childNodes[previousIndex];
130
- if (visibleNodesForConnection.has(previousSibling.nodeId)) {
131
- previousNodeId = previousSibling.nodeId;
132
- break;
138
+ case "childList": {
139
+ let previousSibling = mutation.previousSibling;
140
+ let previousNodeId = null;
141
+ if (previousSibling) {
142
+ let previousIndex = virtualDOMElement.childNodes.indexOf(previousSibling);
143
+ while (previousIndex !== -1) {
144
+ previousSibling = virtualDOMElement.childNodes[previousIndex];
145
+ if (visibleNodesForConnection.has(previousSibling.nodeId)) {
146
+ previousNodeId = previousSibling.nodeId;
147
+ break;
148
+ }
149
+ previousIndex--;
133
150
  }
134
- previousIndex--;
135
151
  }
136
- }
137
- const diff = {
138
- type: "childrenChanged",
139
- nodeId: virtualDOMElement.nodeId,
140
- previousNodeId,
141
- addedNodes: [],
142
- removedNodes: []
143
- };
144
- mutation.addedNodes.forEach((childVirtualDOMElement) => {
145
- const describedNode = describeNodeWithChildrenForConnectionId(
146
- childVirtualDOMElement,
147
- connectionId,
148
- visibleNodesForConnection
149
- );
150
- if (!describedNode) {
151
- return;
152
- }
153
- diff.addedNodes.push(describedNode);
154
- });
155
- mutation.removedNodes.forEach((childVirtualDOMElement) => {
156
- if (visibleNodesForConnection.has(childVirtualDOMElement.nodeId)) {
157
- removeNodeAndChildrenFromVisibleNodes(childVirtualDOMElement, visibleNodesForConnection);
158
- diff.removedNodes.push(childVirtualDOMElement.nodeId);
152
+ const diff = {
153
+ type: "childrenChanged",
154
+ nodeId: virtualDOMElement.nodeId,
155
+ previousNodeId,
156
+ addedNodes: [],
157
+ removedNodes: []
158
+ };
159
+ mutation.addedNodes.forEach((childVirtualDOMElement) => {
160
+ const describedNode = describeNodeWithChildrenForConnectionId(
161
+ childVirtualDOMElement,
162
+ connectionId,
163
+ visibleNodesForConnection
164
+ );
165
+ if (!describedNode) {
166
+ return;
167
+ }
168
+ diff.addedNodes.push(describedNode);
169
+ });
170
+ mutation.removedNodes.forEach((childVirtualDOMElement) => {
171
+ if (visibleNodesForConnection.has(childVirtualDOMElement.nodeId)) {
172
+ removeNodeAndChildrenFromVisibleNodes(childVirtualDOMElement, visibleNodesForConnection);
173
+ diff.removedNodes.push(childVirtualDOMElement.nodeId);
174
+ }
175
+ });
176
+ if (diff.addedNodes.length > 0 || diff.removedNodes.length > 0) {
177
+ return diff;
159
178
  }
160
- });
161
- if (diff.addedNodes.length > 0 || diff.removedNodes.length > 0) {
162
- return diff;
179
+ return null;
163
180
  }
164
- return null;
181
+ default:
182
+ console.error("Unknown mutation type: " + mutation.type);
183
+ break;
165
184
  }
166
- console.error("Unknown mutation type: " + mutation.type);
167
185
  return null;
168
186
  }
169
187
  function getNodeIdOfPreviousVisibleSibling(parentVirtualElement, candidateIndex, visibleNodesForConnection) {
@@ -308,55 +326,72 @@ function virtualDOMDiffToVirtualDOMMutationRecord(virtualStructure, domDiff) {
308
326
  }
309
327
  const addedNodes = [];
310
328
  const removedNodes = [];
311
- if (domDiff.op === "add") {
312
- addedNodes.push(domDiff.value);
313
- return [
314
- {
315
- type: "childList",
316
- target: node,
317
- addedNodes,
318
- removedNodes,
319
- previousSibling,
320
- attributeName: null
321
- }
322
- ];
323
- } else if (domDiff.op === "remove") {
324
- const removedNode = pointer.get(virtualStructure);
325
- removedNodes.push(removedNode);
326
- return [
327
- {
328
- type: "childList",
329
- target: node,
330
- addedNodes,
331
- removedNodes,
332
- previousSibling,
333
- attributeName: null
334
- }
335
- ];
336
- } else if (domDiff.op === "replace") {
337
- const removedNode = pointer.get(virtualStructure);
338
- removedNodes.push(removedNode);
339
- addedNodes.push(domDiff.value);
340
- return [
341
- {
342
- type: "childList",
343
- target: node,
344
- addedNodes: [],
345
- removedNodes,
346
- previousSibling,
347
- attributeName: null
348
- },
349
- {
350
- type: "childList",
351
- target: node,
352
- addedNodes,
353
- removedNodes: [],
354
- previousSibling,
355
- attributeName: null
356
- }
357
- ];
329
+ switch (domDiff.op) {
330
+ case "add": {
331
+ addedNodes.push(domDiff.value);
332
+ return [
333
+ {
334
+ type: "childList",
335
+ target: node,
336
+ addedNodes,
337
+ removedNodes,
338
+ previousSibling,
339
+ attributeName: null
340
+ }
341
+ ];
342
+ }
343
+ case "remove": {
344
+ const removedNode = pointer.get(virtualStructure);
345
+ removedNodes.push(removedNode);
346
+ return [
347
+ {
348
+ type: "childList",
349
+ target: node,
350
+ addedNodes,
351
+ removedNodes,
352
+ previousSibling,
353
+ attributeName: null
354
+ }
355
+ ];
356
+ }
357
+ case "replace": {
358
+ const removedNode = pointer.get(virtualStructure);
359
+ removedNodes.push(removedNode);
360
+ addedNodes.push(domDiff.value);
361
+ return [
362
+ {
363
+ type: "childList",
364
+ target: node,
365
+ addedNodes: [],
366
+ removedNodes,
367
+ previousSibling,
368
+ attributeName: null
369
+ },
370
+ {
371
+ type: "childList",
372
+ target: node,
373
+ addedNodes,
374
+ removedNodes: [],
375
+ previousSibling,
376
+ attributeName: null
377
+ }
378
+ ];
379
+ }
358
380
  }
359
381
  }
382
+ if (domDiff.op === "replace" && domDiff.path === "") {
383
+ const node = domDiff.value;
384
+ return [
385
+ {
386
+ type: "snapshot",
387
+ target: node,
388
+ addedNodes: [],
389
+ removedNodes: [],
390
+ previousSibling: null,
391
+ attributeName: null
392
+ }
393
+ ];
394
+ }
360
395
  console.error("Unhandled JSON diff:", JSON.stringify(domDiff, null, 2));
361
396
  throw new Error("Unhandled diff type");
362
397
  }
@@ -641,19 +676,19 @@ var _NetworkedDOM = class {
641
676
  domDiff.originalState,
642
677
  virtualDOMDiff
643
678
  );
644
- const patchResults = (0, import_rfc6902.applyPatch)(domDiff.originalState, [virtualDOMDiff]);
645
- for (const patchResult of patchResults) {
646
- if (patchResult !== null) {
647
- console.error("Patching virtual dom structure resulted in error", patchResult);
648
- throw patchResult;
679
+ if (virtualDOMDiff.path === "" && virtualDOMDiff.op === "replace") {
680
+ } else {
681
+ const patchResults = (0, import_rfc6902.applyPatch)(domDiff.originalState, [virtualDOMDiff]);
682
+ for (const patchResult of patchResults) {
683
+ if (patchResult !== null) {
684
+ console.error("Patching virtual dom structure resulted in error", patchResult);
685
+ throw patchResult;
686
+ }
649
687
  }
650
688
  }
651
689
  for (const mutationRecordLike of mutationRecordLikes) {
652
690
  const targetNodeId = mutationRecordLike.target.nodeId;
653
691
  const virtualElementParent = findParentNodeOfNodeId(domDiff.originalState, targetNodeId);
654
- if (!virtualElementParent) {
655
- throw new Error(`could not find parent node of nodeId ${targetNodeId}`);
656
- }
657
692
  diffsByConnectionId.forEach((diffs, connectionId) => {
658
693
  const mutationDiff = diffFromApplicationOfStaticVirtualDOMMutationRecordToConnection(
659
694
  mutationRecordLike,
@@ -817,9 +852,6 @@ var _NetworkedDOM = class {
817
852
  }
818
853
  dispose() {
819
854
  this.disposed = true;
820
- for (const [, connectionId] of this.webSocketToConnectionId) {
821
- this.observableDOM.removeConnectedUserId(connectionId);
822
- }
823
855
  this.observableDOM.dispose();
824
856
  for (const [webSocket, connectionId] of this.webSocketToConnectionId) {
825
857
  const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);