@hydranium/protocol 1.0.0-next.18 → 1.0.0-next.181

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.
Files changed (193) hide show
  1. package/README.md +35 -1
  2. package/lib/abstract-logger.d.ts.map +1 -1
  3. package/lib/abstract-logger.js.map +1 -1
  4. package/lib/client/data-connection.d.ts +157 -0
  5. package/lib/client/data-connection.d.ts.map +1 -0
  6. package/lib/client/data-connection.js +236 -0
  7. package/lib/client/data-connection.js.map +1 -0
  8. package/lib/client/data-events.d.ts +13 -1
  9. package/lib/client/data-events.d.ts.map +1 -1
  10. package/lib/client/data-events.js +21 -0
  11. package/lib/client/data-events.js.map +1 -1
  12. package/lib/client/data-port.d.ts +25 -23
  13. package/lib/client/data-port.d.ts.map +1 -1
  14. package/lib/client/data-session.d.ts +440 -78
  15. package/lib/client/data-session.d.ts.map +1 -1
  16. package/lib/client/data-session.js +697 -106
  17. package/lib/client/data-session.js.map +1 -1
  18. package/lib/client/index.d.ts +13 -8
  19. package/lib/client/index.d.ts.map +1 -1
  20. package/lib/client/index.js +13 -8
  21. package/lib/client/index.js.map +1 -1
  22. package/lib/client/message-relay.d.ts +9 -3
  23. package/lib/client/message-relay.d.ts.map +1 -1
  24. package/lib/client/message-relay.js +11 -5
  25. package/lib/client/message-relay.js.map +1 -1
  26. package/lib/client/post-message-transport.d.ts +64 -3
  27. package/lib/client/post-message-transport.d.ts.map +1 -1
  28. package/lib/client/post-message-transport.js +175 -1
  29. package/lib/client/post-message-transport.js.map +1 -1
  30. package/lib/client/rpc-connection.d.ts +149 -0
  31. package/lib/client/rpc-connection.d.ts.map +1 -0
  32. package/lib/client/rpc-connection.js +202 -0
  33. package/lib/client/rpc-connection.js.map +1 -0
  34. package/lib/client-ids.d.ts +45 -0
  35. package/lib/client-ids.d.ts.map +1 -0
  36. package/lib/client-ids.js +48 -0
  37. package/lib/client-ids.js.map +1 -0
  38. package/lib/clock.d.ts +38 -0
  39. package/lib/clock.d.ts.map +1 -1
  40. package/lib/clock.js +36 -1
  41. package/lib/clock.js.map +1 -1
  42. package/lib/data/data-protocol-methods.d.ts +4 -4
  43. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  44. package/lib/data/data-protocol-methods.js +11 -1
  45. package/lib/data/data-protocol-methods.js.map +1 -1
  46. package/lib/data/data-server-protocol.d.ts +111 -31
  47. package/lib/data/data-server-protocol.d.ts.map +1 -1
  48. package/lib/data/events.d.ts +103 -14
  49. package/lib/data/events.d.ts.map +1 -1
  50. package/lib/data/requests.d.ts +67 -11
  51. package/lib/data/requests.d.ts.map +1 -1
  52. package/lib/debouncer.d.ts.map +1 -1
  53. package/lib/debouncer.js.map +1 -1
  54. package/lib/errors.d.ts +163 -17
  55. package/lib/errors.d.ts.map +1 -1
  56. package/lib/errors.js +219 -22
  57. package/lib/errors.js.map +1 -1
  58. package/lib/glsp-save-model-actions.d.ts +50 -0
  59. package/lib/glsp-save-model-actions.d.ts.map +1 -0
  60. package/lib/glsp-save-model-actions.js +28 -0
  61. package/lib/glsp-save-model-actions.js.map +1 -0
  62. package/lib/index.d.ts +3 -0
  63. package/lib/index.d.ts.map +1 -1
  64. package/lib/index.js +6 -0
  65. package/lib/index.js.map +1 -1
  66. package/lib/latency-collector.d.ts +8 -4
  67. package/lib/latency-collector.d.ts.map +1 -1
  68. package/lib/latency-collector.js.map +1 -1
  69. package/lib/logger.js.map +1 -1
  70. package/lib/messages/index.d.ts +29 -0
  71. package/lib/messages/index.d.ts.map +1 -0
  72. package/lib/messages/index.js +58 -0
  73. package/lib/messages/index.js.map +1 -0
  74. package/lib/messages/primitives.d.ts +188 -0
  75. package/lib/messages/primitives.d.ts.map +1 -0
  76. package/lib/messages/primitives.js +161 -0
  77. package/lib/messages/primitives.js.map +1 -0
  78. package/lib/model-server.d.ts +60 -13
  79. package/lib/model-server.d.ts.map +1 -1
  80. package/lib/model-server.js +4 -2
  81. package/lib/model-server.js.map +1 -1
  82. package/lib/model-service/based-on.d.ts +57 -0
  83. package/lib/model-service/based-on.d.ts.map +1 -0
  84. package/lib/model-service/based-on.js +37 -0
  85. package/lib/model-service/based-on.js.map +1 -0
  86. package/lib/model-service/index.d.ts +1 -1
  87. package/lib/model-service/index.d.ts.map +1 -1
  88. package/lib/model-service/index.js +4 -5
  89. package/lib/model-service/index.js.map +1 -1
  90. package/lib/noop-logger.d.ts.map +1 -1
  91. package/lib/noop-logger.js.map +1 -1
  92. package/lib/observable-value.js.map +1 -1
  93. package/lib/patch-merge.d.ts +28 -27
  94. package/lib/patch-merge.d.ts.map +1 -1
  95. package/lib/patch-merge.js +66 -22
  96. package/lib/patch-merge.js.map +1 -1
  97. package/lib/profile-session.d.ts +8 -4
  98. package/lib/profile-session.d.ts.map +1 -1
  99. package/lib/profile-session.js.map +1 -1
  100. package/lib/rpc/bind-rpc-methods.d.ts +29 -3
  101. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  102. package/lib/rpc/bind-rpc-methods.js +22 -3
  103. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  104. package/lib/rpc/create-rpc-proxy.d.ts +7 -0
  105. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  106. package/lib/rpc/create-rpc-proxy.js +6 -1
  107. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  108. package/lib/rpc/wire-prefix.js.map +1 -1
  109. package/lib/testing/catalogue-audit.d.ts +80 -0
  110. package/lib/testing/catalogue-audit.d.ts.map +1 -0
  111. package/lib/testing/catalogue-audit.js +94 -0
  112. package/lib/testing/catalogue-audit.js.map +1 -0
  113. package/lib/testing/data-doubles.d.ts +13 -12
  114. package/lib/testing/data-doubles.d.ts.map +1 -1
  115. package/lib/testing/data-doubles.js +19 -5
  116. package/lib/testing/data-doubles.js.map +1 -1
  117. package/lib/testing/fake-clock.d.ts +9 -1
  118. package/lib/testing/fake-clock.d.ts.map +1 -1
  119. package/lib/testing/fake-clock.js +54 -45
  120. package/lib/testing/fake-clock.js.map +1 -1
  121. package/lib/testing/index.d.ts +1 -0
  122. package/lib/testing/index.d.ts.map +1 -1
  123. package/lib/testing/index.js +4 -1
  124. package/lib/testing/index.js.map +1 -1
  125. package/lib/testing/node/duplex-connection.js.map +1 -1
  126. package/lib/testing/node/duplex-stream.js.map +1 -1
  127. package/lib/testing/node/index.d.ts +1 -0
  128. package/lib/testing/node/index.d.ts.map +1 -1
  129. package/lib/testing/node/index.js +2 -2
  130. package/lib/testing/node/index.js.map +1 -1
  131. package/lib/testing/node/message-port-pair.d.ts +25 -0
  132. package/lib/testing/node/message-port-pair.d.ts.map +1 -0
  133. package/lib/testing/node/message-port-pair.js +26 -0
  134. package/lib/testing/node/message-port-pair.js.map +1 -0
  135. package/lib/testing/wait-for.js.map +1 -1
  136. package/lib/tracer.d.ts.map +1 -1
  137. package/lib/tracer.js.map +1 -1
  138. package/lib/transfer-diagnostic.d.ts +33 -0
  139. package/lib/transfer-diagnostic.d.ts.map +1 -1
  140. package/lib/transfer-diagnostic.js +23 -0
  141. package/lib/transfer-diagnostic.js.map +1 -1
  142. package/lib/transfer-document.d.ts +34 -5
  143. package/lib/transfer-document.d.ts.map +1 -1
  144. package/lib/transfer-document.js +14 -1
  145. package/lib/transfer-document.js.map +1 -1
  146. package/lib/uri.d.ts.map +1 -1
  147. package/lib/uri.js.map +1 -1
  148. package/lib/util.d.ts +8 -0
  149. package/lib/util.d.ts.map +1 -1
  150. package/lib/util.js +32 -0
  151. package/lib/util.js.map +1 -1
  152. package/package.json +27 -18
  153. package/src/client/data-connection.ts +314 -0
  154. package/src/client/data-events.ts +33 -1
  155. package/src/client/data-port.ts +26 -24
  156. package/src/client/data-session.ts +881 -123
  157. package/src/client/index.ts +13 -8
  158. package/src/client/message-relay.ts +29 -7
  159. package/src/client/post-message-transport.ts +219 -4
  160. package/src/client/rpc-connection.ts +268 -0
  161. package/src/client-ids.ts +49 -0
  162. package/src/clock.ts +56 -0
  163. package/src/data/data-protocol-methods.ts +11 -3
  164. package/src/data/data-server-protocol.ts +132 -31
  165. package/src/data/events.ts +109 -14
  166. package/src/data/requests.ts +71 -11
  167. package/src/errors.ts +259 -24
  168. package/src/glsp-save-model-actions.ts +59 -0
  169. package/src/index.ts +6 -0
  170. package/src/latency-collector.ts +8 -3
  171. package/src/messages/index.ts +36 -0
  172. package/src/messages/primitives.ts +271 -0
  173. package/src/model-server.ts +63 -18
  174. package/src/model-service/based-on.ts +63 -0
  175. package/src/model-service/index.ts +4 -5
  176. package/src/patch-merge.ts +90 -37
  177. package/src/profile-session.ts +9 -4
  178. package/src/rpc/bind-rpc-methods.ts +49 -4
  179. package/src/rpc/create-rpc-proxy.ts +14 -1
  180. package/src/testing/catalogue-audit.ts +111 -0
  181. package/src/testing/data-doubles.ts +42 -17
  182. package/src/testing/fake-clock.ts +62 -47
  183. package/src/testing/index.ts +4 -1
  184. package/src/testing/node/index.ts +2 -2
  185. package/src/testing/node/message-port-pair.ts +40 -0
  186. package/src/transfer-diagnostic.ts +40 -0
  187. package/src/transfer-document.ts +41 -6
  188. package/src/util.ts +33 -0
  189. package/lib/model-service/args.d.ts +0 -64
  190. package/lib/model-service/args.d.ts.map +0 -1
  191. package/lib/model-service/args.js +0 -11
  192. package/lib/model-service/args.js.map +0 -1
  193. package/src/model-service/args.ts +0 -67
