@hydranium/protocol 1.0.0-next.17 → 1.0.0-next.174

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