@hydranium/protocol 1.0.0-next.34 → 1.0.0-next.341
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.
- package/README.md +44 -83
- package/lib/abstract-logger.d.ts +5 -0
- package/lib/abstract-logger.d.ts.map +1 -1
- package/lib/abstract-logger.js +7 -0
- package/lib/abstract-logger.js.map +1 -1
- package/lib/client/data-connection.d.ts +302 -0
- package/lib/client/data-connection.d.ts.map +1 -0
- package/lib/client/data-connection.js +533 -0
- package/lib/client/data-connection.js.map +1 -0
- package/lib/client/data-events.d.ts +17 -1
- package/lib/client/data-events.d.ts.map +1 -1
- package/lib/client/data-events.js +25 -0
- package/lib/client/data-events.js.map +1 -1
- package/lib/client/data-port.d.ts +34 -24
- package/lib/client/data-port.d.ts.map +1 -1
- package/lib/client/data-session.d.ts +684 -89
- package/lib/client/data-session.d.ts.map +1 -1
- package/lib/client/data-session.js +1148 -115
- package/lib/client/data-session.js.map +1 -1
- package/lib/client/index.d.ts +15 -9
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/index.js +15 -9
- package/lib/client/index.js.map +1 -1
- package/lib/client/message-relay.d.ts +4 -4
- package/lib/client/message-relay.js +4 -4
- package/lib/client/message-relay.js.map +1 -1
- package/lib/client/post-message-transport.d.ts +64 -3
- package/lib/client/post-message-transport.d.ts.map +1 -1
- package/lib/client/post-message-transport.js +175 -1
- package/lib/client/post-message-transport.js.map +1 -1
- package/lib/client/rpc-connection.d.ts +157 -0
- package/lib/client/rpc-connection.d.ts.map +1 -0
- package/lib/client/rpc-connection.js +214 -0
- package/lib/client/rpc-connection.js.map +1 -0
- package/lib/client/worker-handshake.d.ts +86 -0
- package/lib/client/worker-handshake.d.ts.map +1 -0
- package/lib/client/worker-handshake.js +51 -0
- package/lib/client/worker-handshake.js.map +1 -0
- package/lib/client-ids.d.ts +45 -0
- package/lib/client-ids.d.ts.map +1 -0
- package/lib/client-ids.js +48 -0
- package/lib/client-ids.js.map +1 -0
- package/lib/clock.d.ts +50 -10
- package/lib/clock.d.ts.map +1 -1
- package/lib/clock.js +36 -1
- package/lib/clock.js.map +1 -1
- package/lib/console-logger.d.ts +23 -0
- package/lib/console-logger.d.ts.map +1 -0
- package/lib/console-logger.js +39 -0
- package/lib/console-logger.js.map +1 -0
- package/lib/data/data-protocol-methods.d.ts +4 -4
- package/lib/data/data-protocol-methods.d.ts.map +1 -1
- package/lib/data/data-protocol-methods.js +13 -1
- package/lib/data/data-protocol-methods.js.map +1 -1
- package/lib/data/data-server-protocol.d.ts +170 -48
- package/lib/data/data-server-protocol.d.ts.map +1 -1
- package/lib/data/events.d.ts +137 -21
- package/lib/data/events.d.ts.map +1 -1
- package/lib/data/requests.d.ts +71 -11
- package/lib/data/requests.d.ts.map +1 -1
- package/lib/debouncer.d.ts +3 -3
- package/lib/debouncer.d.ts.map +1 -1
- package/lib/debouncer.js +6 -9
- package/lib/debouncer.js.map +1 -1
- package/lib/errors.d.ts +183 -30
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +268 -32
- package/lib/errors.js.map +1 -1
- package/lib/glsp-edits-settled-actions.d.ts +52 -0
- package/lib/glsp-edits-settled-actions.d.ts.map +1 -0
- package/lib/glsp-edits-settled-actions.js +28 -0
- package/lib/glsp-edits-settled-actions.js.map +1 -0
- package/lib/glsp-request-model-args.d.ts +16 -0
- package/lib/glsp-request-model-args.d.ts.map +1 -0
- package/lib/glsp-request-model-args.js +19 -0
- package/lib/glsp-request-model-args.js.map +1 -0
- package/lib/glsp-save-model-actions.d.ts +50 -0
- package/lib/glsp-save-model-actions.d.ts.map +1 -0
- package/lib/glsp-save-model-actions.js +28 -0
- package/lib/glsp-save-model-actions.js.map +1 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +10 -0
- package/lib/index.js.map +1 -1
- package/lib/latency-collector.d.ts +8 -4
- package/lib/latency-collector.d.ts.map +1 -1
- package/lib/latency-collector.js.map +1 -1
- package/lib/logger.d.ts +38 -7
- package/lib/logger.d.ts.map +1 -1
- package/lib/logger.js +42 -4
- package/lib/logger.js.map +1 -1
- package/lib/messages/index.d.ts +4 -2
- package/lib/messages/index.d.ts.map +1 -1
- package/lib/messages/index.js +15 -4
- package/lib/messages/index.js.map +1 -1
- package/lib/messages/primitives.d.ts +59 -4
- package/lib/messages/primitives.d.ts.map +1 -1
- package/lib/messages/primitives.js +97 -4
- package/lib/messages/primitives.js.map +1 -1
- package/lib/model-server.d.ts +60 -13
- package/lib/model-server.d.ts.map +1 -1
- package/lib/model-server.js +4 -2
- package/lib/model-server.js.map +1 -1
- package/lib/model-service/base-version.d.ts +67 -0
- package/lib/model-service/base-version.d.ts.map +1 -0
- package/lib/model-service/base-version.js +44 -0
- package/lib/model-service/base-version.js.map +1 -0
- package/lib/model-service/index.d.ts +1 -1
- package/lib/model-service/index.d.ts.map +1 -1
- package/lib/model-service/index.js +4 -5
- package/lib/model-service/index.js.map +1 -1
- package/lib/model-service/reference-candidate.d.ts +5 -3
- package/lib/model-service/reference-candidate.d.ts.map +1 -1
- package/lib/node/index.d.ts +10 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +29 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/process-memory.d.ts +66 -0
- package/lib/node/process-memory.d.ts.map +1 -0
- package/lib/node/process-memory.js +291 -0
- package/lib/node/process-memory.js.map +1 -0
- package/lib/noop-logger.d.ts.map +1 -1
- package/lib/noop-logger.js.map +1 -1
- package/lib/observable-value.js.map +1 -1
- package/lib/patch-merge.d.ts +63 -35
- package/lib/patch-merge.d.ts.map +1 -1
- package/lib/patch-merge.js +171 -25
- package/lib/patch-merge.js.map +1 -1
- package/lib/pending-work-tracker.d.ts +29 -0
- package/lib/pending-work-tracker.d.ts.map +1 -0
- package/lib/pending-work-tracker.js +46 -0
- package/lib/pending-work-tracker.js.map +1 -0
- package/lib/profile-session.d.ts +8 -4
- package/lib/profile-session.d.ts.map +1 -1
- package/lib/profile-session.js.map +1 -1
- package/lib/project.d.ts +4 -1
- package/lib/project.d.ts.map +1 -1
- package/lib/project.js.map +1 -1
- package/lib/random-uuid.d.ts +14 -0
- package/lib/random-uuid.d.ts.map +1 -0
- package/lib/random-uuid.js +24 -0
- package/lib/random-uuid.js.map +1 -0
- package/lib/reconcile-write.d.ts +67 -0
- package/lib/reconcile-write.d.ts.map +1 -0
- package/lib/reconcile-write.js +69 -0
- package/lib/reconcile-write.js.map +1 -0
- package/lib/rpc/bind-rpc-methods.d.ts +6 -2
- package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
- package/lib/rpc/bind-rpc-methods.js +17 -7
- package/lib/rpc/bind-rpc-methods.js.map +1 -1
- package/lib/rpc/create-rpc-proxy.d.ts +3 -0
- package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
- package/lib/rpc/create-rpc-proxy.js +6 -1
- package/lib/rpc/create-rpc-proxy.js.map +1 -1
- package/lib/rpc/index.d.ts +1 -0
- package/lib/rpc/index.d.ts.map +1 -1
- package/lib/rpc/index.js +1 -0
- package/lib/rpc/index.js.map +1 -1
- package/lib/rpc/send-by-method-name.d.ts +76 -0
- package/lib/rpc/send-by-method-name.d.ts.map +1 -0
- package/lib/rpc/send-by-method-name.js +120 -0
- package/lib/rpc/send-by-method-name.js.map +1 -0
- package/lib/rpc/wire-prefix.js.map +1 -1
- package/lib/save-text-document.d.ts +40 -0
- package/lib/save-text-document.d.ts.map +1 -0
- package/lib/save-text-document.js +34 -0
- package/lib/save-text-document.js.map +1 -0
- package/lib/testing/catalogue-audit.d.ts +1 -1
- package/lib/testing/catalogue-audit.js +1 -1
- package/lib/testing/catalogue-audit.js.map +1 -1
- package/lib/testing/data-doubles.d.ts +41 -12
- package/lib/testing/data-doubles.d.ts.map +1 -1
- package/lib/testing/data-doubles.js +56 -8
- package/lib/testing/data-doubles.js.map +1 -1
- package/lib/testing/fake-clock.d.ts +17 -2
- package/lib/testing/fake-clock.d.ts.map +1 -1
- package/lib/testing/fake-clock.js +61 -46
- package/lib/testing/fake-clock.js.map +1 -1
- package/lib/testing/index.d.ts +1 -0
- package/lib/testing/index.d.ts.map +1 -1
- package/lib/testing/index.js +4 -3
- package/lib/testing/index.js.map +1 -1
- package/lib/testing/node/duplex-connection.d.ts.map +1 -1
- package/lib/testing/node/duplex-connection.js +3 -2
- package/lib/testing/node/duplex-connection.js.map +1 -1
- package/lib/testing/node/duplex-stream.js.map +1 -1
- package/lib/testing/node/index.d.ts +1 -0
- package/lib/testing/node/index.d.ts.map +1 -1
- package/lib/testing/node/index.js +2 -2
- package/lib/testing/node/index.js.map +1 -1
- package/lib/testing/node/message-port-pair.d.ts +25 -0
- package/lib/testing/node/message-port-pair.d.ts.map +1 -0
- package/lib/testing/node/message-port-pair.js +26 -0
- package/lib/testing/node/message-port-pair.js.map +1 -0
- package/lib/testing/transfer-node-builder.d.ts +81 -0
- package/lib/testing/transfer-node-builder.d.ts.map +1 -0
- package/lib/testing/transfer-node-builder.js +47 -0
- package/lib/testing/transfer-node-builder.js.map +1 -0
- package/lib/testing/wait-for.d.ts +9 -3
- package/lib/testing/wait-for.d.ts.map +1 -1
- package/lib/testing/wait-for.js +57 -11
- package/lib/testing/wait-for.js.map +1 -1
- package/lib/tracer.d.ts.map +1 -1
- package/lib/tracer.js +5 -9
- package/lib/tracer.js.map +1 -1
- package/lib/transfer-diagnostic.js.map +1 -1
- package/lib/transfer-document.d.ts +95 -32
- package/lib/transfer-document.d.ts.map +1 -1
- package/lib/transfer-document.js +17 -9
- package/lib/transfer-document.js.map +1 -1
- package/lib/transfer-element.d.ts +4 -4
- package/lib/types.d.ts +32 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/{model-service/args.js → types.js} +2 -2
- package/lib/types.js.map +1 -0
- package/lib/uri.d.ts.map +1 -1
- package/lib/uri.js.map +1 -1
- package/lib/util.d.ts +8 -2
- package/lib/util.d.ts.map +1 -1
- package/lib/util.js +32 -4
- package/lib/util.js.map +1 -1
- package/package.json +24 -40
- package/src/abstract-logger.ts +8 -0
- package/src/client/data-connection.ts +653 -0
- package/src/client/data-events.ts +37 -1
- package/src/client/data-port.ts +36 -25
- package/src/client/data-session.ts +1469 -137
- package/src/client/index.ts +15 -9
- package/src/client/message-relay.ts +4 -4
- package/src/client/post-message-transport.ts +219 -4
- package/src/client/rpc-connection.ts +281 -0
- package/src/client/worker-handshake.ts +113 -0
- package/src/client-ids.ts +49 -0
- package/src/clock.ts +68 -10
- package/src/console-logger.ts +39 -0
- package/src/data/data-protocol-methods.ts +14 -4
- package/src/data/data-server-protocol.ts +197 -48
- package/src/data/events.ts +143 -21
- package/src/data/requests.ts +77 -11
- package/src/debouncer.ts +7 -10
- package/src/errors.ts +326 -40
- package/src/glsp-edits-settled-actions.ts +60 -0
- package/src/glsp-request-model-args.ts +16 -0
- package/src/glsp-save-model-actions.ts +59 -0
- package/src/index.ts +10 -0
- package/src/latency-collector.ts +8 -3
- package/src/logger.ts +44 -8
- package/src/messages/index.ts +4 -2
- package/src/messages/primitives.ts +129 -5
- package/src/model-server.ts +64 -19
- package/src/model-service/base-version.ts +75 -0
- package/src/model-service/index.ts +4 -5
- package/src/model-service/reference-candidate.ts +5 -3
- package/src/node/index.ts +14 -0
- package/src/node/process-memory.ts +299 -0
- package/src/patch-merge.ts +230 -45
- package/src/pending-work-tracker.ts +46 -0
- package/src/profile-session.ts +9 -4
- package/src/project.ts +4 -1
- package/src/random-uuid.ts +21 -0
- package/src/reconcile-write.ts +128 -0
- package/src/rpc/README.md +6 -6
- package/src/rpc/bind-rpc-methods.ts +18 -8
- package/src/rpc/create-rpc-proxy.ts +6 -1
- package/src/rpc/index.ts +1 -0
- package/src/rpc/send-by-method-name.ts +140 -0
- package/src/save-text-document.ts +39 -0
- package/src/testing/catalogue-audit.ts +1 -1
- package/src/testing/data-doubles.ts +141 -19
- package/src/testing/fake-clock.ts +76 -48
- package/src/testing/index.ts +4 -3
- package/src/testing/node/duplex-connection.ts +3 -2
- package/src/testing/node/index.ts +2 -2
- package/src/testing/node/message-port-pair.ts +40 -0
- package/src/testing/transfer-node-builder.ts +107 -0
- package/src/testing/wait-for.ts +61 -13
- package/src/tracer.ts +5 -9
- package/src/transfer-document.ts +115 -34
- package/src/transfer-element.ts +4 -4
- package/src/types.ts +30 -0
- package/src/util.ts +33 -6
- package/lib/model-service/args.d.ts +0 -64
- package/lib/model-service/args.d.ts.map +0 -1
- package/lib/model-service/args.js.map +0 -1
- package/src/model-service/args.ts +0 -67
|
@@ -7,123 +7,480 @@
|
|
|
7
7
|
* SPDX-License-Identifier: MIT
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
|
-
import type
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import
|
|
14
|
-
|
|
10
|
+
import { type Disposable, Emitter, type Event } from 'vscode-jsonrpc';
|
|
11
|
+
import { UNKNOWN_CLIENT_ID } from '../client-ids';
|
|
12
|
+
import { type Clock, SystemClock, TIMED_OUT } from '../clock';
|
|
13
|
+
import type {
|
|
14
|
+
DataServerProtocol,
|
|
15
|
+
DiagnosticOf,
|
|
16
|
+
TransferDocumentDeletedEvent,
|
|
17
|
+
TransferDocumentDirtyChangedEvent,
|
|
18
|
+
TransferDocumentUpdatedEvent
|
|
19
|
+
} from '../data';
|
|
20
|
+
import { isConflictError, SessionClosedError } from '../errors';
|
|
21
|
+
import type { Logger } from '../logger';
|
|
22
|
+
import { type BaseVersion, isModelVersion, type ModelVersion, type TextVersion, UNRECORDED_VERSION } from '../model-service/base-version';
|
|
23
|
+
import { type ResolvedMessage, defineMessage, describeError, resolve } from '../messages/primitives';
|
|
24
|
+
import { NoopLogger } from '../noop-logger';
|
|
25
|
+
import { PendingWorkTracker } from '../pending-work-tracker';
|
|
26
|
+
import { randomUuid } from '../random-uuid';
|
|
27
|
+
import type { MaybePromise } from '../util';
|
|
28
|
+
import type { RpcProxy } from '../rpc';
|
|
29
|
+
import type { TextState, TransferCreatedDocument, TransferDocument, TransferSavedDocument } from '../transfer-document';
|
|
15
30
|
import type { TransferElement } from '../transfer-element';
|
|
16
|
-
import type { DataPort } from './data-port';
|
|
17
31
|
|
|
18
32
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* correctly in every language.
|
|
33
|
+
* A session could not re-open a document it had open after its connection
|
|
34
|
+
* dropped, and forgot it; or could not write it again for another reason than
|
|
35
|
+
* a conflict, and keeps its unsaved edits for the next restore.
|
|
23
36
|
*/
|
|
24
|
-
export const
|
|
25
|
-
'hydranium/protocol/data-
|
|
26
|
-
'Could not
|
|
37
|
+
export const DATA_SESSION_RESTORE_FAILED = defineMessage(
|
|
38
|
+
'hydranium/protocol/data-session-restore-failed',
|
|
39
|
+
'Could not restore {uri} after reconnecting to the data server: {detail}'
|
|
27
40
|
);
|
|
28
41
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
'
|
|
42
|
+
/** A write or open of `{uri}` that the data server answered without a model. */
|
|
43
|
+
export const DATA_SESSION_ANSWER_WITHOUT_MODEL = defineMessage(
|
|
44
|
+
'hydranium/protocol/data-session-answer-without-model',
|
|
45
|
+
'The data server answered a write of {uri} without a model; the session counts that answer as older than every version.'
|
|
32
46
|
);
|
|
33
47
|
|
|
34
|
-
/**
|
|
35
|
-
|
|
48
|
+
/**
|
|
49
|
+
* A session re-opened documents after its connection dropped and cannot put
|
|
50
|
+
* back what it wrote to them since their last save: another client changed
|
|
51
|
+
* them, even while the session was still connected; the session could not
|
|
52
|
+
* tell the text its writes started from, or lost another document written with
|
|
53
|
+
* them; or the write it sent again conflicted. The session's unsaved edits may
|
|
54
|
+
* be gone, and it no longer holds them.
|
|
55
|
+
*/
|
|
56
|
+
export const DATA_SESSION_UNSAVED_LOST = defineMessage(
|
|
57
|
+
'hydranium/protocol/data-session-unsaved-lost',
|
|
58
|
+
'Unsaved changes to {uris} may have been lost when the connection to the data server dropped.'
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* What one of `TServer`'s document methods takes, minus the `clientId` a
|
|
63
|
+
* {@link DataSession} stamps itself.
|
|
64
|
+
*
|
|
65
|
+
* Read off the SERVER's signature, not off the framework's own arg type: an
|
|
66
|
+
* adopter server widens these, and a wrapper declared against the narrow
|
|
67
|
+
* shape rejects the extra field on a fresh object literal, so that call
|
|
68
|
+
* cannot go through a session at all.
|
|
69
|
+
*/
|
|
70
|
+
export type DataSessionArgs<TMethod extends (args: never) => unknown> = Omit<Parameters<TMethod>[0], 'clientId'>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Open a document through a session; the session supplies `clientId`. Only the
|
|
74
|
+
* URI and the open's options, read off the server's signature: the server
|
|
75
|
+
* ignores the `languageId`, `version` and `text` seeds of `OpenModelArgs`, and
|
|
76
|
+
* a field an adopter adds never reaches the server session's open, so
|
|
77
|
+
* accepting either would let a caller believe it took effect.
|
|
78
|
+
*/
|
|
79
|
+
export type DataSessionOpenArgs<
|
|
80
|
+
TTransfer extends TransferElement,
|
|
81
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
82
|
+
> = Pick<Parameters<TServer['openModelDocument']>[0], 'uri' | ('options' & keyof Parameters<TServer['openModelDocument']>[0])>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The options an open through a session passes to `TServer`, `undefined` when
|
|
86
|
+
* it passed none; only `undefined` for a server whose open takes none.
|
|
87
|
+
*/
|
|
88
|
+
export type DataSessionOpenOptions<
|
|
89
|
+
TTransfer extends TransferElement,
|
|
90
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
91
|
+
> = DataSessionOpenArgs<TTransfer, TServer>['options' & keyof DataSessionOpenArgs<TTransfer, TServer>] | undefined;
|
|
92
|
+
|
|
93
|
+
/** Create a document through a session; the session supplies `clientId`. */
|
|
94
|
+
export type DataSessionCreateArgs<
|
|
95
|
+
TTransfer extends TransferElement,
|
|
96
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
97
|
+
> = DataSessionArgs<TServer['createModelDocument']>;
|
|
98
|
+
|
|
99
|
+
/** Close a document through a session; the session supplies `clientId`. */
|
|
100
|
+
export type DataSessionCloseArgs<
|
|
101
|
+
TTransfer extends TransferElement,
|
|
102
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
103
|
+
> = DataSessionArgs<TServer['closeModelDocument']>;
|
|
104
|
+
|
|
105
|
+
/** Update a document through a session; the session supplies `clientId`. */
|
|
106
|
+
export type DataSessionUpdateArgs<
|
|
107
|
+
TTransfer extends TransferElement,
|
|
108
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
109
|
+
> = DataSessionArgs<TServer['updateModelDocument']>;
|
|
110
|
+
|
|
111
|
+
/** Update several documents at once through a session; the session supplies `clientId`. */
|
|
112
|
+
export type DataSessionUpdatesArgs<
|
|
113
|
+
TTransfer extends TransferElement,
|
|
114
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
115
|
+
> = DataSessionArgs<TServer['updateModelDocuments']>;
|
|
116
|
+
|
|
117
|
+
/** Persist a document through a session; the session supplies `clientId`. */
|
|
118
|
+
export type DataSessionSaveArgs<
|
|
119
|
+
TTransfer extends TransferElement,
|
|
120
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
121
|
+
> = DataSessionArgs<TServer['saveModelDocument']>;
|
|
122
|
+
|
|
123
|
+
/** Persist the text the server holds through a session; the session supplies `clientId`. */
|
|
124
|
+
export type DataSessionPersistArgs<
|
|
125
|
+
TTransfer extends TransferElement,
|
|
126
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
127
|
+
> = DataSessionArgs<TServer['persistModelDocument']>;
|
|
128
|
+
|
|
129
|
+
/** Delete a document through a session; the session supplies `clientId`. */
|
|
130
|
+
export type DataSessionDeleteArgs<
|
|
131
|
+
TTransfer extends TransferElement,
|
|
132
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
133
|
+
> = DataSessionArgs<TServer['deleteModelDocument']>;
|
|
134
|
+
|
|
135
|
+
/** The document a session hands back, carrying its server's diagnostic shape. */
|
|
136
|
+
export type DataSessionDocument<
|
|
137
|
+
TTransfer extends TransferElement,
|
|
138
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
139
|
+
> = TransferDocument<TTransfer, DiagnosticOf<TServer>>;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* What a {@link DataSession} keeps of a URI it wrote since the URI's last save,
|
|
143
|
+
* to write it again after a reconnect that lost it.
|
|
144
|
+
*/
|
|
145
|
+
export interface DataSessionUnsavedWrite<
|
|
146
|
+
TTransfer extends TransferElement,
|
|
147
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
148
|
+
> {
|
|
149
|
+
/**
|
|
150
|
+
* The `text.hash` of the text the first of these writes was based on.
|
|
151
|
+
* `undefined` when the session could not tell, for a write based on
|
|
152
|
+
* `'any'` or on a version it could not read back, and such a write is
|
|
153
|
+
* never sent again: the session cannot tell another client's text from the
|
|
154
|
+
* text it wrote over.
|
|
155
|
+
*/
|
|
156
|
+
readonly baseHash: string | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* The model version the last of these writes was answered with, which
|
|
159
|
+
* orders its answers, and the `text.hash` of that answer.
|
|
160
|
+
*/
|
|
161
|
+
readonly answer: { readonly version: ModelVersion; readonly hash?: string };
|
|
162
|
+
/**
|
|
163
|
+
* The call that made the last write, and for an `updateDocuments` call this
|
|
164
|
+
* URI's index in it. Records whose `updates` is the same object were
|
|
165
|
+
* written together and are sent again together, so a restore never applies
|
|
166
|
+
* part of a set.
|
|
167
|
+
*/
|
|
168
|
+
readonly call:
|
|
169
|
+
| { readonly update: DataSessionUpdateArgs<TTransfer, TServer> }
|
|
170
|
+
| { readonly updates: DataSessionUpdatesArgs<TTransfer, TServer>; readonly index: number };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* One open of a URI by a {@link DataSession}, from the answer to its open,
|
|
175
|
+
* create or re-open until a close or a deletion ends it. A call records its
|
|
176
|
+
* answer only while the open it was sent under is still the URI's: checking
|
|
177
|
+
* that the URI is open accepts an answer from before a close or deletion
|
|
178
|
+
* once the URI is open again.
|
|
179
|
+
*/
|
|
180
|
+
export interface DataSessionOpen<
|
|
181
|
+
TTransfer extends TransferElement,
|
|
182
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
183
|
+
> {
|
|
184
|
+
/** The URI the open's answer named, which the server keys its notifications by. */
|
|
185
|
+
readonly serverUri: string;
|
|
36
186
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* `methodNamespace` option — a mismatch turns every request into
|
|
41
|
-
* "Unhandled method" rather than failing at wire-up.
|
|
187
|
+
* The options the server opened under, which a re-open after a reconnect
|
|
188
|
+
* passes again: those of the earliest open of the URI under way when the
|
|
189
|
+
* first answered, since the server keeps the open that arrives first.
|
|
42
190
|
*/
|
|
43
|
-
readonly
|
|
191
|
+
readonly options: DataSessionOpenOptions<TTransfer, TServer>;
|
|
192
|
+
/** Whether a deletion ended the open, which an open still watching sends again; a close does not. */
|
|
193
|
+
deleted: boolean;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* One open or create of a URI by a {@link DataSession}, from its call until it
|
|
198
|
+
* settles. A close of the URI while it is under way marks it: it then records
|
|
199
|
+
* nothing and is not sent again, so the close stands. Without the mark, a
|
|
200
|
+
* deletion during its watch has it sent again after the close.
|
|
201
|
+
*/
|
|
202
|
+
export interface DataSessionOpening<
|
|
203
|
+
TTransfer extends TransferElement,
|
|
204
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
205
|
+
> {
|
|
206
|
+
/** Whether a close of the URI came while this open was under way. */
|
|
207
|
+
closed: boolean;
|
|
208
|
+
/** The options the open was sent with; none for a create. */
|
|
209
|
+
readonly options: DataSessionOpenOptions<TTransfer, TServer>;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Where `write` stands in the `updateDocuments` call it was last written by; `0` for a single update. */
|
|
213
|
+
function indexOf(write: DataSessionUnsavedWrite<TransferElement, DataServerProtocol<TransferElement>>): number {
|
|
214
|
+
return 'index' in write.call ? write.call.index : 0;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The call `write` was last written by, which a restore writes again as one set. */
|
|
218
|
+
function setOf(write: DataSessionUnsavedWrite<TransferElement, DataServerProtocol<TransferElement>>): object {
|
|
219
|
+
return 'update' in write.call ? write.call.update : write.call.updates;
|
|
44
220
|
}
|
|
45
221
|
|
|
46
|
-
/**
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
222
|
+
/**
|
|
223
|
+
* What a {@link DataSession} needs from the connection that minted it.
|
|
224
|
+
*
|
|
225
|
+
* Narrower than the connection itself so the dependency points one way:
|
|
226
|
+
* `DataConnection` constructs sessions, and nothing here imports it back.
|
|
227
|
+
*/
|
|
228
|
+
export interface DataSessionHost<TTransfer extends TransferElement, TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>>> {
|
|
229
|
+
/** The connected, READY proxy of the current connection; a new object after each reconnect. */
|
|
230
|
+
connected(): Promise<RpcProxy<TServer>>;
|
|
231
|
+
/** Surface a failure no caller is waiting on, such as restoring a document after a reconnect. */
|
|
232
|
+
reportError?(error: unknown, reported: ResolvedMessage): void;
|
|
233
|
+
/**
|
|
234
|
+
* Tell the connection's client the dirty state a restore read, unless it
|
|
235
|
+
* is what the client was last told of the URI since the URI's last open.
|
|
236
|
+
*/
|
|
237
|
+
restoreDirty?(event: TransferDocumentDirtyChangedEvent): void;
|
|
238
|
+
/**
|
|
239
|
+
* Tell the connection's client of a document a restore found gone, whose
|
|
240
|
+
* deletion reached no one while the connection was down, as the server
|
|
241
|
+
* tells it of a deletion over a live connection. Without it, the restore
|
|
242
|
+
* reports the unsaved text such a document took with it as lost.
|
|
243
|
+
*/
|
|
244
|
+
restoreDeletion?(event: TransferDocumentDeletedEvent): void;
|
|
245
|
+
/**
|
|
246
|
+
* Forget what the client was told of `uri`'s dirty state: an open just
|
|
247
|
+
* made answered its caller afresh, and the client heard none of it, or a
|
|
248
|
+
* close leaves nothing to restore. Forgetting a URI another session still
|
|
249
|
+
* has open costs at most one repeat of a state the client already has.
|
|
250
|
+
*/
|
|
251
|
+
forgetDirty?(uri: string): void;
|
|
252
|
+
/**
|
|
253
|
+
* Fires with each flip of a document's dirty state the server sends. A
|
|
254
|
+
* session drops the unsaved write it keeps of a document that turns
|
|
255
|
+
* clean; without this event it keeps it, and a restore after another
|
|
256
|
+
* client's save of the document can report the write lost.
|
|
257
|
+
*/
|
|
258
|
+
readonly onDidChangeDirty?: Event<TransferDocumentDirtyChangedEvent>;
|
|
259
|
+
/**
|
|
260
|
+
* Fires with each update event the server sends. A session drops the
|
|
261
|
+
* unsaved write it keeps of a document another client wrote over; without
|
|
262
|
+
* this event it keeps it, and a restore reports the write lost.
|
|
263
|
+
*/
|
|
264
|
+
readonly onDidUpdateDocument?: Event<TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>>;
|
|
265
|
+
/**
|
|
266
|
+
* Fires with each deletion the server sends. A session forgets a document
|
|
267
|
+
* whose file was deleted; without this event it keeps it as open until a
|
|
268
|
+
* restore finds it gone.
|
|
269
|
+
*/
|
|
270
|
+
readonly onDidDeleteDocument?: Event<TransferDocumentDeletedEvent>;
|
|
271
|
+
/** Where the session logs the documents it forgets as gone. */
|
|
272
|
+
readonly logger?: Logger;
|
|
52
273
|
}
|
|
53
274
|
|
|
54
275
|
/**
|
|
55
|
-
*
|
|
56
|
-
* {@link
|
|
57
|
-
*
|
|
276
|
+
* Builds the session `DataConnection.createSession` hands out, for an adopter
|
|
277
|
+
* that extends {@link DataSession}. The connection has minted `clientId` and
|
|
278
|
+
* checked it by then.
|
|
279
|
+
*/
|
|
280
|
+
export type DataSessionFactory<TTransfer extends TransferElement, TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>>> = (
|
|
281
|
+
clientId: string,
|
|
282
|
+
host: DataSessionHost<TTransfer, TServer>,
|
|
283
|
+
label: string
|
|
284
|
+
) => DataSession<TTransfer, TServer>;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* One participant on a data connection: a properties panel, a tree, a form
|
|
288
|
+
* editor. The client side of a server client session, registered over the
|
|
289
|
+
* wire under {@link clientId}.
|
|
290
|
+
*
|
|
291
|
+
* The session writes only what it has open: the server refuses its update or
|
|
292
|
+
* save of a document it has not opened with a `DocumentNotOpenError` code.
|
|
293
|
+
* Every call waits for the registration, so the first can be issued at once.
|
|
294
|
+
*
|
|
295
|
+
* Every document operation stamps {@link clientId} itself. A caller that
|
|
296
|
+
* passed its own could pass another participant's, and the server would
|
|
297
|
+
* attribute the write and close the document accordingly.
|
|
58
298
|
*
|
|
59
|
-
*
|
|
299
|
+
* {@link closeDocument}, {@link deleteDocument} and {@link dispose} first wait,
|
|
300
|
+
* up to {@link settleBeforeCloseMs}, for this session's calls still in flight
|
|
301
|
+
* on the URI, or on any URI for `dispose`: a save sent just before its close
|
|
302
|
+
* would otherwise reach the server after it, and fail as not open. An open or
|
|
303
|
+
* create of a URI called while a close or delete of it is under way is sent
|
|
304
|
+
* once that has answered, so the server takes them in the order they were
|
|
305
|
+
* called.
|
|
60
306
|
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* 3. **Own the reconnect policy**, by dropping its connection generation when
|
|
69
|
-
* the port disposes and building a fresh one on the next request.
|
|
307
|
+
* After the connection drops, the session registers again under the same id,
|
|
308
|
+
* re-opens and re-watches what it had open, tells the client of their dirty
|
|
309
|
+
* state where it changed, and reports the documents whose unsaved edits did
|
|
310
|
+
* not survive; see {@link restore}. The connection does this at
|
|
311
|
+
* once for a session with documents open, and again once any later connection
|
|
312
|
+
* is ready, so a reconnect that failed is not left to the session's own next
|
|
313
|
+
* call; any session's next call does it too.
|
|
70
314
|
*
|
|
71
|
-
*
|
|
72
|
-
* {@link connected}. The one exception is {@link openDocument}, which exists
|
|
73
|
-
* because the open/watch *order* is silently wrong the other way round — see
|
|
74
|
-
* its own doc.
|
|
315
|
+
* Generic over the transfer root so this file names no grammar.
|
|
75
316
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
317
|
+
* `TServer` is bound to a server answering with ITS OWN diagnostic shape, read
|
|
318
|
+
* back off the parameter being bound. Simplifying that to
|
|
319
|
+
* `DataServerProtocol<TTransfer>` compiles and costs the wrappers their
|
|
320
|
+
* return type: every call through `TServer` would resolve against that looser
|
|
321
|
+
* bound, so an adopter's diagnostics would come back as the framework's and
|
|
322
|
+
* the document would have to be cast on the way out.
|
|
79
323
|
*/
|
|
80
|
-
export class DataSession<
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
324
|
+
export class DataSession<
|
|
325
|
+
TTransfer extends TransferElement,
|
|
326
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
327
|
+
> implements Disposable {
|
|
328
|
+
/**
|
|
329
|
+
* How long {@link closeDocument}, {@link deleteDocument} and {@link dispose}
|
|
330
|
+
* wait for this session's calls in flight before going on anyway. Long,
|
|
331
|
+
* because a close sent while a save is still writing fails that save. It
|
|
332
|
+
* need not outlast the server's language-client settle after the write,
|
|
333
|
+
* which a close does not fail.
|
|
334
|
+
*/
|
|
335
|
+
protected readonly settleBeforeCloseMs: number = 10_000;
|
|
336
|
+
/**
|
|
337
|
+
* How long {@link whenWritesSettled} waits before going on anyway: well past
|
|
338
|
+
* the server's bound on the language-client settle it runs before answering
|
|
339
|
+
* an update. The update itself has no bound, so a server slower than that is
|
|
340
|
+
* taken as lost.
|
|
341
|
+
*/
|
|
342
|
+
protected readonly settleWritesMs: number = 30_000;
|
|
343
|
+
/**
|
|
344
|
+
* The clock {@link settleBeforeCloseMs} runs on. {@link DataSessionHost}
|
|
345
|
+
* carries no {@link Clock}, so a subclass replaces this field as it
|
|
346
|
+
* replaces the bound.
|
|
347
|
+
*/
|
|
348
|
+
protected readonly clock: Clock = new SystemClock();
|
|
349
|
+
/** URIs this session has open, re-opened after a reconnect. */
|
|
350
|
+
protected readonly openUris = new Set<string>();
|
|
351
|
+
/**
|
|
352
|
+
* Per URI in {@link openUris}, or whose open answered and whose watch is
|
|
353
|
+
* still under way, the open it is under. A repeat open keeps it, as the
|
|
354
|
+
* server keeps its earlier open.
|
|
355
|
+
*/
|
|
356
|
+
protected readonly opens = new Map<string, DataSessionOpen<TTransfer, TServer>>();
|
|
357
|
+
/**
|
|
358
|
+
* Per URI, the opens of it this session has under way. Such a URI counts
|
|
359
|
+
* as open for {@link withOpenDocument}, which would otherwise close it
|
|
360
|
+
* under the open that is still being made.
|
|
361
|
+
*/
|
|
362
|
+
protected readonly openings = new Map<string, Set<DataSessionOpening<TTransfer, TServer>>>();
|
|
363
|
+
/**
|
|
364
|
+
* Per URI, the closes and deletes of it this session has under way, which
|
|
365
|
+
* an open or create called meanwhile waits for; see {@link trackEnding}.
|
|
366
|
+
*/
|
|
367
|
+
protected readonly endings = new Map<string, Promise<unknown>>();
|
|
368
|
+
/** Per URI written since its last save, what {@link restore} needs to write it again. */
|
|
369
|
+
protected readonly unsavedWrites = new Map<string, DataSessionUnsavedWrite<TTransfer, TServer>>();
|
|
370
|
+
/**
|
|
371
|
+
* Per URI this session has open, the text of the last document one of its
|
|
372
|
+
* own calls was answered with, which spares the read a first unsaved write
|
|
373
|
+
* otherwise makes for its base; see {@link baseHashOf}.
|
|
374
|
+
*/
|
|
375
|
+
protected readonly lastAnswers = new Map<string, TextState>();
|
|
376
|
+
/**
|
|
377
|
+
* Per URI this session has open, the text version its last save or persist
|
|
378
|
+
* of it wrote, as its answer's `persisted` says; see {@link recordWrite}.
|
|
379
|
+
* Not the answer's model version, which can carry a write that landed
|
|
380
|
+
* after the text was taken. Only those set it: an open answers at the
|
|
381
|
+
* version of a write still in flight as well, and that write is not saved.
|
|
382
|
+
*/
|
|
383
|
+
protected readonly savedVersions = new Map<string, TextVersion>();
|
|
384
|
+
/**
|
|
385
|
+
* URIs this session created whose whole text is still its own and unsaved,
|
|
386
|
+
* even with no write recorded: no save, clean flip, other client's write or
|
|
387
|
+
* reported loss has come since the create, its answer included; see
|
|
388
|
+
* {@link forgetGoneDocument}.
|
|
389
|
+
*/
|
|
390
|
+
protected readonly createdUris = new Set<string>();
|
|
391
|
+
/** Per URI, this session's calls still in flight on it, which a close or delete waits for. */
|
|
392
|
+
protected readonly inFlight = new Map<string, PendingWorkTracker>();
|
|
393
|
+
/** This session's saves, persists and deletes still in flight, which a host's exit waits for; see {@link hasSavesInFlight}. */
|
|
394
|
+
protected readonly savesInFlight = new PendingWorkTracker();
|
|
395
|
+
/** This session's updates still in flight, which a page's unload guard and save account for; see {@link hasWritesInFlight}. */
|
|
396
|
+
protected readonly writesInFlight = new PendingWorkTracker();
|
|
397
|
+
protected readonly disposeEmitter = new Emitter<void>();
|
|
398
|
+
/**
|
|
399
|
+
* Fires once when the session ends, by {@link dispose} or {@link detach}, as
|
|
400
|
+
* soon as it rejects further calls and before any close is sent. For a
|
|
401
|
+
* session its connection created, the connection's listener runs first, so
|
|
402
|
+
* the id is free again on that connection by the time any other listener
|
|
403
|
+
* runs; the server still holds it until the close arrives, and refuses a new
|
|
404
|
+
* session under it until then.
|
|
405
|
+
*
|
|
406
|
+
* A listener subscribed once the session has ended is never called, so a
|
|
407
|
+
* late subscriber checks {@link isDisposed} first.
|
|
408
|
+
*/
|
|
409
|
+
readonly onDidDispose: Event<void> = this.disposeEmitter.event;
|
|
410
|
+
/**
|
|
411
|
+
* Sent with every registration, so the server lets this session register
|
|
412
|
+
* its id again while the dropped connection's session is still live there:
|
|
413
|
+
* a server that has not yet noticed the drop would otherwise refuse the id
|
|
414
|
+
* as a duplicate until it does.
|
|
415
|
+
*/
|
|
416
|
+
protected readonly resumeToken: string = randomUuid();
|
|
417
|
+
/** The proxy the session is registered on; another one means the connection was replaced. */
|
|
418
|
+
protected registeredOn?: RpcProxy<TServer>;
|
|
419
|
+
protected registration?: Promise<void>;
|
|
84
420
|
protected disposed = false;
|
|
85
|
-
|
|
421
|
+
/**
|
|
422
|
+
* Disposed through {@link detach} rather than {@link dispose}, so nothing may
|
|
423
|
+
* be sent. Folding it into {@link disposed} would send the session's close
|
|
424
|
+
* over a connection that is going away.
|
|
425
|
+
*/
|
|
426
|
+
protected detached = false;
|
|
427
|
+
/**
|
|
428
|
+
* Write sets, keyed as a restore groups them, that lost a document to
|
|
429
|
+
* another client's deletion. A restore cannot write such a set again
|
|
430
|
+
* whole, so it writes none of it. Weak, so a set goes once no write
|
|
431
|
+
* record holds it.
|
|
432
|
+
*/
|
|
433
|
+
protected readonly brokenSets = new WeakSet<object>();
|
|
434
|
+
/** The subscription to the host's `onDidChangeDirty`, disposed with the session. */
|
|
435
|
+
protected readonly dirtySubscription?: Disposable;
|
|
436
|
+
/** The subscription to the host's `onDidUpdateDocument`, disposed with the session. */
|
|
437
|
+
protected readonly updateSubscription?: Disposable;
|
|
438
|
+
/** The subscription to the host's `onDidDeleteDocument`, disposed with the session. */
|
|
439
|
+
protected readonly deletionSubscription?: Disposable;
|
|
440
|
+
/** The host's logger, or one that logs nothing. */
|
|
441
|
+
protected readonly logger: Logger;
|
|
86
442
|
|
|
87
443
|
constructor(
|
|
88
|
-
|
|
89
|
-
protected readonly
|
|
90
|
-
|
|
444
|
+
readonly clientId: string,
|
|
445
|
+
protected readonly host: DataSessionHost<TTransfer, TServer>,
|
|
446
|
+
readonly label: string
|
|
91
447
|
) {
|
|
92
|
-
this.
|
|
93
|
-
this.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
/** The identity every request is made under — the port's, not a second one. */
|
|
97
|
-
get clientId(): string {
|
|
98
|
-
return this.port.clientId;
|
|
448
|
+
this.logger = host.logger ?? new NoopLogger();
|
|
449
|
+
this.dirtySubscription = host.onDidChangeDirty?.(event => this.forgetSavedWrite(event));
|
|
450
|
+
this.updateSubscription = host.onDidUpdateDocument?.(event => this.forgetSupersededWrite(event));
|
|
451
|
+
this.deletionSubscription = host.onDidDeleteDocument?.(event => this.forgetDeletedDocument(event));
|
|
99
452
|
}
|
|
100
453
|
|
|
101
454
|
/**
|
|
102
|
-
* The connected, READY server proxy
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* addressing a dead connection with no error — handing it back per call
|
|
107
|
-
* makes the stale reference unrepresentable.
|
|
108
|
-
*
|
|
109
|
-
* Concurrent callers share one readiness promise, so `waitForReady` is
|
|
110
|
-
* awaited once per generation and not once per caller.
|
|
455
|
+
* The connected, READY server proxy once this session is registered on it,
|
|
456
|
+
* for protocol methods this session does not wrap. The proxy stamps
|
|
457
|
+
* nothing: pass this session's {@link clientId} to any method that carries
|
|
458
|
+
* one.
|
|
111
459
|
*/
|
|
112
|
-
async connected(): Promise<RpcProxy<
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
460
|
+
async connected(): Promise<RpcProxy<TServer>> {
|
|
461
|
+
// `async` so a disposed session REJECTS rather than throwing
|
|
462
|
+
// synchronously: the connection's own `connected` rejects, and a caller
|
|
463
|
+
// reaching for `.catch` on one of them would not catch the other.
|
|
464
|
+
this.assertLive();
|
|
465
|
+
const server = await this.host.connected();
|
|
466
|
+
// Again after the wait: a session disposed meanwhile must not register,
|
|
467
|
+
// since its dispose found nothing registered to end.
|
|
468
|
+
this.assertLive();
|
|
469
|
+
if (this.registeredOn !== server) {
|
|
470
|
+
const reconnected = this.registeredOn !== undefined;
|
|
471
|
+
this.registeredOn = server;
|
|
472
|
+
this.registration = this.register(server, reconnected);
|
|
119
473
|
}
|
|
120
|
-
await
|
|
121
|
-
|
|
474
|
+
await this.registration;
|
|
475
|
+
// And after the registration: a call waiting on it has not reached the
|
|
476
|
+
// wire, and sent now it would land after the session's close.
|
|
477
|
+
this.assertLive();
|
|
478
|
+
return server;
|
|
122
479
|
}
|
|
123
480
|
|
|
124
481
|
/**
|
|
125
|
-
* Open `uri` for editing and start watching it, in that order,
|
|
126
|
-
* the opened snapshot.
|
|
482
|
+
* Open `args.uri` for editing and start watching it, in that order,
|
|
483
|
+
* returning the opened snapshot.
|
|
127
484
|
*
|
|
128
485
|
* **The order is the whole reason this method exists.**
|
|
129
486
|
* `watchModelDocument` baselines its dedup fingerprint from the *current*
|
|
@@ -131,32 +488,384 @@ export class DataSession<TTransfer extends TransferElement> {
|
|
|
131
488
|
* baseline, and the first phase event after the open arrives as a spurious
|
|
132
489
|
* `'changed'` — which a widget that resets its in-memory root to the server
|
|
133
490
|
* view misreads as a concurrent third-party write, losing whatever the user
|
|
134
|
-
* had typed.
|
|
135
|
-
* here rather than documented and re-derived.
|
|
491
|
+
* had typed.
|
|
136
492
|
*
|
|
137
|
-
* Note that the returned snapshot's
|
|
138
|
-
*
|
|
493
|
+
* Note that the returned snapshot's model usually has no `diagnostics`:
|
|
494
|
+
* `open` settles at the integrity landmark, not at validation.
|
|
139
495
|
* Validity arrives asynchronously on `onDocumentUpdated`, or synchronously
|
|
140
496
|
* from `getModelDocument({ includeDiagnostics: true })`.
|
|
141
497
|
*/
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
498
|
+
openDocument(args: DataSessionOpenArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
|
|
499
|
+
return this.trackOpen(args.uri, args.options, opening =>
|
|
500
|
+
this.openAndWatch(
|
|
501
|
+
args.uri,
|
|
502
|
+
opening,
|
|
503
|
+
() => undefined,
|
|
504
|
+
server => server.openModelDocument({ ...args, clientId: this.clientId })
|
|
505
|
+
)
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Create a document that exists nowhere yet, open and watched for this
|
|
511
|
+
* session; it reaches disk with the first {@link saveDocument}. The server
|
|
512
|
+
* refuses a URI that exists on disk or that any client has open. The answer
|
|
513
|
+
* names the version the create made in `created`, which a write of what
|
|
514
|
+
* this session created is based on: the answered model can already be
|
|
515
|
+
* another client's write or an integrity repair.
|
|
516
|
+
*/
|
|
517
|
+
createDocument(args: DataSessionCreateArgs<TTransfer, TServer>): Promise<TransferCreatedDocument<TTransfer, DiagnosticOf<TServer>>> {
|
|
518
|
+
return this.trackOpen(args.uri, undefined, opening =>
|
|
519
|
+
this.openAndWatch(
|
|
520
|
+
args.uri,
|
|
521
|
+
opening,
|
|
522
|
+
document => document.created.version,
|
|
523
|
+
server => server.createModelDocument({ ...args, clientId: this.clientId })
|
|
524
|
+
)
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Send `open` and watch what it answered, again for as long as a deletion
|
|
530
|
+
* heard while the watch is under way closes that open: sent after the
|
|
531
|
+
* deletion, the open fails as an open of a gone file does, or opens the
|
|
532
|
+
* file that is back. `created` reads the version the create made off the
|
|
533
|
+
* answered document, or none for an open; see {@link watchOpened} for the
|
|
534
|
+
* mark it puts in {@link createdUris}.
|
|
535
|
+
*
|
|
536
|
+
* An `opening` a close marked answers with the document, recording nothing
|
|
537
|
+
* and sending nothing again; one it marked before the open answered is
|
|
538
|
+
* undone, since the server may have taken the close first. A failed watch
|
|
539
|
+
* is undone too: the caller sees a failed open, and so would never close it.
|
|
540
|
+
*/
|
|
541
|
+
protected async openAndWatch<TDocument extends DataSessionDocument<TTransfer, TServer>>(
|
|
542
|
+
uri: string,
|
|
543
|
+
opening: DataSessionOpening<TTransfer, TServer>,
|
|
544
|
+
created: (document: TDocument) => ModelVersion | undefined,
|
|
545
|
+
open: (server: RpcProxy<TServer>) => Promise<TDocument>
|
|
546
|
+
): Promise<TDocument> {
|
|
547
|
+
for (;;) {
|
|
548
|
+
const server = await this.connected();
|
|
549
|
+
const document = await open(server);
|
|
550
|
+
if (opening.closed) {
|
|
551
|
+
await this.undoOpen(server, uri, opening);
|
|
552
|
+
return document;
|
|
553
|
+
}
|
|
554
|
+
let watched: TDocument | undefined;
|
|
555
|
+
try {
|
|
556
|
+
watched = await this.watchOpened(server, uri, document, created(document));
|
|
557
|
+
} catch (error: unknown) {
|
|
558
|
+
await this.undoOpen(server, uri, opening);
|
|
559
|
+
throw error;
|
|
560
|
+
}
|
|
561
|
+
if (watched || opening.closed) {
|
|
562
|
+
return watched ?? document;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Watch a document this session just opened, and record it as open.
|
|
569
|
+
* Answers `undefined`, recording nothing, when a deletion heard while the
|
|
570
|
+
* watch is under way has ended the open already, and the document,
|
|
571
|
+
* recording nothing, when a close has. A failed watch rejects with the open
|
|
572
|
+
* still recorded, for its caller to undo, see {@link undoOpen}.
|
|
573
|
+
*
|
|
574
|
+
* A document created at `createdVersion` is marked in {@link createdUris}
|
|
575
|
+
* while its text is still at that version: a version past it is another's,
|
|
576
|
+
* a write or a repair, as a changed event would have said. Marked before the
|
|
577
|
+
* watch, so an event heard while it is under way clears the mark for good,
|
|
578
|
+
* and checked again against the text the watch answered, which an event
|
|
579
|
+
* sent before the watch was in place moved on unheard. The restore's
|
|
580
|
+
* re-open is the one other point the session learns of the text without
|
|
581
|
+
* hearing the events between, and checks the mark the same way. An answer
|
|
582
|
+
* without text leaves the mark as it is, at every one of the three.
|
|
583
|
+
*/
|
|
584
|
+
protected async watchOpened<TDocument extends DataSessionDocument<TTransfer, TServer>>(
|
|
585
|
+
server: RpcProxy<TServer>,
|
|
586
|
+
uri: string,
|
|
587
|
+
document: TDocument,
|
|
588
|
+
createdVersion?: ModelVersion
|
|
589
|
+
): Promise<TDocument | undefined> {
|
|
590
|
+
// Recorded before the watch, so a deletion heard while it is under way
|
|
591
|
+
// finds this open, which the server made before the deletion.
|
|
592
|
+
// Under the options of the earliest open still under way, which the
|
|
593
|
+
// server took first; one that failed is gone from `openings` by now,
|
|
594
|
+
// as the connection handles each answer in a task of its own.
|
|
595
|
+
const open = this.opens.get(uri) ?? {
|
|
596
|
+
serverUri: document.uri,
|
|
597
|
+
deleted: false,
|
|
598
|
+
options: [...(this.openings.get(uri) ?? [])].find(opening => !opening.closed)?.options
|
|
599
|
+
};
|
|
600
|
+
this.opens.set(uri, open);
|
|
601
|
+
if (createdVersion !== undefined && (document.text === undefined || document.text.version === createdVersion)) {
|
|
602
|
+
this.createdUris.add(uri);
|
|
603
|
+
}
|
|
604
|
+
const watched = await server.watchModelDocument({ uri, clientId: this.clientId });
|
|
605
|
+
if (createdVersion !== undefined && watched.text !== undefined && watched.text.version !== createdVersion) {
|
|
606
|
+
this.createdUris.delete(uri);
|
|
607
|
+
}
|
|
608
|
+
if (this.opens.get(uri) !== open) {
|
|
609
|
+
// Sent again, it ends as an open sent after the deletion would; a
|
|
610
|
+
// close is the caller's later call, and stands.
|
|
611
|
+
return open.deleted ? undefined : document;
|
|
612
|
+
}
|
|
613
|
+
this.openUris.add(uri);
|
|
614
|
+
this.recordText(uri, document);
|
|
615
|
+
// Keyed as the server keys its notifications, which may not be how the
|
|
616
|
+
// caller spelled the URI.
|
|
617
|
+
this.host.forgetDirty?.(document.uri);
|
|
146
618
|
return document;
|
|
147
619
|
}
|
|
148
620
|
|
|
149
621
|
/**
|
|
150
|
-
* Close `uri
|
|
151
|
-
* {@link
|
|
622
|
+
* Close `args.uri`, once this session's calls on it have settled or
|
|
623
|
+
* {@link settleBeforeCloseMs} has passed. The server unwatches implicitly,
|
|
624
|
+
* so this is the dual of {@link openDocument} and needs no separate unwatch.
|
|
625
|
+
*/
|
|
626
|
+
async closeDocument(args: DataSessionCloseArgs<TTransfer, TServer>): Promise<void> {
|
|
627
|
+
this.assertLive();
|
|
628
|
+
// An open called after this close waits for it instead, see `trackOpen`.
|
|
629
|
+
const underWay = [...(this.openings.get(args.uri) ?? [])];
|
|
630
|
+
await this.trackEnding(args.uri, async () => {
|
|
631
|
+
await this.settle([this.inFlight.get(args.uri)]);
|
|
632
|
+
// Forgotten before the close is sent, so a reconnect in between does
|
|
633
|
+
// not re-open a document the caller has closed. Every open of it that
|
|
634
|
+
// was under way when the close was called is marked, so it neither
|
|
635
|
+
// records it again nor is sent again.
|
|
636
|
+
underWay.forEach(opening => {
|
|
637
|
+
opening.closed = true;
|
|
638
|
+
});
|
|
639
|
+
this.forgetDocument(args.uri);
|
|
640
|
+
const server = await this.connected();
|
|
641
|
+
await server.closeModelDocument({ ...args, clientId: this.clientId });
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Close `uri` again after `opening` failed or was closed while under way,
|
|
647
|
+
* and drop what it recorded, unless the session still relies on the open,
|
|
648
|
+
* see {@link hasOpen}: the server holds one open per session and URI, so
|
|
649
|
+
* the close would end that one too.
|
|
152
650
|
*/
|
|
153
|
-
async
|
|
154
|
-
|
|
155
|
-
|
|
651
|
+
protected async undoOpen(server: RpcProxy<TServer>, uri: string, opening: DataSessionOpening<TTransfer, TServer>): Promise<void> {
|
|
652
|
+
if (this.hasOpen(uri, opening)) {
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
this.opens.delete(uri);
|
|
656
|
+
this.createdUris.delete(uri);
|
|
657
|
+
await server.closeModelDocument({ uri, clientId: this.clientId }).catch(() => undefined);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** Drop everything this session keeps of `uri`, which it no longer has open. */
|
|
661
|
+
protected forgetDocument(uri: string): void {
|
|
662
|
+
const serverUri = this.opens.get(uri)?.serverUri ?? uri;
|
|
663
|
+
this.openUris.delete(uri);
|
|
664
|
+
this.opens.delete(uri);
|
|
665
|
+
this.unsavedWrites.delete(uri);
|
|
666
|
+
this.lastAnswers.delete(uri);
|
|
667
|
+
this.savedVersions.delete(uri);
|
|
668
|
+
this.createdUris.delete(uri);
|
|
669
|
+
this.host.forgetDirty?.(serverUri);
|
|
156
670
|
}
|
|
157
671
|
|
|
158
672
|
/**
|
|
159
|
-
* Whether `
|
|
673
|
+
* Whether this session has `uri` open, or has an open of it under way that
|
|
674
|
+
* no close has marked, other than `except`. A marked open records nothing,
|
|
675
|
+
* so nothing can rely on it.
|
|
676
|
+
*/
|
|
677
|
+
protected hasOpen(uri: string, except?: DataSessionOpening<TTransfer, TServer>): boolean {
|
|
678
|
+
return this.openUris.has(uri) || [...(this.openings.get(uri) ?? [])].some(opening => opening !== except && !opening.closed);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Open `args.uri`, run `fn` with the opened snapshot, and close it once
|
|
683
|
+
* `fn` settles, whether it returned or threw. A URI this session already
|
|
684
|
+
* had open, or is still opening through another call, stays open: the close
|
|
685
|
+
* undoes only the open this call made.
|
|
686
|
+
*/
|
|
687
|
+
async withOpenDocument<T>(
|
|
688
|
+
args: DataSessionOpenArgs<TTransfer, TServer>,
|
|
689
|
+
fn: (document: DataSessionDocument<TTransfer, TServer>) => MaybePromise<T>
|
|
690
|
+
): Promise<T> {
|
|
691
|
+
// Asked once the closes and deletes of the URI under way have answered:
|
|
692
|
+
// before, the session still counts as open a document they are ending.
|
|
693
|
+
for (let ending = this.endings.get(args.uri); ending; ending = this.endings.get(args.uri)) {
|
|
694
|
+
await ending;
|
|
695
|
+
}
|
|
696
|
+
const alreadyOpen = this.hasOpen(args.uri);
|
|
697
|
+
const document = await this.openDocument(args);
|
|
698
|
+
try {
|
|
699
|
+
return await fn(document);
|
|
700
|
+
} finally {
|
|
701
|
+
if (!alreadyOpen && !this.disposed) {
|
|
702
|
+
await this.closeDocument({ uri: args.uri } as DataSessionCloseArgs<TTransfer, TServer>);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/** Write `args.model` back as this session. The session must have `args.uri` open. */
|
|
708
|
+
updateDocument(args: DataSessionUpdateArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
|
|
709
|
+
return this.trackWrite([args.uri], async () => {
|
|
710
|
+
const server = await this.connected();
|
|
711
|
+
const update = { ...args };
|
|
712
|
+
const baseHash = await this.baseHashOf(server, args.uri, args.baseVersion);
|
|
713
|
+
const open = this.opens.get(args.uri);
|
|
714
|
+
const document = await server.updateModelDocument({ ...args, clientId: this.clientId });
|
|
715
|
+
this.recordWrite(args.uri, open, document, baseHash, { update });
|
|
716
|
+
return document;
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* Write several documents this session has open, all or none: the server
|
|
722
|
+
* refuses the whole set, before any text applies, when one is stale or not
|
|
723
|
+
* open. Resolves to the documents in the order given.
|
|
724
|
+
*/
|
|
725
|
+
updateDocuments(args: DataSessionUpdatesArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>[]> {
|
|
726
|
+
return this.trackWrite(
|
|
727
|
+
args.updates.map(update => update.uri),
|
|
728
|
+
async () => {
|
|
729
|
+
// One copy for the whole call: the copy's identity is what ties its
|
|
730
|
+
// records together.
|
|
731
|
+
const updates = { ...args, updates: args.updates.map(update => ({ ...update })) };
|
|
732
|
+
const server = await this.connected();
|
|
733
|
+
const baseHashes = await Promise.all(args.updates.map(update => this.baseHashOf(server, update.uri, update.baseVersion)));
|
|
734
|
+
const opens = args.updates.map(update => this.opens.get(update.uri));
|
|
735
|
+
const documents = await server.updateModelDocuments({ ...args, clientId: this.clientId });
|
|
736
|
+
args.updates.forEach((update, index) =>
|
|
737
|
+
this.recordWrite(update.uri, opens[index], documents[index], baseHashes[index], { updates, index })
|
|
738
|
+
);
|
|
739
|
+
return documents;
|
|
740
|
+
}
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/** Persist `args.model` to disk as this session. The session must have `args.uri` open. */
|
|
745
|
+
saveDocument(args: DataSessionSaveArgs<TTransfer, TServer>): Promise<TransferSavedDocument<TTransfer, DiagnosticOf<TServer>>> {
|
|
746
|
+
return this.trackSave(args.uri, server => server.saveModelDocument({ ...args, clientId: this.clientId }));
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Persist the text the server holds for `args.uri` as this session, with no
|
|
751
|
+
* model of its own. The session must have `args.uri` open.
|
|
752
|
+
*/
|
|
753
|
+
persistDocument(args: DataSessionPersistArgs<TTransfer, TServer>): Promise<TransferSavedDocument<TTransfer, DiagnosticOf<TServer>>> {
|
|
754
|
+
return this.trackSave(args.uri, server => server.persistModelDocument({ ...args, clientId: this.clientId }));
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Delete `args.uri` as this session, at once: its file, then the document.
|
|
759
|
+
* The session must have `args.uri` open. Sent once this session's calls in
|
|
760
|
+
* flight on the URI have settled, as {@link closeDocument} is, and counted
|
|
761
|
+
* in {@link hasSavesInFlight} until it answers.
|
|
762
|
+
*
|
|
763
|
+
* The session forgets the document once the delete has succeeded: on
|
|
764
|
+
* hearing its own deletion, which the server sends before it answers, or
|
|
765
|
+
* on the answer, for a client that hears no deletions. A refused delete
|
|
766
|
+
* leaves the document open, as the server does.
|
|
767
|
+
*/
|
|
768
|
+
deleteDocument(args: DataSessionDeleteArgs<TTransfer, TServer>): Promise<void> {
|
|
769
|
+
const deleting = this.trackEnding(args.uri, () =>
|
|
770
|
+
this.track([args.uri], async () => {
|
|
771
|
+
this.assertLive();
|
|
772
|
+
await this.settle([this.inFlight.get(args.uri)]);
|
|
773
|
+
const server = await this.connected();
|
|
774
|
+
const open = this.opens.get(args.uri);
|
|
775
|
+
await server.deleteModelDocument({ ...args, clientId: this.clientId });
|
|
776
|
+
// Still the open it was sent under: the deletion was not heard.
|
|
777
|
+
if (open && this.opens.get(args.uri) === open) {
|
|
778
|
+
open.deleted = true;
|
|
779
|
+
this.logger.withUri(args.uri).debug('Forgot the document: this session deleted it');
|
|
780
|
+
this.forgetDocument(args.uri);
|
|
781
|
+
}
|
|
782
|
+
})
|
|
783
|
+
);
|
|
784
|
+
return this.savesInFlight.track(deleting);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/** Run a save or persist of `uri` and record what it answered as saved. */
|
|
788
|
+
protected trackSave(
|
|
789
|
+
uri: string,
|
|
790
|
+
send: (server: RpcProxy<TServer>) => Promise<TransferSavedDocument<TTransfer, DiagnosticOf<TServer>>>
|
|
791
|
+
): Promise<TransferSavedDocument<TTransfer, DiagnosticOf<TServer>>> {
|
|
792
|
+
const saving = this.track([uri], async () => {
|
|
793
|
+
const server = await this.connected();
|
|
794
|
+
const open = this.opens.get(uri);
|
|
795
|
+
const document = await send(server);
|
|
796
|
+
// Sent under no open, or under one a close or deletion has ended
|
|
797
|
+
// since: recorded, it would outlive that open.
|
|
798
|
+
if (!open || this.opens.get(uri) !== open) {
|
|
799
|
+
return document;
|
|
800
|
+
}
|
|
801
|
+
// Kept only when its last answer is newer than the text written. Kept
|
|
802
|
+
// whenever it changed while the save ran, a record the save covered
|
|
803
|
+
// survives, and a restore then reports it lost.
|
|
804
|
+
const unsaved = this.unsavedWrites.get(uri);
|
|
805
|
+
if (unsaved && unsaved.answer.version <= document.persisted.version) {
|
|
806
|
+
this.unsavedWrites.delete(uri);
|
|
807
|
+
}
|
|
808
|
+
this.recordText(uri, document);
|
|
809
|
+
this.createdUris.delete(uri);
|
|
810
|
+
// The higher of two saves' answers, which may arrive out of order.
|
|
811
|
+
const saved = this.savedVersions.get(uri);
|
|
812
|
+
if (saved === undefined || document.persisted.version > saved) {
|
|
813
|
+
this.savedVersions.set(uri, document.persisted.version);
|
|
814
|
+
}
|
|
815
|
+
return document;
|
|
816
|
+
});
|
|
817
|
+
return this.savesInFlight.track(saving);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
/** Whether the session has ended, by {@link dispose} or {@link detach}. */
|
|
821
|
+
get isDisposed(): boolean {
|
|
822
|
+
return this.disposed;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* Whether a save, persist or delete of this session has not answered yet.
|
|
827
|
+
* Synchronous, for a host whose exit veto must decide within the tick, such
|
|
828
|
+
* as Theia's `onWillStop`.
|
|
829
|
+
*/
|
|
830
|
+
get hasSavesInFlight(): boolean {
|
|
831
|
+
return !this.savesInFlight.isIdle;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Resolves once the saves in flight now have answered, or
|
|
836
|
+
* {@link settleBeforeCloseMs} has passed. Never rejects: a failed save has
|
|
837
|
+
* answered too.
|
|
838
|
+
*/
|
|
839
|
+
whenSavesSettled(): Promise<void> {
|
|
840
|
+
return this.settle([this.savesInFlight]);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* Whether an update of this session, one a restore sends again included,
|
|
845
|
+
* has not answered yet. The server answers once an editor on the language
|
|
846
|
+
* client holds the text, so a page that reads unsaved work from its editor
|
|
847
|
+
* guards a reload on this too. Synchronous, for an unload guard that must
|
|
848
|
+
* decide within the tick.
|
|
849
|
+
*/
|
|
850
|
+
get hasWritesInFlight(): boolean {
|
|
851
|
+
return !this.writesInFlight.isIdle;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Resolves to `true` once the updates in flight now have answered, so a
|
|
856
|
+
* save of the editor's documents started after it includes them, or to
|
|
857
|
+
* `false` once {@link settleWritesMs} has passed first. An update sent while
|
|
858
|
+
* it waits is not waited for. Never rejects.
|
|
859
|
+
*/
|
|
860
|
+
async whenWritesSettled(): Promise<boolean> {
|
|
861
|
+
if (this.writesInFlight.isIdle) {
|
|
862
|
+
return true;
|
|
863
|
+
}
|
|
864
|
+
return (await this.clock.raceTimer(this.writesInFlight.whenSettled(), this.settleWritesMs)) !== TIMED_OUT;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/**
|
|
868
|
+
* Whether `sourceClientId` identifies this session's own write.
|
|
160
869
|
*
|
|
161
870
|
* Every watcher needs this and the check is one comparison, so getting it
|
|
162
871
|
* wrong is cheap to do and expensive to find: an unfiltered echo looks
|
|
@@ -166,63 +875,686 @@ export class DataSession<TTransfer extends TransferElement> {
|
|
|
166
875
|
return sourceClientId === this.clientId;
|
|
167
876
|
}
|
|
168
877
|
|
|
169
|
-
/**
|
|
878
|
+
/**
|
|
879
|
+
* End the session: detach it from the connection at once, and once its
|
|
880
|
+
* calls in flight have settled or {@link settleBeforeCloseMs} has passed,
|
|
881
|
+
* end it on the server, which closes everything it has open. Idempotent,
|
|
882
|
+
* and leaves the connection usable by its other sessions.
|
|
883
|
+
*
|
|
884
|
+
* Every later call rejects, and so does a call made earlier in the same
|
|
885
|
+
* tick, which has not reached the wire yet and is never sent. The server
|
|
886
|
+
* close is not awaited, because a `Disposable` cannot be; a close that fails
|
|
887
|
+
* leaves the session to the server's connection-close cleanup.
|
|
888
|
+
*/
|
|
170
889
|
dispose(): void {
|
|
171
890
|
if (this.disposed) {
|
|
172
891
|
return;
|
|
173
892
|
}
|
|
174
893
|
this.disposed = true;
|
|
175
|
-
this.
|
|
176
|
-
this.
|
|
894
|
+
this.fireDispose();
|
|
895
|
+
const settled = this.settle([...this.inFlight.values()]);
|
|
896
|
+
void (async () => {
|
|
897
|
+
await settled;
|
|
898
|
+
await this.registration;
|
|
899
|
+
// The proxy the session registered on, never a fresh connection: a
|
|
900
|
+
// connection that dropped already ended the session on the server.
|
|
901
|
+
if (!this.detached && this.registeredOn) {
|
|
902
|
+
await this.registeredOn.closeSession({ clientId: this.clientId });
|
|
903
|
+
}
|
|
904
|
+
})().catch(() => undefined);
|
|
177
905
|
}
|
|
178
906
|
|
|
179
|
-
/**
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
907
|
+
/**
|
|
908
|
+
* Come off the connection because it is going away.
|
|
909
|
+
*
|
|
910
|
+
* Sends nothing, unlike {@link dispose}: the server ends every session on a
|
|
911
|
+
* connection it sees close, and the close would travel over the very
|
|
912
|
+
* connection being disposed.
|
|
913
|
+
*
|
|
914
|
+
* Public because the connection calls it; anyone else ends a session with
|
|
915
|
+
* {@link dispose}. It fires {@link onDidDispose} only if the session has not
|
|
916
|
+
* already ended, and after {@link dispose} it still cancels the close that
|
|
917
|
+
* dispose has not sent yet.
|
|
918
|
+
*/
|
|
919
|
+
detach(): void {
|
|
920
|
+
this.detached = true;
|
|
921
|
+
this.openUris.clear();
|
|
922
|
+
this.opens.clear();
|
|
923
|
+
this.unsavedWrites.clear();
|
|
924
|
+
this.lastAnswers.clear();
|
|
925
|
+
this.savedVersions.clear();
|
|
926
|
+
this.createdUris.clear();
|
|
927
|
+
if (!this.disposed) {
|
|
928
|
+
this.disposed = true;
|
|
929
|
+
this.fireDispose();
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/**
|
|
934
|
+
* Dispose {@link dirtySubscription}, {@link updateSubscription} and
|
|
935
|
+
* {@link deletionSubscription}, fire {@link onDidDispose} and dispose its
|
|
936
|
+
* emitter, so a second call fires nothing.
|
|
937
|
+
*/
|
|
938
|
+
protected fireDispose(): void {
|
|
939
|
+
this.dirtySubscription?.dispose();
|
|
940
|
+
this.updateSubscription?.dispose();
|
|
941
|
+
this.deletionSubscription?.dispose();
|
|
942
|
+
this.disposeEmitter.fire(undefined);
|
|
943
|
+
this.disposeEmitter.dispose();
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Register again and restore now, after the connection dropped, instead of
|
|
948
|
+
* on the next call. A no-op for a session with nothing open, which the next
|
|
949
|
+
* call restores anyway, and for one already registered on the current
|
|
950
|
+
* connection. A failure is retried on the connection's next ready
|
|
951
|
+
* generation; a call before then meets it again.
|
|
952
|
+
*/
|
|
953
|
+
reconnect(): void {
|
|
954
|
+
if (!this.disposed && this.openUris.size > 0) {
|
|
955
|
+
this.connected().catch(() => undefined);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* Drop the record in {@link unsavedWrites} of a document this session has
|
|
961
|
+
* open once the server says it turned clean, and its mark in
|
|
962
|
+
* {@link createdUris}: it holds no unsaved text then, and a record kept
|
|
963
|
+
* would have a restore report the write lost once another client's save
|
|
964
|
+
* changed the file.
|
|
965
|
+
*
|
|
966
|
+
* The server sends a flip only for a document someone watches, so a
|
|
967
|
+
* document the session writes without watching it keeps its record, and
|
|
968
|
+
* can still be reported lost that way.
|
|
969
|
+
*/
|
|
970
|
+
protected forgetSavedWrite(event: TransferDocumentDirtyChangedEvent): void {
|
|
971
|
+
if (event.text?.dirty) {
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
// The flip names the server's key, which is not always the caller's
|
|
975
|
+
// spelling that the records are kept under.
|
|
976
|
+
for (const [uri, open] of this.opens) {
|
|
977
|
+
if (open.serverUri === event.uri) {
|
|
978
|
+
this.unsavedWrites.delete(uri);
|
|
979
|
+
this.createdUris.delete(uri);
|
|
980
|
+
}
|
|
183
981
|
}
|
|
184
|
-
const connection = this.port.connect();
|
|
185
|
-
// Rejection is reported here rather than left to float: an unhandled
|
|
186
|
-
// rejection on a connection promise is the failure mode that reads as
|
|
187
|
-
// "the model is empty" instead of "the transport never opened".
|
|
188
|
-
connection.catch((error: unknown) =>
|
|
189
|
-
this.port.reportError(error, resolve(DATA_SERVER_CONNECT_FAILED, { detail: describeError(error) }))
|
|
190
|
-
);
|
|
191
|
-
const server = createRpcProxy<DataServerProtocol<TTransfer>, DataClientProtocol<TTransfer>>(connection, {
|
|
192
|
-
methodNamespace: this.methodNamespace,
|
|
193
|
-
localTarget: this.client,
|
|
194
|
-
localMethods: DATA_CLIENT_PROTOCOL_METHODS
|
|
195
|
-
});
|
|
196
|
-
this.generation = { connection, server };
|
|
197
|
-
return this.generation;
|
|
198
982
|
}
|
|
199
983
|
|
|
200
|
-
/**
|
|
201
|
-
|
|
984
|
+
/**
|
|
985
|
+
* Drop the record in {@link unsavedWrites} of a document another client
|
|
986
|
+
* wrote over while the connection held: its write replaced the session's,
|
|
987
|
+
* so a restore has nothing of the session's to put back, and a record kept
|
|
988
|
+
* would have it report the write lost to the reconnect. A write superseded
|
|
989
|
+
* before its answer needs nothing: the answer names the other client's
|
|
990
|
+
* text, which the record then holds.
|
|
991
|
+
*
|
|
992
|
+
* Only a `'changed'` event from another client counts, and only where the
|
|
993
|
+
* document no longer holds the write: the session's own echo, and an
|
|
994
|
+
* integrity repair already in the write's answer, replaced nothing, and a
|
|
995
|
+
* `'rebuilt'` event carries no new text. Like {@link forgetSavedWrite}, it
|
|
996
|
+
* depends on the server sending the event, which it does only for a
|
|
997
|
+
* document someone watches.
|
|
998
|
+
*
|
|
999
|
+
* The mark in {@link createdUris} goes on any such event: the unsaved text
|
|
1000
|
+
* is then the other client's, and a record still kept reports the
|
|
1001
|
+
* session's own.
|
|
1002
|
+
*/
|
|
1003
|
+
protected forgetSupersededWrite(event: TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>): void {
|
|
1004
|
+
if (event.reason !== 'changed' || this.isOwnEcho(event.sourceClientId)) {
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
for (const [uri, open] of this.opens) {
|
|
1008
|
+
if (open.serverUri !== event.document.uri) {
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
this.createdUris.delete(uri);
|
|
1012
|
+
const write = this.unsavedWrites.get(uri);
|
|
1013
|
+
if (write && this.restoreOutcome(write, event.document) !== 'kept') {
|
|
1014
|
+
this.unsavedWrites.delete(uri);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* Forget a document whose file was deleted, which closed this session's
|
|
1021
|
+
* open. At once rather than after the calls in flight settle, which would
|
|
1022
|
+
* let a restore re-open the file that is gone. Unwatched rather than
|
|
1023
|
+
* closed: the server keeps the watch, and a close could end a re-open of a
|
|
1024
|
+
* re-created file still in flight. Unsaved writes are logged, not
|
|
1025
|
+
* reported: the client hears the deletion too. A document this session
|
|
1026
|
+
* created and never saved, deleted by no session, is reported as well: a
|
|
1027
|
+
* drop released it. An unsaved write breaks the set it was written with,
|
|
1028
|
+
* see {@link brokenSets}.
|
|
1029
|
+
*
|
|
1030
|
+
* This session's own deletion is neither unwatched nor logged as dropping
|
|
1031
|
+
* unsaved text: its delete released the watch, and its caller chose to
|
|
1032
|
+
* drop the text.
|
|
1033
|
+
*/
|
|
1034
|
+
protected forgetDeletedDocument(event: TransferDocumentDeletedEvent): void {
|
|
1035
|
+
for (const [uri, open] of this.opens) {
|
|
1036
|
+
if (open.serverUri !== event.uri) {
|
|
1037
|
+
continue;
|
|
1038
|
+
}
|
|
1039
|
+
open.deleted = true;
|
|
1040
|
+
if (this.isOwnEcho(event.sourceClientId)) {
|
|
1041
|
+
this.logger.withUri(uri).debug('Forgot the document: this session deleted it');
|
|
1042
|
+
this.forgetDocument(uri);
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
const write = this.unsavedWrites.get(uri);
|
|
1046
|
+
if (write) {
|
|
1047
|
+
this.brokenSets.add(setOf(write));
|
|
1048
|
+
}
|
|
1049
|
+
// Never saved, so no file a watcher could see removed: a deletion
|
|
1050
|
+
// naming no session is the release of the text a drop left behind.
|
|
1051
|
+
const released = this.createdUris.has(uri) && event.sourceClientId === UNKNOWN_CLIENT_ID;
|
|
1052
|
+
this.forgetGoneDocument(uri);
|
|
1053
|
+
if (released) {
|
|
1054
|
+
const reported = resolve(DATA_SESSION_UNSAVED_LOST, { uris: uri });
|
|
1055
|
+
this.host.reportError?.(new Error(reported.text), reported);
|
|
1056
|
+
}
|
|
1057
|
+
this.connected()
|
|
1058
|
+
.then(server => server.unwatchModelDocument({ uri, clientId: this.clientId }))
|
|
1059
|
+
.catch(() => undefined);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* {@link forgetDocument} a document that is gone, logging any unsaved text
|
|
1065
|
+
* it drops, written or created; returns whether it dropped some.
|
|
1066
|
+
*/
|
|
1067
|
+
protected forgetGoneDocument(uri: string): boolean {
|
|
1068
|
+
const unsaved = this.unsavedWrites.has(uri) || this.createdUris.has(uri);
|
|
1069
|
+
if (unsaved) {
|
|
1070
|
+
this.logger.withUri(uri).warn('Dropped unsaved text: the document is gone');
|
|
1071
|
+
} else {
|
|
1072
|
+
this.logger.withUri(uri).debug('Forgot the document: it is gone');
|
|
1073
|
+
}
|
|
1074
|
+
this.forgetDocument(uri);
|
|
1075
|
+
return unsaved;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
* Register on `server`, and after a reconnect restore what the ended session
|
|
1080
|
+
* had. Calls issued meanwhile wait for this.
|
|
1081
|
+
*/
|
|
1082
|
+
protected async register(server: RpcProxy<TServer>, reconnected: boolean): Promise<void> {
|
|
1083
|
+
await server.createSession({ clientId: this.clientId, label: this.label, resumeToken: this.resumeToken });
|
|
1084
|
+
if (reconnected) {
|
|
1085
|
+
await this.restore(server);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* Re-open, each under the options its {@link DataSessionOpen} holds, and
|
|
1091
|
+
* re-watch every document the session had open, write again what it wrote
|
|
1092
|
+
* since their last save where the re-open lost it, and tell the host which
|
|
1093
|
+
* of them lost it for good.
|
|
1094
|
+
*
|
|
1095
|
+
* Decided per document by the re-opened document's `text.hash`,
|
|
1096
|
+
* in {@link restoreOutcome}. Versions cannot decide a write: a revert moves
|
|
1097
|
+
* the version on and a restarted server numbers afresh, so the version the
|
|
1098
|
+
* last write was answered with never matches where a write is needed, and
|
|
1099
|
+
* any other version may be another client's edit, which a write would
|
|
1100
|
+
* overwrite.
|
|
1101
|
+
*
|
|
1102
|
+
* Only what the drop cost is reported: a write another client wrote over
|
|
1103
|
+
* while the connection held has no record left by then, see
|
|
1104
|
+
* {@link forgetSupersededWrite}.
|
|
1105
|
+
*
|
|
1106
|
+
* A write is sent again based on the re-opened version, as an ordinary
|
|
1107
|
+
* write, so an edit arriving in between conflicts, and a conflict is not
|
|
1108
|
+
* retried. Documents last written by one {@link updateDocuments} are sent
|
|
1109
|
+
* again by one, and only when every one of them may be; a document that
|
|
1110
|
+
* could not be re-opened, or that another client deleted, blocks its set.
|
|
1111
|
+
*
|
|
1112
|
+
* No caller is waiting, so the outcomes go through the host: one report
|
|
1113
|
+
* naming every document whose unsaved text is gone, whose record is then
|
|
1114
|
+
* dropped, and one per document that could not be re-opened or written
|
|
1115
|
+
* again for another reason than a conflict. A document that could not be
|
|
1116
|
+
* re-opened is forgotten; a failed write keeps its record, for the next
|
|
1117
|
+
* restore to decide again. A document closed while the restore runs, or
|
|
1118
|
+
* whose file is deleted then, is neither restored nor reported. One gone
|
|
1119
|
+
* while the connection was down, which the server reads back without a
|
|
1120
|
+
* model, is forgotten, and its deletion goes to the host as the deletion
|
|
1121
|
+
* no event brought; unsaved text it took with it is logged, as for a
|
|
1122
|
+
* deletion heard live, and reported as well for a document the session
|
|
1123
|
+
* created and never saved, which the drop released. A host that cannot
|
|
1124
|
+
* tell the client has the unsaved text of either reported. Once the session
|
|
1125
|
+
* is disposed, its caller has ended it, and the restore returns at its
|
|
1126
|
+
* next await: nothing more goes to the server or the host.
|
|
1127
|
+
*
|
|
1128
|
+
* Each document's dirty state goes to the host too, read once the watch is
|
|
1129
|
+
* in place and any write is sent: a flip while the connection was down, or
|
|
1130
|
+
* between the re-open and the watch, reached no one, and the re-open's own
|
|
1131
|
+
* answer misses the second. Read before the write, it would tell the client
|
|
1132
|
+
* a document is clean that the write is about to make dirty again.
|
|
1133
|
+
*/
|
|
1134
|
+
protected async restore(server: RpcProxy<TServer>): Promise<void> {
|
|
1135
|
+
const writes = [...this.unsavedWrites];
|
|
1136
|
+
// No write sent before the drop answers any more, and a restarted
|
|
1137
|
+
// server numbers afresh, below the versions saved before it.
|
|
1138
|
+
this.savedVersions.clear();
|
|
1139
|
+
const reopened = new Map<string, DataSessionDocument<TTransfer, TServer>>();
|
|
1140
|
+
const lost: string[] = [];
|
|
1141
|
+
for (const uri of [...this.openUris]) {
|
|
1142
|
+
// Closed, or its file deleted, while the restore ran.
|
|
1143
|
+
if (!this.openUris.has(uri)) {
|
|
1144
|
+
continue;
|
|
1145
|
+
}
|
|
1146
|
+
try {
|
|
1147
|
+
const options = this.opens.get(uri)?.options;
|
|
1148
|
+
const document = await server.openModelDocument({ uri, clientId: this.clientId, options });
|
|
1149
|
+
if (this.disposed) {
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
if (!this.openUris.has(uri)) {
|
|
1153
|
+
// Forgotten while it re-opened. A close of the caller's would
|
|
1154
|
+
// follow, but a deletion sends none.
|
|
1155
|
+
await server.closeModelDocument({ uri, clientId: this.clientId }).catch(() => undefined);
|
|
1156
|
+
continue;
|
|
1157
|
+
}
|
|
1158
|
+
// Recorded before the watch, so a deletion heard while it is under
|
|
1159
|
+
// way finds the re-open under the URI it answered with, which a
|
|
1160
|
+
// restarted server may key differently.
|
|
1161
|
+
this.opens.set(uri, { serverUri: document.uri, deleted: false, options });
|
|
1162
|
+
await server.watchModelDocument({ uri, clientId: this.clientId }).catch(async (error: unknown) => {
|
|
1163
|
+
// Closed, or the server holds the re-open until the session
|
|
1164
|
+
// ends; no other call relies on it while the restore runs.
|
|
1165
|
+
await server.closeModelDocument({ uri, clientId: this.clientId }).catch(() => undefined);
|
|
1166
|
+
throw error;
|
|
1167
|
+
});
|
|
1168
|
+
if (this.disposed) {
|
|
1169
|
+
return;
|
|
1170
|
+
}
|
|
1171
|
+
if (this.openUris.has(uri)) {
|
|
1172
|
+
// Text other than this session last saw was written while the
|
|
1173
|
+
// connection was down, by an event it never heard.
|
|
1174
|
+
const known = this.lastAnswers.get(uri);
|
|
1175
|
+
if (this.createdUris.has(uri) && document.text !== undefined && known !== undefined && document.text.hash !== known.hash) {
|
|
1176
|
+
this.createdUris.delete(uri);
|
|
1177
|
+
}
|
|
1178
|
+
this.recordText(uri, document);
|
|
1179
|
+
}
|
|
1180
|
+
reopened.set(uri, document);
|
|
1181
|
+
} catch (error: unknown) {
|
|
1182
|
+
// Gone while the connection was down, which no deletion told the
|
|
1183
|
+
// session of: the server answers a read of it without a model.
|
|
1184
|
+
const read = await server.getModelDocument({ uri }).catch(() => undefined);
|
|
1185
|
+
if (this.disposed) {
|
|
1186
|
+
return;
|
|
1187
|
+
}
|
|
1188
|
+
// Forgotten meanwhile, so nothing is lost.
|
|
1189
|
+
if (!this.openUris.has(uri)) {
|
|
1190
|
+
continue;
|
|
1191
|
+
}
|
|
1192
|
+
if (read && !read.model) {
|
|
1193
|
+
const write = this.unsavedWrites.get(uri);
|
|
1194
|
+
// Named as the client last heard it, by the open's answer.
|
|
1195
|
+
const serverUri = this.opens.get(uri)?.serverUri ?? uri;
|
|
1196
|
+
// Never saved, so the drop released it and no one deleted it.
|
|
1197
|
+
const created = this.createdUris.has(uri);
|
|
1198
|
+
const unsaved = this.forgetGoneDocument(uri);
|
|
1199
|
+
// Kept for `reapply`, where it blocks its set as a document
|
|
1200
|
+
// that could not be re-opened does, and goes.
|
|
1201
|
+
if (write) {
|
|
1202
|
+
this.unsavedWrites.set(uri, write);
|
|
1203
|
+
}
|
|
1204
|
+
if (!this.host.restoreDeletion) {
|
|
1205
|
+
// No client hears of the deletion, so the loss is reported.
|
|
1206
|
+
if (unsaved) {
|
|
1207
|
+
lost.push(uri);
|
|
1208
|
+
}
|
|
1209
|
+
continue;
|
|
1210
|
+
}
|
|
1211
|
+
// The client hears of the deletion, so unsaved text is logged,
|
|
1212
|
+
// as for one heard live; the drop's release is reported too.
|
|
1213
|
+
if (created) {
|
|
1214
|
+
lost.push(uri);
|
|
1215
|
+
}
|
|
1216
|
+
try {
|
|
1217
|
+
this.host.restoreDeletion({ uri: serverUri, sourceClientId: UNKNOWN_CLIENT_ID });
|
|
1218
|
+
} catch {
|
|
1219
|
+
// The client's listener failed, not the restore.
|
|
1220
|
+
}
|
|
1221
|
+
continue;
|
|
1222
|
+
}
|
|
1223
|
+
// Its record goes with its set's, in `reapply`, which it blocks.
|
|
1224
|
+
this.openUris.delete(uri);
|
|
1225
|
+
this.opens.delete(uri);
|
|
1226
|
+
this.lastAnswers.delete(uri);
|
|
1227
|
+
this.savedVersions.delete(uri);
|
|
1228
|
+
this.createdUris.delete(uri);
|
|
1229
|
+
this.host.reportError?.(error, resolve(DATA_SESSION_RESTORE_FAILED, { uri, detail: describeError(error) }));
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
const sets = new Map<object, [string, DataSessionUnsavedWrite<TTransfer, TServer>][]>();
|
|
1233
|
+
for (const [uri, write] of writes) {
|
|
1234
|
+
const set = setOf(write);
|
|
1235
|
+
sets.set(set, [...(sets.get(set) ?? []), [uri, write]]);
|
|
1236
|
+
}
|
|
1237
|
+
for (const members of sets.values()) {
|
|
1238
|
+
lost.push(...(await this.reapply(server, members, reopened)));
|
|
1239
|
+
if (this.disposed) {
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
for (const uri of reopened.keys()) {
|
|
1244
|
+
if (!this.openUris.has(uri)) {
|
|
1245
|
+
continue;
|
|
1246
|
+
}
|
|
1247
|
+
// A failed read, or one without text, leaves the client's dirty
|
|
1248
|
+
// state as it was; the document is restored all the same.
|
|
1249
|
+
const current = await server.getModelDocument({ uri }).catch(() => undefined);
|
|
1250
|
+
if (this.disposed) {
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1253
|
+
// Again after the read: one closed or deleted meanwhile has no
|
|
1254
|
+
// state to tell.
|
|
1255
|
+
if (current?.text !== undefined && this.openUris.has(uri)) {
|
|
1256
|
+
// A created document reads clean only once a save gave it a
|
|
1257
|
+
// baseline, so its text is no longer unsaved. A write record
|
|
1258
|
+
// stays: a reverted document reads clean too, its write unsent.
|
|
1259
|
+
if (!current.text.dirty) {
|
|
1260
|
+
this.createdUris.delete(uri);
|
|
1261
|
+
}
|
|
1262
|
+
try {
|
|
1263
|
+
this.host.restoreDirty?.({ uri: current.uri, text: current.text });
|
|
1264
|
+
} catch {
|
|
1265
|
+
// The client's listener failed, not the restore: the server
|
|
1266
|
+
// has the document open and watched, so it stays restored.
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
if (lost.length > 0) {
|
|
1271
|
+
const reported = resolve(DATA_SESSION_UNSAVED_LOST, { uris: lost.join(', ') });
|
|
1272
|
+
this.host.reportError?.(new Error(reported.text), reported);
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
/**
|
|
1277
|
+
* Whether the re-opened `document` still holds the session's unsaved
|
|
1278
|
+
* `write` (`'kept'`), holds the text the write started from, so the write
|
|
1279
|
+
* can be sent again (`'resend'`), or holds something else (`'lost'`).
|
|
1280
|
+
*
|
|
1281
|
+
* A document without `text` comes from a server that sends none, and
|
|
1282
|
+
* counts as kept only at the version the write was answered with. One
|
|
1283
|
+
* without a model has no version to send the write again on.
|
|
1284
|
+
*/
|
|
1285
|
+
protected restoreOutcome(
|
|
1286
|
+
write: DataSessionUnsavedWrite<TTransfer, TServer>,
|
|
1287
|
+
document: DataSessionDocument<TTransfer, TServer>
|
|
1288
|
+
): 'kept' | 'resend' | 'lost' {
|
|
1289
|
+
if (document.text === undefined) {
|
|
1290
|
+
return document.model?.version === write.answer.version ? 'kept' : 'lost';
|
|
1291
|
+
}
|
|
1292
|
+
if (document.text.hash === write.answer.hash) {
|
|
1293
|
+
return 'kept';
|
|
1294
|
+
}
|
|
1295
|
+
return document.model && write.baseHash !== undefined && document.text.hash === write.baseHash ? 'resend' : 'lost';
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
/**
|
|
1299
|
+
* Write again, in one call and based on their `reopened` versions, the
|
|
1300
|
+
* `members` of one write call that the re-open lost, all or none. Returns
|
|
1301
|
+
* the URIs whose unsaved text is gone, and drops their records, and the
|
|
1302
|
+
* record of a member that could not be re-opened.
|
|
1303
|
+
*
|
|
1304
|
+
* A member whose record is no longer the one the restore started with was
|
|
1305
|
+
* closed, saved or disposed meanwhile, and is left out: writing it would
|
|
1306
|
+
* put back text its caller discarded. A set in {@link brokenSets} is
|
|
1307
|
+
* not written again.
|
|
1308
|
+
*/
|
|
1309
|
+
protected async reapply(
|
|
1310
|
+
server: RpcProxy<TServer>,
|
|
1311
|
+
members: readonly [string, DataSessionUnsavedWrite<TTransfer, TServer>][],
|
|
1312
|
+
reopened: ReadonlyMap<string, DataSessionDocument<TTransfer, TServer>>
|
|
1313
|
+
): Promise<string[]> {
|
|
1314
|
+
const current = (uri: string, write: DataSessionUnsavedWrite<TTransfer, TServer>): boolean =>
|
|
1315
|
+
!this.disposed && this.unsavedWrites.get(uri) === write;
|
|
1316
|
+
const outcomes = members
|
|
1317
|
+
.filter(([uri, write]) => current(uri, write))
|
|
1318
|
+
.map(([uri, write]) => {
|
|
1319
|
+
const document = reopened.get(uri);
|
|
1320
|
+
return { uri, write, document, outcome: document ? this.restoreOutcome(write, document) : 'failed' };
|
|
1321
|
+
});
|
|
1322
|
+
// A restarted server numbers afresh: the answers to later writes follow
|
|
1323
|
+
// on from the re-opened version, not from the one the record holds.
|
|
1324
|
+
for (const { uri, write, document, outcome } of outcomes) {
|
|
1325
|
+
if (outcome === 'kept' && document) {
|
|
1326
|
+
this.unsavedWrites.set(uri, { ...write, answer: this.answerOf(uri, document) });
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
// A document that could not be re-opened is forgotten, and was reported
|
|
1330
|
+
// as such; one closed, saved or deleted while a write ran lost nothing.
|
|
1331
|
+
const lose = (): string[] => {
|
|
1332
|
+
const gone = outcomes.filter(member => member.outcome !== 'kept' && current(member.uri, member.write));
|
|
1333
|
+
gone.forEach(member => {
|
|
1334
|
+
this.unsavedWrites.delete(member.uri);
|
|
1335
|
+
this.createdUris.delete(member.uri);
|
|
1336
|
+
});
|
|
1337
|
+
return gone.filter(member => member.outcome === 'lost' || member.outcome === 'resend').map(member => member.uri);
|
|
1338
|
+
};
|
|
1339
|
+
if (this.brokenSets.has(setOf(members[0][1])) || outcomes.some(member => member.outcome === 'lost' || member.outcome === 'failed')) {
|
|
1340
|
+
return lose();
|
|
1341
|
+
}
|
|
1342
|
+
// In the order of the call's updates, which is the order of its answers.
|
|
1343
|
+
const resend = outcomes
|
|
1344
|
+
.flatMap(({ uri, write, document, outcome }) =>
|
|
1345
|
+
outcome === 'resend' && document?.model ? [{ uri, write, baseVersion: document.model.version }] : []
|
|
1346
|
+
)
|
|
1347
|
+
.sort((left, right) => indexOf(left.write) - indexOf(right.write));
|
|
1348
|
+
if (resend.length === 0) {
|
|
1349
|
+
return [];
|
|
1350
|
+
}
|
|
1351
|
+
const opens = resend.map(member => this.opens.get(member.uri));
|
|
1352
|
+
let documents: DataSessionDocument<TTransfer, TServer>[];
|
|
202
1353
|
try {
|
|
203
|
-
|
|
204
|
-
|
|
1354
|
+
const { call } = resend[0].write;
|
|
1355
|
+
if ('update' in call) {
|
|
1356
|
+
documents = [
|
|
1357
|
+
await this.writesInFlight.track(
|
|
1358
|
+
server.updateModelDocument({ ...call.update, baseVersion: resend[0].baseVersion, clientId: this.clientId })
|
|
1359
|
+
)
|
|
1360
|
+
];
|
|
1361
|
+
} else {
|
|
1362
|
+
const baseVersion = new Map(resend.map(member => [indexOf(member.write), member.baseVersion]));
|
|
1363
|
+
documents = await this.writesInFlight.track(
|
|
1364
|
+
server.updateModelDocuments({
|
|
1365
|
+
...call.updates,
|
|
1366
|
+
clientId: this.clientId,
|
|
1367
|
+
updates: call.updates.updates.flatMap((update, index) => {
|
|
1368
|
+
const version = baseVersion.get(index);
|
|
1369
|
+
return version === undefined ? [] : [{ ...update, baseVersion: version }];
|
|
1370
|
+
})
|
|
1371
|
+
})
|
|
1372
|
+
);
|
|
1373
|
+
}
|
|
205
1374
|
} catch (error: unknown) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
1375
|
+
if (isConflictError(error)) {
|
|
1376
|
+
return lose();
|
|
1377
|
+
}
|
|
1378
|
+
for (const { uri, write, baseVersion } of resend) {
|
|
1379
|
+
// Closed, saved or deleted while the write ran: nothing to restore.
|
|
1380
|
+
if (!current(uri, write)) {
|
|
1381
|
+
continue;
|
|
1382
|
+
}
|
|
1383
|
+
this.host.reportError?.(error, resolve(DATA_SESSION_RESTORE_FAILED, { uri, detail: describeError(error) }));
|
|
1384
|
+
// Kept for the next restore, and numbered from the re-opened
|
|
1385
|
+
// version, as the kept records are: a record numbered by a server
|
|
1386
|
+
// that has since restarted would have later answers ignored.
|
|
1387
|
+
this.unsavedWrites.set(uri, { ...write, answer: { ...write.answer, version: baseVersion } });
|
|
210
1388
|
}
|
|
211
|
-
|
|
212
|
-
throw error;
|
|
1389
|
+
return [];
|
|
213
1390
|
}
|
|
1391
|
+
resend.forEach((member, index) => {
|
|
1392
|
+
if (current(member.uri, member.write)) {
|
|
1393
|
+
// Recorded afresh, since the answer of a restarted server may be
|
|
1394
|
+
// numbered below the one it replaces.
|
|
1395
|
+
this.unsavedWrites.delete(member.uri);
|
|
1396
|
+
this.recordWrite(member.uri, opens[index], documents[index], member.write.baseHash, member.write.call);
|
|
1397
|
+
}
|
|
1398
|
+
});
|
|
1399
|
+
return [];
|
|
214
1400
|
}
|
|
215
1401
|
|
|
216
1402
|
/**
|
|
217
|
-
*
|
|
218
|
-
*
|
|
1403
|
+
* The text hash of the version `baseVersion` names for `uri`, for a write that
|
|
1404
|
+
* starts the URI's record in {@link unsavedWrites}; `undefined` for any
|
|
1405
|
+
* other write, whose record keeps the base it has.
|
|
1406
|
+
*
|
|
1407
|
+
* Taken from {@link lastAnswers} when that is at the version, otherwise
|
|
1408
|
+
* read, and kept only when the read answers that version: a server's text
|
|
1409
|
+
* changes only with its version, so the text at a version is the text the
|
|
1410
|
+
* write, once it passes the gate, was applied to.
|
|
219
1411
|
*/
|
|
220
|
-
protected
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
1412
|
+
protected async baseHashOf(server: RpcProxy<TServer>, uri: string, baseVersion: BaseVersion): Promise<string | undefined> {
|
|
1413
|
+
if (this.unsavedWrites.has(uri) || !isModelVersion(baseVersion)) {
|
|
1414
|
+
return undefined;
|
|
1415
|
+
}
|
|
1416
|
+
const known = this.lastAnswers.get(uri);
|
|
1417
|
+
if (known?.version === baseVersion) {
|
|
1418
|
+
return known.hash;
|
|
1419
|
+
}
|
|
1420
|
+
const read = await server.getModelDocument({ uri }).catch(() => undefined);
|
|
1421
|
+
return read?.text?.version === baseVersion ? read.text.hash : undefined;
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
/**
|
|
1425
|
+
* Record `document` as the answer to a write of `uri` sent under `open`,
|
|
1426
|
+
* keeping the base of the URI's record if it has one. An answer sent under
|
|
1427
|
+
* no open, or under one that has ended, is ignored. So is one numbered below
|
|
1428
|
+
* the record's: two writes of `uri` in flight at once may answer out of
|
|
1429
|
+
* order, and the record holds the one applied last.
|
|
1430
|
+
*
|
|
1431
|
+
* So is one numbered at or below the URI's entry in {@link savedVersions}.
|
|
1432
|
+
* The save came after that write was applied, so the save persisted its
|
|
1433
|
+
* text or a later write replaced it, and its answer carries the text as it
|
|
1434
|
+
* was when sent, which a restore would report as lost.
|
|
1435
|
+
*/
|
|
1436
|
+
protected recordWrite(
|
|
1437
|
+
uri: string,
|
|
1438
|
+
open: DataSessionOpen<TTransfer, TServer> | undefined,
|
|
1439
|
+
document: DataSessionDocument<TTransfer, TServer>,
|
|
1440
|
+
baseHash: string | undefined,
|
|
1441
|
+
call: DataSessionUnsavedWrite<TTransfer, TServer>['call']
|
|
1442
|
+
): void {
|
|
1443
|
+
// Recorded, it would outlive that open.
|
|
1444
|
+
if (!open || this.opens.get(uri) !== open) {
|
|
1445
|
+
return;
|
|
1446
|
+
}
|
|
1447
|
+
const answer = this.answerOf(uri, document);
|
|
1448
|
+
const kept = this.unsavedWrites.get(uri);
|
|
1449
|
+
if (kept && answer.version < kept.answer.version) {
|
|
1450
|
+
return;
|
|
1451
|
+
}
|
|
1452
|
+
const saved = this.savedVersions.get(uri);
|
|
1453
|
+
if (saved !== undefined && answer.version <= saved) {
|
|
1454
|
+
return;
|
|
1455
|
+
}
|
|
1456
|
+
this.unsavedWrites.set(uri, { baseHash: kept ? kept.baseHash : baseHash, answer, call });
|
|
1457
|
+
this.recordText(uri, document);
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
/**
|
|
1461
|
+
* What {@link DataSessionUnsavedWrite.answer} keeps of `document`. Every write
|
|
1462
|
+
* and open answers with a model; one without is reported and counted older
|
|
1463
|
+
* than every version, so it never replaces a real answer.
|
|
1464
|
+
*/
|
|
1465
|
+
protected answerOf(
|
|
1466
|
+
uri: string,
|
|
1467
|
+
document: DataSessionDocument<TTransfer, TServer>
|
|
1468
|
+
): DataSessionUnsavedWrite<TTransfer, TServer>['answer'] {
|
|
1469
|
+
if (!document.model) {
|
|
1470
|
+
const reported = resolve(DATA_SESSION_ANSWER_WITHOUT_MODEL, { uri });
|
|
1471
|
+
this.host.reportError?.(new Error(reported.text), reported);
|
|
1472
|
+
return { version: UNRECORDED_VERSION, hash: document.text?.hash };
|
|
1473
|
+
}
|
|
1474
|
+
return { version: document.model.version, hash: document.text?.hash };
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
/** Keep `document`'s text in {@link lastAnswers}, or forget it for a server that sends none. */
|
|
1478
|
+
protected recordText(uri: string, document: DataSessionDocument<TTransfer, TServer>): void {
|
|
1479
|
+
if (document.text) {
|
|
1480
|
+
this.lastAnswers.set(uri, document.text);
|
|
1481
|
+
} else {
|
|
1482
|
+
this.lastAnswers.delete(uri);
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
/** Run `call`, counted as in flight on each of `uris` until it settles. */
|
|
1487
|
+
protected track<T>(uris: readonly string[], call: () => Promise<T>): Promise<T> {
|
|
1488
|
+
const running = call();
|
|
1489
|
+
for (const uri of uris) {
|
|
1490
|
+
let tracker = this.inFlight.get(uri);
|
|
1491
|
+
if (tracker === undefined) {
|
|
1492
|
+
tracker = new PendingWorkTracker();
|
|
1493
|
+
this.inFlight.set(uri, tracker);
|
|
1494
|
+
}
|
|
1495
|
+
tracker.track(running);
|
|
1496
|
+
}
|
|
1497
|
+
return running;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
/** {@link track} an update of `uris`, counted in {@link writesInFlight} too. */
|
|
1501
|
+
protected trackWrite<T>(uris: readonly string[], call: () => Promise<T>): Promise<T> {
|
|
1502
|
+
return this.writesInFlight.track(this.track(uris, call));
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
/**
|
|
1506
|
+
* {@link track} an open of `uri`, kept in {@link openings} until it settles,
|
|
1507
|
+
* and sent once the closes and deletes of `uri` called before it have
|
|
1508
|
+
* answered.
|
|
1509
|
+
*/
|
|
1510
|
+
protected trackOpen<T>(
|
|
1511
|
+
uri: string,
|
|
1512
|
+
options: DataSessionOpenOptions<TTransfer, TServer>,
|
|
1513
|
+
call: (opening: DataSessionOpening<TTransfer, TServer>) => Promise<T>
|
|
1514
|
+
): Promise<T> {
|
|
1515
|
+
const opening: DataSessionOpening<TTransfer, TServer> = { closed: false, options };
|
|
1516
|
+
const openings = this.openings.get(uri) ?? new Set<DataSessionOpening<TTransfer, TServer>>();
|
|
1517
|
+
this.openings.set(uri, openings.add(opening));
|
|
1518
|
+
const done = (): void => {
|
|
1519
|
+
openings.delete(opening);
|
|
1520
|
+
if (openings.size === 0 && this.openings.get(uri) === openings) {
|
|
1521
|
+
this.openings.delete(uri);
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
const ending = this.endings.get(uri);
|
|
1525
|
+
const running = this.track([uri], () => (ending ? ending.then(() => call(opening)) : call(opening)));
|
|
1526
|
+
running.then(done, done);
|
|
1527
|
+
return running;
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
/**
|
|
1531
|
+
* Run `end`, a close or delete of `uri`, kept in {@link endings} until it
|
|
1532
|
+
* and every earlier one of `uri` have settled.
|
|
1533
|
+
*/
|
|
1534
|
+
protected trackEnding(uri: string, end: () => Promise<void>): Promise<void> {
|
|
1535
|
+
const ending = end();
|
|
1536
|
+
const settled = Promise.allSettled([this.endings.get(uri), ending]);
|
|
1537
|
+
this.endings.set(uri, settled);
|
|
1538
|
+
void settled.then(() => {
|
|
1539
|
+
if (this.endings.get(uri) === settled) {
|
|
1540
|
+
this.endings.delete(uri);
|
|
1541
|
+
}
|
|
1542
|
+
});
|
|
1543
|
+
return ending;
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
/** Wait until the work `trackers` hold now has settled, or {@link settleBeforeCloseMs} has passed. */
|
|
1547
|
+
protected async settle(trackers: Iterable<PendingWorkTracker | undefined>): Promise<void> {
|
|
1548
|
+
const pending = [...trackers].filter((tracker): tracker is PendingWorkTracker => tracker !== undefined && !tracker.isIdle);
|
|
1549
|
+
if (pending.length === 0) {
|
|
224
1550
|
return;
|
|
225
1551
|
}
|
|
226
|
-
|
|
1552
|
+
await this.clock.raceTimer(Promise.all(pending.map(tracker => tracker.whenSettled())), this.settleBeforeCloseMs);
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
protected assertLive(): void {
|
|
1556
|
+
if (this.disposed) {
|
|
1557
|
+
throw new SessionClosedError(this.clientId);
|
|
1558
|
+
}
|
|
227
1559
|
}
|
|
228
1560
|
}
|