@@ -7,106 +7,337 @@
7
7
  * SPDX-License-Identifier: MIT
8
8
  ********************************************************************************/
9
9
 
10
- import type { MessageConnection } from 'vscode-jsonrpc';
11
- import { DATA_CLIENT_PROTOCOL_METHODS, DATA_SERVER_WIRE_PREFIX, type DataClientProtocol, type DataServerProtocol } from '../data';
12
- import { type RpcProxy, createRpcProxy } from '../rpc';
10
+ import { type Disposable, Emitter, type Event } from 'vscode-jsonrpc';
11
+ import { type Clock, SystemClock } from '../clock';
12
+ import type { DataServerProtocol, DiagnosticOf, TransferDocumentDirtyChangedEvent, TransferDocumentUpdatedEvent } from '../data';
13
+ import { isConflictError, SessionClosedError } from '../errors';
14
+ import { type BasedOn, isSnapshotVersion, type SnapshotVersion } from '../model-service/based-on';
15
+ import { type ResolvedMessage, defineMessage, describeError, resolve } from '../messages/primitives';
16
+ import type { OpenModelArgs } from '../model-server';
17
+ import type { MaybePromise } from '../util';
18
+ import type { RpcProxy } from '../rpc';
13
19
  import type { TransferDocument } from '../transfer-document';
14
20
  import type { TransferElement } from '../transfer-element';
15
- import type { DataPort } from './data-port';
16
21
 
