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