17
- /** Options for {@link DataSession}. */
18
- export interface DataSessionOptions {
22
+ /**
23
+ * A session could not re-open a document it had open after its connection
24
+ * dropped, and forgot it; or could not write it again for another reason than
25
+ * a conflict, and keeps its unsaved edits for the next restore.
26
+ */
27
+ export const DATA_SESSION_RESTORE_FAILED = defineMessage(
28
+ 'hydranium/protocol/data-session-restore-failed',
29
+ 'Could not restore {uri} after reconnecting to the data server: {detail}'
30
+ );
31
+
32
+ /**
33
+ * A session re-opened documents after its connection dropped and cannot put
34
+ * back what it wrote to them since their last save: another client changed
35
+ * them, even while the session was still connected; the session could not
36
+ * tell the text its writes started from, or lost another document written with
37
+ * them; or the write it sent again conflicted. The session's unsaved edits may
38
+ * be gone, and it no longer holds them.
39
+ */
40
+ export const DATA_SESSION_UNSAVED_LOST = defineMessage(
41
+ 'hydranium/protocol/data-session-unsaved-lost',
42
+ 'Unsaved changes to {uris} may have been lost when the connection to the data server dropped.'
43
+ );
44
+
45
+ /**
46
+ * What one of `TServer`'s document methods takes, minus the `clientId` a
47
+ * {@link DataSession} stamps itself.
48
+ *
49
+ * Read off the SERVER's signature, not off the framework's own arg type: an
50
+ * adopter server widens these, and a wrapper declared against the narrow
51
+ * shape rejects the extra field on a fresh object literal, so that call
52
+ * cannot go through a session at all.
53
+ */
54
+ export type DataSessionArgs<TMethod extends (args: never) => unknown> = Omit<Parameters<TMethod>[0], 'clientId'>;
55
+
56
+ /**
57
+ * Open a document through a session; the session supplies `clientId`. Only the
58
+ * URI and the open's options: a session's open reads the file, and the server
59
+ * ignores the `languageId`, `version` and `text` seeds of `OpenModelArgs`, so
60
+ * accepting them here would let a caller believe they took effect.
61
+ */
62
+ export type DataSessionOpenArgs<
63
+ TTransfer extends TransferElement,
64
+ // oxlint-disable-next-line no-unused-vars -- TServer validates its own diagnostic constraint; Oxlint does not count that use.
65
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
66
+ > = Pick<OpenModelArgs, 'uri' | 'options'>;
67
+
68
+ /** Create a document through a session; the session supplies `clientId`. */
69
+ export type DataSessionCreateArgs<
70
+ TTransfer extends TransferElement,
71
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
72
+ > = DataSessionArgs<TServer['createModelDocument']>;
73
+
74
+ /** Close a document through a session; the session supplies `clientId`. */
75
+ export type DataSessionCloseArgs<
76
+ TTransfer extends TransferElement,
77
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
78
+ > = DataSessionArgs<TServer['closeModelDocument']>;
79
+
80
+ /** Update a document through a session; the session supplies `clientId`. */
81
+ export type DataSessionUpdateArgs<
82
+ TTransfer extends TransferElement,
83
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
84
+ > = DataSessionArgs<TServer['updateModelDocument']>;
85
+
86
+ /** Update several documents at once through a session; the session supplies `clientId`. */
87
+ export type DataSessionUpdatesArgs<
88
+ TTransfer extends TransferElement,
89
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
90
+ > = DataSessionArgs<TServer['updateModelDocuments']>;
91
+
92
+ /** Persist a document through a session; the session supplies `clientId`. */
93
+ export type DataSessionSaveArgs<
94
+ TTransfer extends TransferElement,
95
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
96
+ > = DataSessionArgs<TServer['saveModelDocument']>;
97
+
98
+ /** The document a session hands back, carrying its server's diagnostic shape. */
99
+ export type DataSessionDocument<
100
+ TTransfer extends TransferElement,
101
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
102
+ > = TransferDocument<TTransfer, DiagnosticOf<TServer>>;
103
+
104
+ /**
105
+ * What a {@link DataSession} keeps of a URI it wrote since the URI's last save,
106
+ * to write it again after a reconnect that lost it.
107
+ */
108
+ export interface DataSessionUnsavedWrite<
109
+ TTransfer extends TransferElement,
110
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
111
+ > {
19
112
  /**
20
- * Wire namespace the server is addressed under. Defaults to the
21
- * framework's {@link DATA_SERVER_WIRE_PREFIX}, which is what an unmodified
22
- * `DataServer` binds. Override only alongside the server's own
23
- * `methodNamespace` option — a mismatch turns every request into
24
- * "Unhandled method" rather than failing at wire-up.
113
+ * The {@link TransferDocument.textHash} of the text the first of these
114
+ * writes was based on. `undefined` when the session could not tell, for a
115
+ * write based on `'anything'` or on a version it could not read back, and
116
+ * such a write is never sent again: the session cannot tell another
117
+ * client's text from the text it wrote over.
25
118
  */
26
- readonly methodNamespace?: string;
119
+ readonly baseHash: string | undefined;
120
+ readonly answer: Pick<DataSessionDocument<TTransfer, TServer>, 'version' | 'textHash'>;
121
+ /**
122
+ * The call that made the last write, and for an `updateDocuments` call this
123
+ * URI's index in it. Records whose `updates` is the same object were
124
+ * written together and are sent again together, so a restore never applies
125
+ * part of a set.
126
+ */
127
+ readonly call:
128
+ | { readonly update: DataSessionUpdateArgs<TTransfer, TServer> }
129
+ | { readonly updates: DataSessionUpdatesArgs<TTransfer, TServer>; readonly index: number };
130
+ }
131
+
132
+ /** Where `write` stands in the `updateDocuments` call it was last written by; `0` for a single update. */
133
+ function indexOf(write: DataSessionUnsavedWrite<TransferElement, DataServerProtocol<TransferElement>>): number {
134
+ return 'index' in write.call ? write.call.index : 0;
27
135
  }
28
136
 
29
- /** One connection generation: its connection, its proxy, and its readiness. */
30
- interface Generation<TTransfer extends TransferElement> {
31
- readonly connection: Promise<MessageConnection>;
32
- readonly server: RpcProxy<DataServerProtocol<TTransfer>>;
33
- /** Set on first use; the shared readiness gate for this generation. */
34
- ready?: Promise<void>;
137
+ /**
138
+ * What a {@link DataSession} needs from the connection that minted it.
139
+ *
140
+ * Narrower than the connection itself so the dependency points one way:
141
+ * `DataConnection` constructs sessions, and nothing here imports it back.
142
+ */
143
+ export interface DataSessionHost<TTransfer extends TransferElement, TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>>> {
144
+ /** The connected, READY proxy of the current connection; a new object after each reconnect. */
145
+ connected(): Promise<RpcProxy<TServer>>;
146
+ /** Surface a failure no caller is waiting on, such as restoring a document after a reconnect. */
147
+ reportError?(error: unknown, reported: ResolvedMessage): void;
148
+ /**
149
+ * Tell the connection's client the dirty state a restore read, unless it
150
+ * is what the client was last told of the URI since the URI's last open.
151
+ */
152
+ restoreDirty?(event: TransferDocumentDirtyChangedEvent): void;
153
+ /**
154
+ * Forget what the client was told of `uri`'s dirty state: an open just
155
+ * made answered its caller afresh, and the client heard none of it, or a
156
+ * close leaves nothing to restore. Forgetting a URI another session still
157
+ * has open costs at most one repeat of a state the client already has.
158
+ */
159
+ forgetDirty?(uri: string): void;
160
+ /**
161
+ * Fires with each flip of a document's dirty state the server sends. A
162
+ * session drops the unsaved write it keeps of a document that turns
163
+ * clean; without this event it keeps it, and a restore after another
164
+ * client's save of the document can report the write lost.
165
+ */
166
+ readonly onDidChangeDirty?: Event<TransferDocumentDirtyChangedEvent>;
167
+ /**
168
+ * Fires with each update event the server sends. A session drops the
169
+ * unsaved write it keeps of a document another client wrote over; without
170
+ * this event it keeps it, and a restore reports the write lost.
171
+ */
172
+ readonly onDidUpdateDocument?: Event<TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>>;
35
173
  }
36
174
 
37
175
  /**
38
- * The host-invariant half of talking to the data head: everything above
39
- * {@link DataPort} that would otherwise be re-derived by every host
40
- * adapter.
176
+ * Builds the session `DataConnection.createSession` hands out, for an adopter
177
+ * that extends {@link DataSession}. The connection has minted `clientId` and
178
+ * checked it by then.
179
+ */
180
+ export type DataSessionFactory<TTransfer extends TransferElement, TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>>> = (
181
+ clientId: string,
182
+ host: DataSessionHost<TTransfer, TServer>,
183
+ label: string
184
+ ) => DataSession<TTransfer, TServer>;
185
+
186
+ /**
187
+ * One participant on a data connection: a properties panel, a tree, a form
188
+ * editor. The client side of a server client session, registered over the
189
+ * wire under {@link clientId}.
190
+ *
191
+ * The session writes only what it has open: the server refuses its update or
192
+ * save of a document it has not opened with a `DocumentNotOpenError` code.
193
+ * Every call waits for the registration, so the first can be issued at once.
194
+ *
195
+ * Every document operation stamps {@link clientId} itself. A caller that
196
+ * passed its own could pass another participant's, and the server would
197
+ * attribute the write and close the document accordingly.
41
198
  *
42
- * Three jobs, and deliberately no fourth:
199
+ * {@link closeDocument} and {@link dispose} first wait, up to
200
+ * {@link settleBeforeCloseMs}, for this session's calls still in flight on the
201
+ * URI, or on any URI for `dispose`: a save sent just before its close would
202
+ * otherwise reach the server after it, and fail as not open.
43
203
  *
44
- * 1. **Build the typed proxy** over the port's connection, with the framework's
45
- * wire prefix and its drift-proof client-method allowlist.
46
- * 2. **Own the readiness gate** — `waitForReady` once per connection, shared
47
- * across concurrent callers. A socket client can connect before the
48
- * workspace walk finishes, and an early request is then answered correctly
49
- * from an empty registry, which reads as a broken project tier rather than
50
- * as a race.
51
- * 3. **Own the reconnect policy**, by dropping its connection generation when
52
- * the port disposes and building a fresh one on the next request.
204
+ * After the connection drops, the session registers again under the same id,
205
+ * re-opens and re-watches what it had open, tells the client of their dirty
206
+ * state where it changed, and reports the documents whose unsaved edits did
207
+ * not survive; see {@link restore}. The connection does this at
208
+ * once for a session with documents open, and any session's next call does it
209
+ * too.
53
210
  *
54
- * It does **not** wrap the protocol methods; callers reach them through
55
- * {@link connected}. The one exception is {@link openDocument}, which exists
56
- * because the open/watch *order* is silently wrong the other way round — see
57
- * its own doc.
211
+ * Generic over the transfer root so this file names no grammar.
58
212
  *
59
- * Generic over the transfer root so this file names no grammar. An adopter
60
- * binds the concrete root (or the union of them, for a multi-grammar head) at
61
- * its own edge.
213
+ * `TServer` is bound to a server answering with ITS OWN diagnostic shape, read
214
+ * back off the parameter being bound. Simplifying that to
215
+ * `DataServerProtocol<TTransfer>` compiles and costs the wrappers their
216
+ * return type: every call through `TServer` would resolve against that looser
217
+ * bound, so an adopter's diagnostics would come back as the framework's and
218
+ * the document would have to be cast on the way out.
62
219
  */
63
- export class DataSession<TTransfer extends TransferElement> {
64
- protected readonly methodNamespace: string;
65
- /** The current generation, or `undefined` before the first request / after a teardown. */
66
- protected generation?: Generation<TTransfer>;
220
+ export class DataSession<
221
+ TTransfer extends TransferElement,
222
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
223
+ > implements Disposable {
224
+ /**
225
+ * How long {@link closeDocument} and {@link dispose} wait for this session's
226
+ * calls in flight before closing anyway. Long, because the wait only runs
227
+ * out when a call hangs, and a close sent while a save is still running
228
+ * fails that save.
229
+ */
230
+ protected readonly settleBeforeCloseMs: number = 10_000;
231
+ /**
232
+ * The clock {@link settleBeforeCloseMs} runs on. {@link DataSessionHost}
233
+ * carries no {@link Clock}, so a subclass replaces this field as it
234
+ * replaces the bound.
235
+ */
236
+ protected readonly clock: Clock = new SystemClock();
237
+ /** URIs this session has open, re-opened after a reconnect. */
238
+ protected readonly openUris = new Set<string>();
239
+ /**
240
+ * Per URI in {@link openUris}, the URI the server's last answer to its open
241
+ * or re-open named. The server keys its notifications by it, and so does
242
+ * the host's record of what its client was told.
243
+ */
244
+ protected readonly serverUris = new Map<string, string>();
245
+ /**
246
+ * Per URI, how many opens of it this session has under way. Such a URI
247
+ * counts as open for {@link withOpenDocument}, which would otherwise close
248
+ * it under the open that is still being made.
249
+ */
250
+ protected readonly openingUris = new Map<string, number>();
251
+ /** Per URI written since its last save, what {@link restore} needs to write it again. */
252
+ protected readonly unsavedWrites = new Map<string, DataSessionUnsavedWrite<TTransfer, TServer>>();
253
+ /**
254
+ * Per URI this session has open, the version and text hash of the last
255
+ * document one of its own calls was answered with, which spares the read a
256
+ * first unsaved write otherwise makes for its base; see {@link baseHashOf}.
257
+ */
258
+ protected readonly lastAnswers = new Map<string, Pick<DataSessionDocument<TTransfer, TServer>, 'version' | 'textHash'>>();
259
+ /**
260
+ * Per URI this session has open, the version its last save of it was
261
+ * answered with; see {@link recordWrite}. Only a save sets it: an open
262
+ * answers at the version of a write still in flight as well, and that
263
+ * write is not saved.
264
+ */
265
+ protected readonly savedVersions = new Map<string, SnapshotVersion>();
266
+ /** Per URI, this session's calls still in flight on it, which a close waits for. */
267
+ protected readonly inFlight = new Map<string, Set<Promise<unknown>>>();
268
+ /** This session's saves still in flight, which a host's exit waits for; see {@link hasSavesInFlight}. */
269
+ protected readonly savesInFlight = new Set<Promise<unknown>>();
270
+ protected readonly disposeEmitter = new Emitter<void>();
271
+ /**
272
+ * Fires once when the session ends, by {@link dispose} or {@link detach}, as
273
+ * soon as it rejects further calls and before any close is sent. For a
274
+ * session its connection created, the connection's listener runs first, so
275
+ * the id is free again on that connection by the time any other listener
276
+ * runs; the server still holds it until the close arrives, and refuses a new
277
+ * session under it until then.
278
+ *
279
+ * A listener subscribed once the session has ended is never called, so a
280
+ * late subscriber checks {@link isDisposed} first.
281
+ */
282
+ readonly onDidDispose: Event<void> = this.disposeEmitter.event;
283
+ /**
284
+ * Sent with every registration, so the server lets this session register
285
+ * its id again while the dropped connection's session is still live there:
286
+ * a server that has not yet noticed the drop would otherwise refuse the id
287
+ * as a duplicate until it does.
288
+ */
289
+ protected readonly resumeToken: string = globalThis.crypto.randomUUID();
290
+ /** The proxy the session is registered on; another one means the connection was replaced. */
291
+ protected registeredOn?: RpcProxy<TServer>;
292
+ protected registration?: Promise<void>;
67
293
  protected disposed = false;
68
- protected readonly portDisposeListener: { dispose(): void };
294
+ /**
295
+ * Disposed through {@link detach} rather than {@link dispose}, so nothing may
296
+ * be sent. Folding it into {@link disposed} would send the session's close
297
+ * over a connection that is going away.
298
+ */
299
+ protected detached = false;
300
+ /** The subscription to the host's `onDidChangeDirty`, disposed with the session. */
301
+ protected readonly dirtySubscription?: Disposable;
302
+ /** The subscription to the host's `onDidUpdateDocument`, disposed with the session. */
303
+ protected readonly updateSubscription?: Disposable;
69
304
 
70
305
  constructor(
71
- protected readonly port: DataPort,
72
- protected readonly client: DataClientProtocol<TTransfer>,
73
- options: DataSessionOptions = {}
306
+ readonly clientId: string,
307
+ protected readonly host: DataSessionHost<TTransfer, TServer>,
308
+ readonly label: string
74
309
  ) {
75
- this.methodNamespace = options.methodNamespace ?? DATA_SERVER_WIRE_PREFIX;
76
- this.portDisposeListener = this.port.onDispose(() => this.dropGeneration());
77
- }
78
-
79
- /** The identity every request is made under — the port's, not a second one. */
80
- get clientId(): string {
81
- return this.port.clientId;
310
+ this.dirtySubscription = host.onDidChangeDirty?.(event => this.forgetSavedWrite(event));
311
+ this.updateSubscription = host.onDidUpdateDocument?.(event => this.forgetSupersededWrite(event));
82
312
  }
83
313
 
84
314
  /**
85
- * The connected, READY server proxy.
86
- *
87
- * Returns the proxy rather than `void` on purpose. A reconnect replaces the
88
- * proxy, so a caller that cached one from an earlier call would go on
89
- * addressing a dead connection with no error — handing it back per call
90
- * makes the stale reference unrepresentable.
91
- *
92
- * Concurrent callers share one readiness promise, so `waitForReady` is
93
- * awaited once per generation and not once per caller.
315
+ * The connected, READY server proxy once this session is registered on it,
316
+ * for protocol methods this session does not wrap. The proxy stamps
317
+ * nothing: pass this session's {@link clientId} to any method that carries
318
+ * one.
94
319
  */
95
- async connected(): Promise<RpcProxy<DataServerProtocol<TTransfer>>> {
96
- if (this.disposed) {
97
- throw new Error('DataSession is disposed');
320
+ async connected(): Promise<RpcProxy<TServer>> {
321
+ // `async` so a disposed session REJECTS rather than throwing
322
+ // synchronously: the connection's own `connected` rejects, and a caller
323
+ // reaching for `.catch` on one of them would not catch the other.
324
+ this.assertLive();
325
+ const server = await this.host.connected();
326
+ // Again after the wait: a session disposed meanwhile must not register,
327
+ // since its dispose found nothing registered to end.
328
+ this.assertLive();
329
+ if (this.registeredOn !== server) {
330
+ const reconnected = this.registeredOn !== undefined;
331
+ this.registeredOn = server;
332
+ this.registration = this.register(server, reconnected);
98
333
  }
99
- const generation = this.currentGeneration();
100
- if (!generation.ready) {
101
- generation.ready = this.awaitReady(generation);
102
- }
103
- await generation.ready;
104
- return generation.server;
334
+ await this.registration;
335
+ return server;
105
336
  }
106
337
 
107
338
  /**
108
- * Open `uri` for editing and start watching it, in that order, returning
109
- * the opened snapshot.
339
+ * Open `args.uri` for editing and start watching it, in that order,
340
+ * returning the opened snapshot.
110
341
  *
111
342
  * **The order is the whole reason this method exists.**
112
343
  * `watchModelDocument` baselines its dedup fingerprint from the *current*
@@ -114,32 +345,183 @@ export class DataSession<TTransfer extends TransferElement> {
114
345
  * baseline, and the first phase event after the open arrives as a spurious
115
346
  * `'changed'` — which a widget that resets its in-memory root to the server
116
347
  * view misreads as a concurrent third-party write, losing whatever the user
117
- * had typed. Nothing about the wrong order fails loudly, so it is encoded
118
- * here rather than documented and re-derived.
348
+ * had typed.
119
349
  *
120
350
  * Note that the returned snapshot's empty `diagnostics` does not mean
121
351
  * valid: `open` settles at the integrity landmark, not at validation.
122
352
  * Validity arrives asynchronously on `onDocumentUpdated`, or synchronously
123
353
  * from `getModelDocument({ includeDiagnostics: true })`.
124
354
  */
125
- async openDocument(uri: string): Promise<TransferDocument<TTransfer>> {
126
- const server = await this.connected();
127
- const document = await server.openModelDocument({ uri, clientId: this.clientId });
128
- await server.watchModelDocument({ uri, clientId: this.clientId });
355
+ openDocument(args: DataSessionOpenArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
356
+ return this.trackOpen(args.uri, async () => {
357
+ const server = await this.connected();
358
+ return this.watchOpened(server, args.uri, await server.openModelDocument({ ...args, clientId: this.clientId }));
359
+ });
360
+ }
361
+
362
+ /**
363
+ * Create a document that exists nowhere yet, open and watched for this
364
+ * session; it reaches disk with the first {@link saveDocument}. The server
365
+ * refuses a URI that exists on disk or that any client has open.
366
+ */
367
+ createDocument(args: DataSessionCreateArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
368
+ return this.trackOpen(args.uri, async () => {
369
+ const server = await this.connected();
370
+ return this.watchOpened(server, args.uri, await server.createModelDocument({ ...args, clientId: this.clientId }));
371
+ });
372
+ }
373
+
374
+ /**
375
+ * Watch a document this session just opened, and record it as open. A
376
+ * failed watch closes it again: the caller sees a failed open and so would
377
+ * never close it.
378
+ */
379
+ protected async watchOpened(
380
+ server: RpcProxy<TServer>,
381
+ uri: string,
382
+ document: DataSessionDocument<TTransfer, TServer>
383
+ ): Promise<DataSessionDocument<TTransfer, TServer>> {
384
+ try {
385
+ await server.watchModelDocument({ uri, clientId: this.clientId });
386
+ } catch (error: unknown) {
387
+ await server.closeModelDocument({ uri, clientId: this.clientId }).catch(() => undefined);
388
+ throw error;
389
+ }
390
+ this.openUris.add(uri);
391
+ this.serverUris.set(uri, document.uri);
392
+ this.lastAnswers.set(uri, { version: document.version, textHash: document.textHash });
393
+ // Keyed as the server keys its notifications, which may not be how the
394
+ // caller spelled the URI.
395
+ this.host.forgetDirty?.(document.uri);
129
396
  return document;
130
397
  }
131
398
 
132
399
  /**
133
- * Close `uri`. The server unwatches implicitly, so this is the dual of
134
- * {@link openDocument} and needs no separate unwatch.
400
+ * Close `args.uri`, once this session's calls on it have settled or
401
+ * {@link settleBeforeCloseMs} has passed. The server unwatches implicitly,
402
+ * so this is the dual of {@link openDocument} and needs no separate unwatch.
135
403
  */
136
- async closeDocument(uri: string): Promise<void> {
404
+ async closeDocument(args: DataSessionCloseArgs<TTransfer, TServer>): Promise<void> {
405
+ this.assertLive();
406
+ await this.settle(this.inFlight.get(args.uri));
407
+ // Forgotten before the close is sent, so a reconnect in between does not
408
+ // re-open a document the caller has closed; after the wait, so an open
409
+ // of it that was still in flight does not record it again.
410
+ const serverUri = this.serverUris.get(args.uri) ?? args.uri;
411
+ this.openUris.delete(args.uri);
412
+ this.serverUris.delete(args.uri);
413
+ this.unsavedWrites.delete(args.uri);
414
+ this.lastAnswers.delete(args.uri);
415
+ this.savedVersions.delete(args.uri);
416
+ this.host.forgetDirty?.(serverUri);
137
417
  const server = await this.connected();
138
- await server.closeModelDocument({ uri, clientId: this.clientId });
418
+ await server.closeModelDocument({ ...args, clientId: this.clientId });
139
419
  }
140
420
 
141
421
  /**
142
- * Whether `event.sourceClientId` identifies this session's own write.
422
+ * Open `args.uri`, run `fn` with the opened snapshot, and close it once
423
+ * `fn` settles, whether it returned or threw. A URI this session already
424
+ * had open, or is still opening through another call, stays open: the close
425
+ * undoes only the open this call made.
426
+ */
427
+ async withOpenDocument<T>(
428
+ args: DataSessionOpenArgs<TTransfer, TServer>,
429
+ fn: (document: DataSessionDocument<TTransfer, TServer>) => MaybePromise<T>
430
+ ): Promise<T> {
431
+ const alreadyOpen = this.openUris.has(args.uri) || this.openingUris.has(args.uri);
432
+ const document = await this.openDocument(args);
433
+ try {
434
+ return await fn(document);
435
+ } finally {
436
+ if (!alreadyOpen && !this.disposed) {
437
+ await this.closeDocument({ uri: args.uri } as DataSessionCloseArgs<TTransfer, TServer>);
438
+ }
439
+ }
440
+ }
441
+
442
+ /** Write `args.model` back as this session. The session must have `args.uri` open. */
443
+ updateDocument(args: DataSessionUpdateArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
444
+ return this.track([args.uri], async () => {
445
+ const server = await this.connected();
446
+ const update = { ...args };
447
+ const baseHash = await this.baseHashOf(server, args.uri, args.basedOn);
448
+ const document = await server.updateModelDocument({ ...args, clientId: this.clientId });
449
+ this.recordWrite(args.uri, document, baseHash, { update });
450
+ return document;
451
+ });
452
+ }
453
+
454
+ /**
455
+ * Write several documents this session has open, all or none: the server
456
+ * refuses the whole set, before any text applies, when one is stale or not
457
+ * open. Resolves to the documents in the order given.
458
+ */
459
+ updateDocuments(args: DataSessionUpdatesArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>[]> {
460
+ return this.track(
461
+ args.updates.map(update => update.uri),
462
+ async () => {
463
+ // One copy for the whole call: the copy's identity is what ties its
464
+ // records together.
465
+ const updates = { ...args, updates: args.updates.map(update => ({ ...update })) };
466
+ const server = await this.connected();
467
+ const baseHashes = await Promise.all(args.updates.map(update => this.baseHashOf(server, update.uri, update.basedOn)));
468
+ const documents = await server.updateModelDocuments({ ...args, clientId: this.clientId });
469
+ args.updates.forEach((update, index) => this.recordWrite(update.uri, documents[index], baseHashes[index], { updates, index }));
470
+ return documents;
471
+ }
472
+ );
473
+ }
474
+
475
+ /** Persist `args.model` to disk as this session. The session must have `args.uri` open. */
476
+ saveDocument(args: DataSessionSaveArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
477
+ const saving = this.track([args.uri], async () => {
478
+ const server = await this.connected();
479
+ const unsaved = this.unsavedWrites.get(args.uri);
480
+ const document = await server.saveModelDocument({ ...args, clientId: this.clientId });
481
+ // A write that answered while the save ran made a record the save
482
+ // may not have persisted, which a restore still needs.
483
+ if (this.unsavedWrites.get(args.uri) === unsaved) {
484
+ this.unsavedWrites.delete(args.uri);
485
+ }
486
+ this.lastAnswers.set(args.uri, { version: document.version, textHash: document.textHash });
487
+ // The higher of two saves' answers, which may arrive out of order.
488
+ const saved = this.savedVersions.get(args.uri);
489
+ if (saved === undefined || document.version > saved) {
490
+ this.savedVersions.set(args.uri, document.version);
491
+ }
492
+ return document;
493
+ });
494
+ this.savesInFlight.add(saving);
495
+ const done = (): boolean => this.savesInFlight.delete(saving);
496
+ saving.then(done, done);
497
+ return saving;
498
+ }
499
+
500
+ /** Whether the session has ended, by {@link dispose} or {@link detach}. */
501
+ get isDisposed(): boolean {
502
+ return this.disposed;
503
+ }
504
+
505
+ /**
506
+ * Whether a save of this session has not answered yet. Synchronous, for a
507
+ * host whose exit veto must decide within the tick, such as Theia's
508
+ * `onWillStop`.
509
+ */
510
+ get hasSavesInFlight(): boolean {
511
+ return this.savesInFlight.size > 0;
512
+ }
513
+
514
+ /**
515
+ * Resolves once the saves in flight now have answered, or
516
+ * {@link settleBeforeCloseMs} has passed. Never rejects: a failed save has
517
+ * answered too.
518
+ */
519
+ whenSavesSettled(): Promise<void> {
520
+ return this.settle(this.savesInFlight);
521
+ }
522
+
523
+ /**
524
+ * Whether `sourceClientId` identifies this session's own write.
143
525
  *
144
526
  * Every watcher needs this and the check is one comparison, so getting it
145
527
  * wrong is cheap to do and expensive to find: an unfiltered echo looks
@@ -149,61 +531,437 @@ export class DataSession<TTransfer extends TransferElement> {
149
531
  return sourceClientId === this.clientId;
150
532
  }
151
533
 
152
- /** Tear down the current connection and stop tracking the port. Idempotent. */
534
+ /**
535
+ * End the session: detach it from the connection at once, and once its
536
+ * calls in flight have settled or {@link settleBeforeCloseMs} has passed,
537
+ * end it on the server, which closes everything it has open. Idempotent,
538
+ * and leaves the connection usable by its other sessions.
539
+ *
540
+ * Every later call rejects, and so does a call made earlier in the same
541
+ * tick, which has not reached the wire yet and is never sent. The server
542
+ * close is not awaited, because a `Disposable` cannot be; a close that fails
543
+ * leaves the session to the server's connection-close cleanup.
544
+ */
153
545
  dispose(): void {
154
546
  if (this.disposed) {
155
547
  return;
156
548
  }
157
549
  this.disposed = true;
158
- this.portDisposeListener.dispose();
159
- this.dropGeneration();
160
- }
161
-
162
- /** The live generation, building one if there is none. */
163
- protected currentGeneration(): Generation<TTransfer> {
164
- if (this.generation) {
165
- return this.generation;
166
- }
167
- const connection = this.port.connect();
168
- // Rejection is reported here rather than left to float: an unhandled
169
- // rejection on a connection promise is the failure mode that reads as
170
- // "the model is empty" instead of "the transport never opened".
171
- connection.catch((error: unknown) => this.port.reportError(error, 'connecting to the data server'));
172
- const server = createRpcProxy<DataServerProtocol<TTransfer>, DataClientProtocol<TTransfer>>(connection, {
173
- methodNamespace: this.methodNamespace,
174
- localTarget: this.client,
175
- localMethods: DATA_CLIENT_PROTOCOL_METHODS
176
- });
177
- this.generation = { connection, server };
178
- return this.generation;
550
+ this.fireDispose();
551
+ const pending = [...this.inFlight.values()].flatMap(calls => [...calls]);
552
+ void (async () => {
553
+ await this.settle(pending);
554
+ await this.registration;
555
+ // The proxy the session registered on, never a fresh connection: a
556
+ // connection that dropped already ended the session on the server.
557
+ if (!this.detached && this.registeredOn) {
558
+ await this.registeredOn.closeSession({ clientId: this.clientId });
559
+ }
560
+ })().catch(() => undefined);
561
+ }
562
+
563
+ /**
564
+ * Come off the connection because it is going away.
565
+ *
566
+ * Sends nothing, unlike {@link dispose}: the server ends every session on a
567
+ * connection it sees close, and the close would travel over the very
568
+ * connection being disposed.
569
+ *
570
+ * Public because the connection calls it; anyone else ends a session with
571
+ * {@link dispose}. It fires {@link onDidDispose} only if the session has not
572
+ * already ended, and after {@link dispose} it still cancels the close that
573
+ * dispose has not sent yet.
574
+ */
575
+ detach(): void {
576
+ this.detached = true;
577
+ this.openUris.clear();
578
+ this.serverUris.clear();
579
+ this.unsavedWrites.clear();
580
+ this.lastAnswers.clear();
581
+ this.savedVersions.clear();
582
+ if (!this.disposed) {
583
+ this.disposed = true;
584
+ this.fireDispose();
585
+ }
586
+ }
587
+
588
+ /**
589
+ * Dispose {@link dirtySubscription} and {@link updateSubscription}, fire
590
+ * {@link onDidDispose} and dispose its emitter, so a second call fires
591
+ * nothing.
592
+ */
593
+ protected fireDispose(): void {
594
+ this.dirtySubscription?.dispose();
595
+ this.updateSubscription?.dispose();
596
+ this.disposeEmitter.fire(undefined);
597
+ this.disposeEmitter.dispose();
598
+ }
599
+
600
+ /**
601
+ * Register again and restore now, after the connection dropped, instead of
602
+ * on the next call. A no-op for a session with nothing open, which the next
603
+ * call restores anyway. A failure is left to that next call, which meets it
604
+ * again.
605
+ */
606
+ reconnect(): void {
607
+ if (!this.disposed && this.openUris.size > 0) {
608
+ this.connected().catch(() => undefined);
609
+ }
610
+ }
611
+
612
+ /**
613
+ * Drop the record in {@link unsavedWrites} of a document this session has
614
+ * open once the server says it turned clean: it holds no unsaved text
615
+ * then, and a record kept would have a restore report the write lost once
616
+ * another client's save changed the file.
617
+ *
618
+ * The server sends a flip only for a document someone watches, so a
619
+ * document the session writes without watching it keeps its record, and
620
+ * can still be reported lost that way.
621
+ */
622
+ protected forgetSavedWrite(event: TransferDocumentDirtyChangedEvent): void {
623
+ if (event.dirty) {
624
+ return;
625
+ }
626
+ // The flip names the server's key, which is not always the caller's
627
+ // spelling that the records are kept under.
628
+ for (const [uri, serverUri] of this.serverUris) {
629
+ if (serverUri === event.uri) {
630
+ this.unsavedWrites.delete(uri);
631
+ }
632
+ }
179
633
  }
180
634
 
181
- /** Await the connection and the server's startup gate for one generation. */
182
- protected async awaitReady(generation: Generation<TTransfer>): Promise<void> {
635
+ /**
636
+ * Drop the record in {@link unsavedWrites} of a document another client
637
+ * wrote over while the connection held: its write replaced the session's,
638
+ * so a restore has nothing of the session's to put back, and a record kept
639
+ * would have it report the write lost to the reconnect. A write superseded
640
+ * before its answer needs nothing: the answer names the other client's
641
+ * text, which the record then holds.
642
+ *
643
+ * Only a `'changed'` event from another client counts, and only where the
644
+ * document no longer holds the write: the session's own echo, and an
645
+ * integrity repair already in the write's answer, replaced nothing, and a
646
+ * `'rebuilt'` event carries no new text. Like {@link forgetSavedWrite}, it
647
+ * depends on the server sending the event, which it does only for a
648
+ * document someone watches.
649
+ */
650
+ protected forgetSupersededWrite(event: TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>): void {
651
+ if (event.reason !== 'changed' || this.isOwnEcho(event.sourceClientId)) {
652
+ return;
653
+ }
654
+ for (const [uri, serverUri] of this.serverUris) {
655
+ const write = this.unsavedWrites.get(uri);
656
+ if (serverUri === event.document.uri && write && this.restoreOutcome(write, event.document) !== 'kept') {
657
+ this.unsavedWrites.delete(uri);
658
+ }
659
+ }
660
+ }
661
+
662
+ /**
663
+ * Register on `server`, and after a reconnect restore what the ended session
664
+ * had. Calls issued meanwhile wait for this.
665
+ */
666
+ protected async register(server: RpcProxy<TServer>, reconnected: boolean): Promise<void> {
667
+ await server.createSession({ clientId: this.clientId, label: this.label, resumeToken: this.resumeToken });
668
+ if (reconnected) {
669
+ await this.restore(server);
670
+ }
671
+ }
672
+
673
+ /**
674
+ * Re-open and re-watch every document the session had open, write again
675
+ * what it wrote since their last save where the re-open lost it, and tell
676
+ * the host which of them lost it for good.
677
+ *
678
+ * Decided per document by the re-opened {@link TransferDocument.textHash},
679
+ * in {@link restoreOutcome}. Versions cannot decide a write: a revert moves
680
+ * the version on and a restarted server numbers afresh, so the version the
681
+ * last write was answered with never matches where a write is needed, and
682
+ * any other version may be another client's edit, which a write would
683
+ * overwrite.
684
+ *
685
+ * Only what the drop cost is reported: a write another client wrote over
686
+ * while the connection held has no record left by then, see
687
+ * {@link forgetSupersededWrite}.
688
+ *
689
+ * A write is sent again based on the re-opened version, as an ordinary
690
+ * write, so an edit arriving in between conflicts, and a conflict is not
691
+ * retried. Documents last written by one {@link updateDocuments} are sent
692
+ * again by one, and only when every one of them may be; a document that
693
+ * could not be re-opened blocks its set.
694
+ *
695
+ * No caller is waiting, so the outcomes go through the host: one report
696
+ * naming every document whose unsaved text is gone, whose record is then
697
+ * dropped, and one per document that could not be re-opened or written
698
+ * again for another reason than a conflict. A document that could not be
699
+ * re-opened is forgotten; a failed write keeps its record, for the next
700
+ * restore to decide again.
701
+ *
702
+ * Each document's dirty state goes to the host too, read once the watch is
703
+ * in place and any write is sent: a flip while the connection was down, or
704
+ * between the re-open and the watch, reached no one, and the re-open's own
705
+ * answer misses the second. Read before the write, it would tell the client
706
+ * a document is clean that the write is about to make dirty again.
707
+ */
708
+ protected async restore(server: RpcProxy<TServer>): Promise<void> {
709
+ const writes = [...this.unsavedWrites];
710
+ // No write sent before the drop answers any more, and a restarted
711
+ // server numbers afresh, below the versions saved before it.
712
+ this.savedVersions.clear();
713
+ const reopened = new Map<string, DataSessionDocument<TTransfer, TServer>>();
714
+ for (const uri of [...this.openUris]) {
715
+ try {
716
+ const document = await server.openModelDocument({ uri, clientId: this.clientId });
717
+ await server.watchModelDocument({ uri, clientId: this.clientId });
718
+ if (this.openUris.has(uri)) {
719
+ this.serverUris.set(uri, document.uri);
720
+ this.lastAnswers.set(uri, { version: document.version, textHash: document.textHash });
721
+ }
722
+ reopened.set(uri, document);
723
+ } catch (error: unknown) {
724
+ // Its record goes with its set's, in `reapply`, which it blocks.
725
+ this.openUris.delete(uri);
726
+ this.serverUris.delete(uri);
727
+ this.lastAnswers.delete(uri);
728
+ this.savedVersions.delete(uri);
729
+ this.host.reportError?.(error, resolve(DATA_SESSION_RESTORE_FAILED, { uri, detail: describeError(error) }));
730
+ }
731
+ }
732
+ const sets = new Map<object, [string, DataSessionUnsavedWrite<TTransfer, TServer>][]>();
733
+ for (const [uri, write] of writes) {
734
+ const call = 'update' in write.call ? write.call.update : write.call.updates;
735
+ sets.set(call, [...(sets.get(call) ?? []), [uri, write]]);
736
+ }
737
+ const lost: string[] = [];
738
+ for (const members of sets.values()) {
739
+ lost.push(...(await this.reapply(server, members, reopened)));
740
+ }
741
+ for (const uri of reopened.keys()) {
742
+ if (!this.openUris.has(uri)) {
743
+ continue;
744
+ }
745
+ // A failed read leaves the client's dirty state as it was; the
746
+ // document is restored all the same.
747
+ const current = await server.getModelDocument({ uri }).catch(() => undefined);
748
+ if (current?.dirty !== undefined) {
749
+ try {
750
+ this.host.restoreDirty?.({ uri: current.uri, dirty: current.dirty });
751
+ } catch {
752
+ // The client's listener failed, not the restore: the server
753
+ // has the document open and watched, so it stays restored.
754
+ }
755
+ }
756
+ }
757
+ if (lost.length > 0) {
758
+ const reported = resolve(DATA_SESSION_UNSAVED_LOST, { uris: lost.join(', ') });
759
+ this.host.reportError?.(new Error(reported.text), reported);
760
+ }
761
+ }
762
+
763
+ /**
764
+ * Whether the re-opened `document` still holds the session's unsaved
765
+ * `write` (`'kept'`), holds the text the write started from, so the write
766
+ * can be sent again (`'resend'`), or holds something else (`'lost'`).
767
+ *
768
+ * A document without a text hash comes from a server that sends none, and
769
+ * counts as kept only at the version the write was answered with.
770
+ */
771
+ protected restoreOutcome(
772
+ write: DataSessionUnsavedWrite<TTransfer, TServer>,
773
+ document: DataSessionDocument<TTransfer, TServer>
774
+ ): 'kept' | 'resend' | 'lost' {
775
+ if (document.textHash === undefined) {
776
+ return document.version === write.answer.version ? 'kept' : 'lost';
777
+ }
778
+ if (document.textHash === write.answer.textHash) {
779
+ return 'kept';
780
+ }
781
+ return write.baseHash !== undefined && document.textHash === write.baseHash ? 'resend' : 'lost';
782
+ }
783
+
784
+ /**
785
+ * Write again, in one call and based on their `reopened` versions, the
786
+ * `members` of one write call that the re-open lost, all or none. Returns
787
+ * the URIs whose unsaved text is gone, and drops their records, and the
788
+ * record of a member that could not be re-opened.
789
+ *
790
+ * A member whose record is no longer the one the restore started with was
791
+ * closed, saved or disposed meanwhile, and is left out: writing it would
792
+ * put back text its caller discarded.
793
+ */
794
+ protected async reapply(
795
+ server: RpcProxy<TServer>,
796
+ members: readonly [string, DataSessionUnsavedWrite<TTransfer, TServer>][],
797
+ reopened: ReadonlyMap<string, DataSessionDocument<TTransfer, TServer>>
798
+ ): Promise<string[]> {
799
+ const current = (uri: string, write: DataSessionUnsavedWrite<TTransfer, TServer>): boolean =>
800
+ !this.disposed && this.unsavedWrites.get(uri) === write;
801
+ const outcomes = members
802
+ .filter(([uri, write]) => current(uri, write))
803
+ .map(([uri, write]) => {
804
+ const document = reopened.get(uri);
805
+ return { uri, write, document, outcome: document ? this.restoreOutcome(write, document) : 'failed' };
806
+ });
807
+ // A restarted server numbers afresh: the answers to later writes follow
808
+ // on from the re-opened version, not from the one the record holds.
809
+ for (const { uri, write, document, outcome } of outcomes) {
810
+ if (outcome === 'kept' && document) {
811
+ this.unsavedWrites.set(uri, { ...write, answer: { version: document.version, textHash: document.textHash } });
812
+ }
813
+ }
814
+ // A document that could not be re-opened is forgotten, and was reported
815
+ // as such.
816
+ const lose = (): string[] => {
817
+ outcomes.filter(member => member.outcome !== 'kept').forEach(member => this.unsavedWrites.delete(member.uri));
818
+ return outcomes.filter(member => member.outcome === 'lost' || member.outcome === 'resend').map(member => member.uri);
819
+ };
820
+ if (outcomes.some(member => member.outcome === 'lost' || member.outcome === 'failed')) {
821
+ return lose();
822
+ }
823
+ // In the order of the call's updates, which is the order of its answers.
824
+ const resend = outcomes
825
+ .flatMap(({ uri, write, document, outcome }) =>
826
+ outcome === 'resend' && document ? [{ uri, write, basedOn: document.version }] : []
827
+ )
828
+ .sort((left, right) => indexOf(left.write) - indexOf(right.write));
829
+ if (resend.length === 0) {
830
+ return [];
831
+ }
832
+ let documents: DataSessionDocument<TTransfer, TServer>[];
183
833
  try {
184
- await generation.connection;
185
- await generation.server.waitForReady();
834
+ const { call } = resend[0].write;
835
+ if ('update' in call) {
836
+ documents = [await server.updateModelDocument({ ...call.update, basedOn: resend[0].basedOn, clientId: this.clientId })];
837
+ } else {
838
+ const basedOn = new Map(resend.map(member => [indexOf(member.write), member.basedOn]));
839
+ documents = await server.updateModelDocuments({
840
+ ...call.updates,
841
+ clientId: this.clientId,
842
+ updates: call.updates.updates.flatMap((update, index) => {
843
+ const version = basedOn.get(index);
844
+ return version === undefined ? [] : [{ ...update, basedOn: version }];
845
+ })
846
+ });
847
+ }
186
848
  } catch (error: unknown) {
187
- // Drop the generation so the next request retries rather than
188
- // re-awaiting a settled rejection forever.
189
- if (this.generation === generation) {
190
- this.generation = undefined;
849
+ if (isConflictError(error)) {
850
+ return lose();
191
851
  }
192
- this.port.reportError(error, 'waiting for the data server to become ready');
193
- throw error;
852
+ for (const { uri, write, basedOn } of resend) {
853
+ this.host.reportError?.(error, resolve(DATA_SESSION_RESTORE_FAILED, { uri, detail: describeError(error) }));
854
+ // Kept for the next restore, and numbered from the re-opened
855
+ // version, as the kept records are: a record numbered by a server
856
+ // that has since restarted would have later answers ignored.
857
+ if (current(uri, write)) {
858
+ this.unsavedWrites.set(uri, { ...write, answer: { ...write.answer, version: basedOn } });
859
+ }
860
+ }
861
+ return [];
194
862
  }
863
+ resend.forEach((member, index) => {
864
+ if (current(member.uri, member.write)) {
865
+ // Recorded afresh, since the answer of a restarted server may be
866
+ // numbered below the one it replaces.
867
+ this.unsavedWrites.delete(member.uri);
868
+ this.recordWrite(member.uri, documents[index], member.write.baseHash, member.write.call);
869
+ }
870
+ });
871
+ return [];
195
872
  }
196
873
 
197
874
  /**
198
- * Discard the current generation, disposing its connection if it opened.
199
- * The next {@link connected} builds a fresh one.
875
+ * The text hash of the version `basedOn` names for `uri`, for a write that
876
+ * starts the URI's record in {@link unsavedWrites}; `undefined` for any
877
+ * other write, whose record keeps the base it has.
878
+ *
879
+ * Taken from {@link lastAnswers} when that is at the version, otherwise
880
+ * read, and kept only when the read answers that version: a server's text
881
+ * changes only with its version, so the text at a version is the text the
882
+ * write, once it passes the gate, was applied to.
200
883
  */
201
- protected dropGeneration(): void {
202
- const generation = this.generation;
203
- this.generation = undefined;
204
- if (!generation) {
884
+ protected async baseHashOf(server: RpcProxy<TServer>, uri: string, basedOn: BasedOn): Promise<string | undefined> {
885
+ if (this.unsavedWrites.has(uri) || !isSnapshotVersion(basedOn)) {
886
+ return undefined;
887
+ }
888
+ const known = this.lastAnswers.get(uri);
889
+ if (known?.version === basedOn) {
890
+ return known.textHash;
891
+ }
892
+ const read = await server.getModelDocument({ uri }).catch(() => undefined);
893
+ return read?.version === basedOn ? read.textHash : undefined;
894
+ }
895
+
896
+ /**
897
+ * Record `document` as the answer to a write of `uri`, keeping the base of
898
+ * the URI's record if it has one. An answer numbered below the record's is
899
+ * ignored: two writes of `uri` in flight at once may answer out of order,
900
+ * and the record holds the one applied last.
901
+ *
902
+ * So is one numbered at or below the URI's entry in {@link savedVersions}.
903
+ * The save came after that write was applied, so the save persisted its
904
+ * text or a later write replaced it, and its answer carries the text as it
905
+ * was when sent, which a restore would report as lost.
906
+ */
907
+ protected recordWrite(
908
+ uri: string,
909
+ document: DataSessionDocument<TTransfer, TServer>,
910
+ baseHash: string | undefined,
911
+ call: DataSessionUnsavedWrite<TTransfer, TServer>['call']
912
+ ): void {
913
+ const answer = { version: document.version, textHash: document.textHash };
914
+ const kept = this.unsavedWrites.get(uri);
915
+ if (kept && answer.version < kept.answer.version) {
916
+ return;
917
+ }
918
+ const saved = this.savedVersions.get(uri);
919
+ if (saved !== undefined && answer.version <= saved) {
920
+ return;
921
+ }
922
+ this.unsavedWrites.set(uri, { baseHash: kept ? kept.baseHash : baseHash, answer, call });
923
+ this.lastAnswers.set(uri, answer);
924
+ }
925
+
926
+ /** Run `call`, counted as in flight on each of `uris` until it settles. */
927
+ protected track<T>(uris: readonly string[], call: () => Promise<T>): Promise<T> {
928
+ const running = call();
929
+ for (const uri of uris) {
930
+ this.inFlight.set(uri, (this.inFlight.get(uri) ?? new Set<Promise<unknown>>()).add(running));
931
+ }
932
+ const done = (): void => uris.forEach(uri => this.inFlight.get(uri)?.delete(running));
933
+ running.then(done, done);
934
+ return running;
935
+ }
936
+
937
+ /** {@link track} an open of `uri`, counted in {@link openingUris} until it settles. */
938
+ protected trackOpen<T>(uri: string, call: () => Promise<T>): Promise<T> {
939
+ this.openingUris.set(uri, (this.openingUris.get(uri) ?? 0) + 1);
940
+ const done = (): void => {
941
+ const remaining = (this.openingUris.get(uri) ?? 1) - 1;
942
+ if (remaining > 0) {
943
+ this.openingUris.set(uri, remaining);
944
+ } else {
945
+ this.openingUris.delete(uri);
946
+ }
947
+ };
948
+ const running = this.track([uri], call);
949
+ running.then(done, done);
950
+ return running;
951
+ }
952
+
953
+ /** Wait until `calls` have settled, or {@link settleBeforeCloseMs} has passed. */
954
+ protected async settle(calls: Iterable<Promise<unknown>> | undefined): Promise<void> {
955
+ const pending = [...(calls ?? [])];
956
+ if (pending.length === 0) {
205
957
  return;
206
958
  }
207
- generation.connection.then(connection => connection.dispose()).catch(() => undefined);
959
+ await this.clock.raceTimer(Promise.allSettled(pending), this.settleBeforeCloseMs);
960
+ }
961
+
962
+ protected assertLive(): void {
963
+ if (this.disposed) {
964
+ throw new SessionClosedError(this.clientId);
965
+ }
208
966
  }
209
967
  }