@hydranium/protocol 1.0.0-next.148 → 1.0.0-next.155
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/lib/client/data-connection.d.ts +10 -3
- package/lib/client/data-connection.d.ts.map +1 -1
- package/lib/client/data-connection.js +30 -5
- package/lib/client/data-connection.js.map +1 -1
- package/lib/client/data-session.d.ts +170 -29
- package/lib/client/data-session.d.ts.map +1 -1
- package/lib/client/data-session.js +322 -49
- package/lib/client/data-session.js.map +1 -1
- package/lib/transfer-document.d.ts +12 -0
- package/lib/transfer-document.d.ts.map +1 -1
- package/lib/transfer-document.js.map +1 -1
- package/lib/util.d.ts +6 -9
- package/lib/util.d.ts.map +1 -1
- package/lib/util.js +13 -5
- package/lib/util.js.map +1 -1
- package/package.json +1 -1
- package/src/client/data-connection.ts +32 -6
- package/src/client/data-session.ts +386 -52
- package/src/transfer-document.ts +12 -0
- package/src/util.ts +14 -5
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
import { type Disposable, Emitter, type Event } from 'vscode-jsonrpc';
|
|
11
11
|
import { type Clock, SystemClock } from '../clock';
|
|
12
|
-
import type { DataServerProtocol, DiagnosticOf, TransferDocumentDirtyChangedEvent } from '../data';
|
|
13
|
-
import { SessionClosedError } from '../errors';
|
|
14
|
-
import type
|
|
12
|
+
import type { DataServerProtocol, DiagnosticOf, TransferDocumentDirtyChangedEvent, TransferDocumentUpdatedEvent } from '../data';
|
|
13
|
+
import { isConflictError, SessionClosedError } from '../errors';
|
|
14
|
+
import { type BasedOn, isSnapshotVersion, type SnapshotVersion } from '../model-service/based-on';
|
|
15
15
|
import { type ResolvedMessage, defineMessage, describeError, resolve } from '../messages/primitives';
|
|
16
16
|
import type { OpenModelArgs } from '../model-server';
|
|
17
17
|
import type { MaybePromise } from '../util';
|
|
@@ -21,7 +21,8 @@ import type { TransferElement } from '../transfer-element';
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* A session could not re-open a document it had open after its connection
|
|
24
|
-
* dropped
|
|
24
|
+
* dropped, and forgot it; or could not write it again for another reason than
|
|
25
|
+
* a conflict, and keeps its unsaved edits for the next restore.
|
|
25
26
|
*/
|
|
26
27
|
export const DATA_SESSION_RESTORE_FAILED = defineMessage(
|
|
27
28
|
'hydranium/protocol/data-session-restore-failed',
|
|
@@ -29,11 +30,12 @@ export const DATA_SESSION_RESTORE_FAILED = defineMessage(
|
|
|
29
30
|
);
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
|
-
* A session re-opened documents after its connection dropped
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
33
|
+
* A session re-opened documents after its connection dropped and cannot put
|
|
34
|
+
* back what it wrote to them since their last save: another client changed
|
|
35
|
+
* them, even while the session was still connected; the session could not
|
|
36
|
+
* tell the text its writes started from, or lost another document written with
|
|
37
|
+
* them; or the write it sent again conflicted. The session's unsaved edits may
|
|
38
|
+
* be gone, and it no longer holds them.
|
|
37
39
|
*/
|
|
38
40
|
export const DATA_SESSION_UNSAVED_LOST = defineMessage(
|
|
39
41
|
'hydranium/protocol/data-session-unsaved-lost',
|
|
@@ -98,6 +100,39 @@ export type DataSessionDocument<
|
|
|
98
100
|
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
99
101
|
> = TransferDocument<TTransfer, DiagnosticOf<TServer>>;
|
|
100
102
|
|
|
103
|
+
/**
|
|
104
|
+
* What a {@link DataSession} keeps of a URI it wrote since the URI's last save,
|
|
105
|
+
* to write it again after a reconnect that lost it.
|
|
106
|
+
*/
|
|
107
|
+
export interface DataSessionUnsavedWrite<
|
|
108
|
+
TTransfer extends TransferElement,
|
|
109
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
110
|
+
> {
|
|
111
|
+
/**
|
|
112
|
+
* The {@link TransferDocument.textHash} of the text the first of these
|
|
113
|
+
* writes was based on. `undefined` when the session could not tell, for a
|
|
114
|
+
* write based on `'anything'` or on a version it could not read back, and
|
|
115
|
+
* such a write is never sent again: the session cannot tell another
|
|
116
|
+
* client's text from the text it wrote over.
|
|
117
|
+
*/
|
|
118
|
+
readonly baseHash: string | undefined;
|
|
119
|
+
readonly answer: Pick<DataSessionDocument<TTransfer, TServer>, 'version' | 'textHash'>;
|
|
120
|
+
/**
|
|
121
|
+
* The call that made the last write, and for an `updateDocuments` call this
|
|
122
|
+
* URI's index in it. Records whose `updates` is the same object were
|
|
123
|
+
* written together and are sent again together, so a restore never applies
|
|
124
|
+
* part of a set.
|
|
125
|
+
*/
|
|
126
|
+
readonly call:
|
|
127
|
+
| { readonly update: DataSessionUpdateArgs<TTransfer, TServer> }
|
|
128
|
+
| { readonly updates: DataSessionUpdatesArgs<TTransfer, TServer>; readonly index: number };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Where `write` stands in the `updateDocuments` call it was last written by; `0` for a single update. */
|
|
132
|
+
function indexOf(write: DataSessionUnsavedWrite<TransferElement, DataServerProtocol<TransferElement>>): number {
|
|
133
|
+
return 'index' in write.call ? write.call.index : 0;
|
|
134
|
+
}
|
|
135
|
+
|
|
101
136
|
/**
|
|
102
137
|
* What a {@link DataSession} needs from the connection that minted it.
|
|
103
138
|
*
|
|
@@ -121,6 +156,19 @@ export interface DataSessionHost<TTransfer extends TransferElement, TServer exte
|
|
|
121
156
|
* has open costs at most one repeat of a state the client already has.
|
|
122
157
|
*/
|
|
123
158
|
forgetDirty?(uri: string): void;
|
|
159
|
+
/**
|
|
160
|
+
* Fires with each flip of a document's dirty state the server sends. A
|
|
161
|
+
* session drops the unsaved write it keeps of a document that turns
|
|
162
|
+
* clean; without this event it keeps it, and a restore after another
|
|
163
|
+
* client's save of the document can report the write lost.
|
|
164
|
+
*/
|
|
165
|
+
readonly onDidChangeDirty?: Event<TransferDocumentDirtyChangedEvent>;
|
|
166
|
+
/**
|
|
167
|
+
* Fires with each update event the server sends. A session drops the
|
|
168
|
+
* unsaved write it keeps of a document another client wrote over; without
|
|
169
|
+
* this event it keeps it, and a restore reports the write lost.
|
|
170
|
+
*/
|
|
171
|
+
readonly onDidUpdateDocument?: Event<TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>>;
|
|
124
172
|
}
|
|
125
173
|
|
|
126
174
|
/**
|
|
@@ -199,11 +247,21 @@ export class DataSession<
|
|
|
199
247
|
* it under the open that is still being made.
|
|
200
248
|
*/
|
|
201
249
|
protected readonly openingUris = new Map<string, number>();
|
|
250
|
+
/** Per URI written since its last save, what {@link restore} needs to write it again. */
|
|
251
|
+
protected readonly unsavedWrites = new Map<string, DataSessionUnsavedWrite<TTransfer, TServer>>();
|
|
202
252
|
/**
|
|
203
|
-
* Per URI
|
|
204
|
-
*
|
|
253
|
+
* Per URI this session has open, the version and text hash of the last
|
|
254
|
+
* document one of its own calls was answered with, which spares the read a
|
|
255
|
+
* first unsaved write otherwise makes for its base; see {@link baseHashOf}.
|
|
205
256
|
*/
|
|
206
|
-
protected readonly
|
|
257
|
+
protected readonly lastAnswers = new Map<string, Pick<DataSessionDocument<TTransfer, TServer>, 'version' | 'textHash'>>();
|
|
258
|
+
/**
|
|
259
|
+
* Per URI this session has open, the version its last save of it was
|
|
260
|
+
* answered with; see {@link recordWrite}. Only a save sets it: an open
|
|
261
|
+
* answers at the version of a write still in flight as well, and that
|
|
262
|
+
* write is not saved.
|
|
263
|
+
*/
|
|
264
|
+
protected readonly savedVersions = new Map<string, SnapshotVersion>();
|
|
207
265
|
/** Per URI, this session's calls still in flight on it, which a close waits for. */
|
|
208
266
|
protected readonly inFlight = new Map<string, Set<Promise<unknown>>>();
|
|
209
267
|
/** This session's saves still in flight, which a host's exit waits for; see {@link hasSavesInFlight}. */
|
|
@@ -238,12 +296,19 @@ export class DataSession<
|
|
|
238
296
|
* over a connection that is going away.
|
|
239
297
|
*/
|
|
240
298
|
protected detached = false;
|
|
299
|
+
/** The subscription to the host's `onDidChangeDirty`, disposed with the session. */
|
|
300
|
+
protected readonly dirtySubscription?: Disposable;
|
|
301
|
+
/** The subscription to the host's `onDidUpdateDocument`, disposed with the session. */
|
|
302
|
+
protected readonly updateSubscription?: Disposable;
|
|
241
303
|
|
|
242
304
|
constructor(
|
|
243
305
|
readonly clientId: string,
|
|
244
306
|
protected readonly host: DataSessionHost<TTransfer, TServer>,
|
|
245
307
|
readonly label: string
|
|
246
|
-
) {
|
|
308
|
+
) {
|
|
309
|
+
this.dirtySubscription = host.onDidChangeDirty?.(event => this.forgetSavedWrite(event));
|
|
310
|
+
this.updateSubscription = host.onDidUpdateDocument?.(event => this.forgetSupersededWrite(event));
|
|
311
|
+
}
|
|
247
312
|
|
|
248
313
|
/**
|
|
249
314
|
* The connected, READY server proxy once this session is registered on it,
|
|
@@ -323,6 +388,7 @@ export class DataSession<
|
|
|
323
388
|
}
|
|
324
389
|
this.openUris.add(uri);
|
|
325
390
|
this.serverUris.set(uri, document.uri);
|
|
391
|
+
this.lastAnswers.set(uri, { version: document.version, textHash: document.textHash });
|
|
326
392
|
// Keyed as the server keys its notifications, which may not be how the
|
|
327
393
|
// caller spelled the URI.
|
|
328
394
|
this.host.forgetDirty?.(document.uri);
|
|
@@ -344,6 +410,8 @@ export class DataSession<
|
|
|
344
410
|
this.openUris.delete(args.uri);
|
|
345
411
|
this.serverUris.delete(args.uri);
|
|
346
412
|
this.unsavedWrites.delete(args.uri);
|
|
413
|
+
this.lastAnswers.delete(args.uri);
|
|
414
|
+
this.savedVersions.delete(args.uri);
|
|
347
415
|
this.host.forgetDirty?.(serverUri);
|
|
348
416
|
const server = await this.connected();
|
|
349
417
|
await server.closeModelDocument({ ...args, clientId: this.clientId });
|
|
@@ -374,8 +442,10 @@ export class DataSession<
|
|
|
374
442
|
updateDocument(args: DataSessionUpdateArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
|
|
375
443
|
return this.track([args.uri], async () => {
|
|
376
444
|
const server = await this.connected();
|
|
445
|
+
const update = { ...args };
|
|
446
|
+
const baseHash = await this.baseHashOf(server, args.uri, args.basedOn);
|
|
377
447
|
const document = await server.updateModelDocument({ ...args, clientId: this.clientId });
|
|
378
|
-
this.
|
|
448
|
+
this.recordWrite(args.uri, document, baseHash, { update });
|
|
379
449
|
return document;
|
|
380
450
|
});
|
|
381
451
|
}
|
|
@@ -389,9 +459,13 @@ export class DataSession<
|
|
|
389
459
|
return this.track(
|
|
390
460
|
args.updates.map(update => update.uri),
|
|
391
461
|
async () => {
|
|
462
|
+
// One copy for the whole call: the copy's identity is what ties its
|
|
463
|
+
// records together.
|
|
464
|
+
const updates = { ...args, updates: args.updates.map(update => ({ ...update })) };
|
|
392
465
|
const server = await this.connected();
|
|
466
|
+
const baseHashes = await Promise.all(args.updates.map(update => this.baseHashOf(server, update.uri, update.basedOn)));
|
|
393
467
|
const documents = await server.updateModelDocuments({ ...args, clientId: this.clientId });
|
|
394
|
-
args.updates.forEach((update,
|
|
468
|
+
args.updates.forEach((update, index) => this.recordWrite(update.uri, documents[index], baseHashes[index], { updates, index }));
|
|
395
469
|
return documents;
|
|
396
470
|
}
|
|
397
471
|
);
|
|
@@ -401,8 +475,19 @@ export class DataSession<
|
|
|
401
475
|
saveDocument(args: DataSessionSaveArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
|
|
402
476
|
const saving = this.track([args.uri], async () => {
|
|
403
477
|
const server = await this.connected();
|
|
478
|
+
const unsaved = this.unsavedWrites.get(args.uri);
|
|
404
479
|
const document = await server.saveModelDocument({ ...args, clientId: this.clientId });
|
|
405
|
-
|
|
480
|
+
// A write that answered while the save ran made a record the save
|
|
481
|
+
// may not have persisted, which a restore still needs.
|
|
482
|
+
if (this.unsavedWrites.get(args.uri) === unsaved) {
|
|
483
|
+
this.unsavedWrites.delete(args.uri);
|
|
484
|
+
}
|
|
485
|
+
this.lastAnswers.set(args.uri, { version: document.version, textHash: document.textHash });
|
|
486
|
+
// The higher of two saves' answers, which may arrive out of order.
|
|
487
|
+
const saved = this.savedVersions.get(args.uri);
|
|
488
|
+
if (saved === undefined || document.version > saved) {
|
|
489
|
+
this.savedVersions.set(args.uri, document.version);
|
|
490
|
+
}
|
|
406
491
|
return document;
|
|
407
492
|
});
|
|
408
493
|
this.savesInFlight.add(saving);
|
|
@@ -491,14 +576,22 @@ export class DataSession<
|
|
|
491
576
|
this.openUris.clear();
|
|
492
577
|
this.serverUris.clear();
|
|
493
578
|
this.unsavedWrites.clear();
|
|
579
|
+
this.lastAnswers.clear();
|
|
580
|
+
this.savedVersions.clear();
|
|
494
581
|
if (!this.disposed) {
|
|
495
582
|
this.disposed = true;
|
|
496
583
|
this.fireDispose();
|
|
497
584
|
}
|
|
498
585
|
}
|
|
499
586
|
|
|
500
|
-
/**
|
|
587
|
+
/**
|
|
588
|
+
* Dispose {@link dirtySubscription} and {@link updateSubscription}, fire
|
|
589
|
+
* {@link onDidDispose} and dispose its emitter, so a second call fires
|
|
590
|
+
* nothing.
|
|
591
|
+
*/
|
|
501
592
|
protected fireDispose(): void {
|
|
593
|
+
this.dirtySubscription?.dispose();
|
|
594
|
+
this.updateSubscription?.dispose();
|
|
502
595
|
this.disposeEmitter.fire(undefined);
|
|
503
596
|
this.disposeEmitter.dispose();
|
|
504
597
|
}
|
|
@@ -515,6 +608,56 @@ export class DataSession<
|
|
|
515
608
|
}
|
|
516
609
|
}
|
|
517
610
|
|
|
611
|
+
/**
|
|
612
|
+
* Drop the record in {@link unsavedWrites} of a document this session has
|
|
613
|
+
* open once the server says it turned clean: it holds no unsaved text
|
|
614
|
+
* then, and a record kept would have a restore report the write lost once
|
|
615
|
+
* another client's save changed the file.
|
|
616
|
+
*
|
|
617
|
+
* The server sends a flip only for a document someone watches, so a
|
|
618
|
+
* document the session writes without watching it keeps its record, and
|
|
619
|
+
* can still be reported lost that way.
|
|
620
|
+
*/
|
|
621
|
+
protected forgetSavedWrite(event: TransferDocumentDirtyChangedEvent): void {
|
|
622
|
+
if (event.dirty) {
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
// The flip names the server's key, which is not always the caller's
|
|
626
|
+
// spelling that the records are kept under.
|
|
627
|
+
for (const [uri, serverUri] of this.serverUris) {
|
|
628
|
+
if (serverUri === event.uri) {
|
|
629
|
+
this.unsavedWrites.delete(uri);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Drop the record in {@link unsavedWrites} of a document another client
|
|
636
|
+
* wrote over while the connection held: its write replaced the session's,
|
|
637
|
+
* so a restore has nothing of the session's to put back, and a record kept
|
|
638
|
+
* would have it report the write lost to the reconnect. A write superseded
|
|
639
|
+
* before its answer needs nothing: the answer names the other client's
|
|
640
|
+
* text, which the record then holds.
|
|
641
|
+
*
|
|
642
|
+
* Only a `'changed'` event from another client counts, and only where the
|
|
643
|
+
* document no longer holds the write: the session's own echo, and an
|
|
644
|
+
* integrity repair already in the write's answer, replaced nothing, and a
|
|
645
|
+
* `'rebuilt'` event carries no new text. Like {@link forgetSavedWrite}, it
|
|
646
|
+
* depends on the server sending the event, which it does only for a
|
|
647
|
+
* document someone watches.
|
|
648
|
+
*/
|
|
649
|
+
protected forgetSupersededWrite(event: TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>): void {
|
|
650
|
+
if (event.reason !== 'changed' || this.isOwnEcho(event.sourceClientId)) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
for (const [uri, serverUri] of this.serverUris) {
|
|
654
|
+
const write = this.unsavedWrites.get(uri);
|
|
655
|
+
if (serverUri === event.document.uri && write && this.restoreOutcome(write, event.document) !== 'kept') {
|
|
656
|
+
this.unsavedWrites.delete(uri);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
518
661
|
/**
|
|
519
662
|
* Register on `server`, and after a reconnect restore what the ended session
|
|
520
663
|
* had. Calls issued meanwhile wait for this.
|
|
@@ -527,67 +670,258 @@ export class DataSession<
|
|
|
527
670
|
}
|
|
528
671
|
|
|
529
672
|
/**
|
|
530
|
-
* Re-open and re-watch every document the session had open,
|
|
531
|
-
*
|
|
673
|
+
* Re-open and re-watch every document the session had open, write again
|
|
674
|
+
* what it wrote since their last save where the re-open lost it, and tell
|
|
675
|
+
* the host which of them lost it for good.
|
|
532
676
|
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
*
|
|
539
|
-
* Re-sending based on the write's own version then always conflicts, and
|
|
540
|
-
* based on the re-opened version it would overwrite whatever changed, so
|
|
541
|
-
* neither is done.
|
|
677
|
+
* Decided per document by the re-opened {@link TransferDocument.textHash},
|
|
678
|
+
* in {@link restoreOutcome}. Versions cannot decide a write: a revert moves
|
|
679
|
+
* the version on and a restarted server numbers afresh, so the version the
|
|
680
|
+
* last write was answered with never matches where a write is needed, and
|
|
681
|
+
* any other version may be another client's edit, which a write would
|
|
682
|
+
* overwrite.
|
|
542
683
|
*
|
|
543
|
-
*
|
|
684
|
+
* Only what the drop cost is reported: a write another client wrote over
|
|
685
|
+
* while the connection held has no record left by then, see
|
|
686
|
+
* {@link forgetSupersededWrite}.
|
|
687
|
+
*
|
|
688
|
+
* A write is sent again based on the re-opened version, as an ordinary
|
|
689
|
+
* write, so an edit arriving in between conflicts, and a conflict is not
|
|
690
|
+
* retried. Documents last written by one {@link updateDocuments} are sent
|
|
691
|
+
* again by one, and only when every one of them may be; a document that
|
|
692
|
+
* could not be re-opened blocks its set.
|
|
693
|
+
*
|
|
694
|
+
* No caller is waiting, so the outcomes go through the host: one report
|
|
544
695
|
* naming every document whose unsaved text is gone, whose record is then
|
|
545
|
-
* dropped, and one per document that could not be re-opened
|
|
546
|
-
*
|
|
696
|
+
* dropped, and one per document that could not be re-opened or written
|
|
697
|
+
* again for another reason than a conflict. A document that could not be
|
|
698
|
+
* re-opened is forgotten; a failed write keeps its record, for the next
|
|
699
|
+
* restore to decide again.
|
|
547
700
|
*
|
|
548
701
|
* Each document's dirty state goes to the host too, read once the watch is
|
|
549
|
-
* in place: a flip while the connection was down, or
|
|
550
|
-
* and the watch, reached no one, and the re-open's own
|
|
551
|
-
* second.
|
|
702
|
+
* in place and any write is sent: a flip while the connection was down, or
|
|
703
|
+
* between the re-open and the watch, reached no one, and the re-open's own
|
|
704
|
+
* answer misses the second. Read before the write, it would tell the client
|
|
705
|
+
* a document is clean that the write is about to make dirty again.
|
|
552
706
|
*/
|
|
553
707
|
protected async restore(server: RpcProxy<TServer>): Promise<void> {
|
|
554
|
-
const
|
|
708
|
+
const writes = [...this.unsavedWrites];
|
|
709
|
+
// No write sent before the drop answers any more, and a restarted
|
|
710
|
+
// server numbers afresh, below the versions saved before it.
|
|
711
|
+
this.savedVersions.clear();
|
|
712
|
+
const reopened = new Map<string, DataSessionDocument<TTransfer, TServer>>();
|
|
555
713
|
for (const uri of [...this.openUris]) {
|
|
556
714
|
try {
|
|
557
715
|
const document = await server.openModelDocument({ uri, clientId: this.clientId });
|
|
558
716
|
await server.watchModelDocument({ uri, clientId: this.clientId });
|
|
559
717
|
if (this.openUris.has(uri)) {
|
|
560
718
|
this.serverUris.set(uri, document.uri);
|
|
719
|
+
this.lastAnswers.set(uri, { version: document.version, textHash: document.textHash });
|
|
561
720
|
}
|
|
562
|
-
|
|
563
|
-
// document is restored all the same.
|
|
564
|
-
const current = await server.getModelDocument({ uri }).catch(() => undefined);
|
|
565
|
-
if (current?.dirty !== undefined) {
|
|
566
|
-
try {
|
|
567
|
-
this.host.restoreDirty?.({ uri: current.uri, dirty: current.dirty });
|
|
568
|
-
} catch {
|
|
569
|
-
// The client's listener failed, not the restore: the server
|
|
570
|
-
// has the document open and watched, so it stays restored.
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
const written = this.unsavedWrites.get(uri);
|
|
574
|
-
if (written !== undefined && written !== document.version) {
|
|
575
|
-
lost.push(uri);
|
|
576
|
-
this.unsavedWrites.delete(uri);
|
|
577
|
-
}
|
|
721
|
+
reopened.set(uri, document);
|
|
578
722
|
} catch (error: unknown) {
|
|
723
|
+
// Its record goes with its set's, in `reapply`, which it blocks.
|
|
579
724
|
this.openUris.delete(uri);
|
|
580
725
|
this.serverUris.delete(uri);
|
|
581
|
-
this.
|
|
726
|
+
this.lastAnswers.delete(uri);
|
|
727
|
+
this.savedVersions.delete(uri);
|
|
582
728
|
this.host.reportError?.(error, resolve(DATA_SESSION_RESTORE_FAILED, { uri, detail: describeError(error) }));
|
|
583
729
|
}
|
|
584
730
|
}
|
|
731
|
+
const sets = new Map<object, [string, DataSessionUnsavedWrite<TTransfer, TServer>][]>();
|
|
732
|
+
for (const [uri, write] of writes) {
|
|
733
|
+
const call = 'update' in write.call ? write.call.update : write.call.updates;
|
|
734
|
+
sets.set(call, [...(sets.get(call) ?? []), [uri, write]]);
|
|
735
|
+
}
|
|
736
|
+
const lost: string[] = [];
|
|
737
|
+
for (const members of sets.values()) {
|
|
738
|
+
lost.push(...(await this.reapply(server, members, reopened)));
|
|
739
|
+
}
|
|
740
|
+
for (const uri of reopened.keys()) {
|
|
741
|
+
if (!this.openUris.has(uri)) {
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
// A failed read leaves the client's dirty state as it was; the
|
|
745
|
+
// document is restored all the same.
|
|
746
|
+
const current = await server.getModelDocument({ uri }).catch(() => undefined);
|
|
747
|
+
if (current?.dirty !== undefined) {
|
|
748
|
+
try {
|
|
749
|
+
this.host.restoreDirty?.({ uri: current.uri, dirty: current.dirty });
|
|
750
|
+
} catch {
|
|
751
|
+
// The client's listener failed, not the restore: the server
|
|
752
|
+
// has the document open and watched, so it stays restored.
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
585
756
|
if (lost.length > 0) {
|
|
586
757
|
const reported = resolve(DATA_SESSION_UNSAVED_LOST, { uris: lost.join(', ') });
|
|
587
758
|
this.host.reportError?.(new Error(reported.text), reported);
|
|
588
759
|
}
|
|
589
760
|
}
|
|
590
761
|
|
|
762
|
+
/**
|
|
763
|
+
* Whether the re-opened `document` still holds the session's unsaved
|
|
764
|
+
* `write` (`'kept'`), holds the text the write started from, so the write
|
|
765
|
+
* can be sent again (`'resend'`), or holds something else (`'lost'`).
|
|
766
|
+
*
|
|
767
|
+
* A document without a text hash comes from a server that sends none, and
|
|
768
|
+
* counts as kept only at the version the write was answered with.
|
|
769
|
+
*/
|
|
770
|
+
protected restoreOutcome(
|
|
771
|
+
write: DataSessionUnsavedWrite<TTransfer, TServer>,
|
|
772
|
+
document: DataSessionDocument<TTransfer, TServer>
|
|
773
|
+
): 'kept' | 'resend' | 'lost' {
|
|
774
|
+
if (document.textHash === undefined) {
|
|
775
|
+
return document.version === write.answer.version ? 'kept' : 'lost';
|
|
776
|
+
}
|
|
777
|
+
if (document.textHash === write.answer.textHash) {
|
|
778
|
+
return 'kept';
|
|
779
|
+
}
|
|
780
|
+
return write.baseHash !== undefined && document.textHash === write.baseHash ? 'resend' : 'lost';
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* Write again, in one call and based on their `reopened` versions, the
|
|
785
|
+
* `members` of one write call that the re-open lost, all or none. Returns
|
|
786
|
+
* the URIs whose unsaved text is gone, and drops their records, and the
|
|
787
|
+
* record of a member that could not be re-opened.
|
|
788
|
+
*
|
|
789
|
+
* A member whose record is no longer the one the restore started with was
|
|
790
|
+
* closed, saved or disposed meanwhile, and is left out: writing it would
|
|
791
|
+
* put back text its caller discarded.
|
|
792
|
+
*/
|
|
793
|
+
protected async reapply(
|
|
794
|
+
server: RpcProxy<TServer>,
|
|
795
|
+
members: readonly [string, DataSessionUnsavedWrite<TTransfer, TServer>][],
|
|
796
|
+
reopened: ReadonlyMap<string, DataSessionDocument<TTransfer, TServer>>
|
|
797
|
+
): Promise<string[]> {
|
|
798
|
+
const current = (uri: string, write: DataSessionUnsavedWrite<TTransfer, TServer>): boolean =>
|
|
799
|
+
!this.disposed && this.unsavedWrites.get(uri) === write;
|
|
800
|
+
const outcomes = members
|
|
801
|
+
.filter(([uri, write]) => current(uri, write))
|
|
802
|
+
.map(([uri, write]) => {
|
|
803
|
+
const document = reopened.get(uri);
|
|
804
|
+
return { uri, write, document, outcome: document ? this.restoreOutcome(write, document) : 'failed' };
|
|
805
|
+
});
|
|
806
|
+
// A restarted server numbers afresh: the answers to later writes follow
|
|
807
|
+
// on from the re-opened version, not from the one the record holds.
|
|
808
|
+
for (const { uri, write, document, outcome } of outcomes) {
|
|
809
|
+
if (outcome === 'kept' && document) {
|
|
810
|
+
this.unsavedWrites.set(uri, { ...write, answer: { version: document.version, textHash: document.textHash } });
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
// A document that could not be re-opened is forgotten, and was reported
|
|
814
|
+
// as such.
|
|
815
|
+
const lose = (): string[] => {
|
|
816
|
+
outcomes.filter(member => member.outcome !== 'kept').forEach(member => this.unsavedWrites.delete(member.uri));
|
|
817
|
+
return outcomes.filter(member => member.outcome === 'lost' || member.outcome === 'resend').map(member => member.uri);
|
|
818
|
+
};
|
|
819
|
+
if (outcomes.some(member => member.outcome === 'lost' || member.outcome === 'failed')) {
|
|
820
|
+
return lose();
|
|
821
|
+
}
|
|
822
|
+
// In the order of the call's updates, which is the order of its answers.
|
|
823
|
+
const resend = outcomes
|
|
824
|
+
.flatMap(({ uri, write, document, outcome }) =>
|
|
825
|
+
outcome === 'resend' && document ? [{ uri, write, basedOn: document.version }] : []
|
|
826
|
+
)
|
|
827
|
+
.sort((left, right) => indexOf(left.write) - indexOf(right.write));
|
|
828
|
+
if (resend.length === 0) {
|
|
829
|
+
return [];
|
|
830
|
+
}
|
|
831
|
+
let documents: DataSessionDocument<TTransfer, TServer>[];
|
|
832
|
+
try {
|
|
833
|
+
const { call } = resend[0].write;
|
|
834
|
+
if ('update' in call) {
|
|
835
|
+
documents = [await server.updateModelDocument({ ...call.update, basedOn: resend[0].basedOn, clientId: this.clientId })];
|
|
836
|
+
} else {
|
|
837
|
+
const basedOn = new Map(resend.map(member => [indexOf(member.write), member.basedOn]));
|
|
838
|
+
documents = await server.updateModelDocuments({
|
|
839
|
+
...call.updates,
|
|
840
|
+
clientId: this.clientId,
|
|
841
|
+
updates: call.updates.updates.flatMap((update, index) => {
|
|
842
|
+
const version = basedOn.get(index);
|
|
843
|
+
return version === undefined ? [] : [{ ...update, basedOn: version }];
|
|
844
|
+
})
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
} catch (error: unknown) {
|
|
848
|
+
if (isConflictError(error)) {
|
|
849
|
+
return lose();
|
|
850
|
+
}
|
|
851
|
+
for (const { uri, write, basedOn } of resend) {
|
|
852
|
+
this.host.reportError?.(error, resolve(DATA_SESSION_RESTORE_FAILED, { uri, detail: describeError(error) }));
|
|
853
|
+
// Kept for the next restore, and numbered from the re-opened
|
|
854
|
+
// version, as the kept records are: a record numbered by a server
|
|
855
|
+
// that has since restarted would have later answers ignored.
|
|
856
|
+
if (current(uri, write)) {
|
|
857
|
+
this.unsavedWrites.set(uri, { ...write, answer: { ...write.answer, version: basedOn } });
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
return [];
|
|
861
|
+
}
|
|
862
|
+
resend.forEach((member, index) => {
|
|
863
|
+
if (current(member.uri, member.write)) {
|
|
864
|
+
// Recorded afresh, since the answer of a restarted server may be
|
|
865
|
+
// numbered below the one it replaces.
|
|
866
|
+
this.unsavedWrites.delete(member.uri);
|
|
867
|
+
this.recordWrite(member.uri, documents[index], member.write.baseHash, member.write.call);
|
|
868
|
+
}
|
|
869
|
+
});
|
|
870
|
+
return [];
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* The text hash of the version `basedOn` names for `uri`, for a write that
|
|
875
|
+
* starts the URI's record in {@link unsavedWrites}; `undefined` for any
|
|
876
|
+
* other write, whose record keeps the base it has.
|
|
877
|
+
*
|
|
878
|
+
* Taken from {@link lastAnswers} when that is at the version, otherwise
|
|
879
|
+
* read, and kept only when the read answers that version: a server's text
|
|
880
|
+
* changes only with its version, so the text at a version is the text the
|
|
881
|
+
* write, once it passes the gate, was applied to.
|
|
882
|
+
*/
|
|
883
|
+
protected async baseHashOf(server: RpcProxy<TServer>, uri: string, basedOn: BasedOn): Promise<string | undefined> {
|
|
884
|
+
if (this.unsavedWrites.has(uri) || !isSnapshotVersion(basedOn)) {
|
|
885
|
+
return undefined;
|
|
886
|
+
}
|
|
887
|
+
const known = this.lastAnswers.get(uri);
|
|
888
|
+
if (known?.version === basedOn) {
|
|
889
|
+
return known.textHash;
|
|
890
|
+
}
|
|
891
|
+
const read = await server.getModelDocument({ uri }).catch(() => undefined);
|
|
892
|
+
return read?.version === basedOn ? read.textHash : undefined;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* Record `document` as the answer to a write of `uri`, keeping the base of
|
|
897
|
+
* the URI's record if it has one. An answer numbered below the record's is
|
|
898
|
+
* ignored: two writes of `uri` in flight at once may answer out of order,
|
|
899
|
+
* and the record holds the one applied last.
|
|
900
|
+
*
|
|
901
|
+
* So is one numbered at or below the URI's entry in {@link savedVersions}.
|
|
902
|
+
* The save came after that write was applied, so the save persisted its
|
|
903
|
+
* text or a later write replaced it, and its answer carries the text as it
|
|
904
|
+
* was when sent, which a restore would report as lost.
|
|
905
|
+
*/
|
|
906
|
+
protected recordWrite(
|
|
907
|
+
uri: string,
|
|
908
|
+
document: DataSessionDocument<TTransfer, TServer>,
|
|
909
|
+
baseHash: string | undefined,
|
|
910
|
+
call: DataSessionUnsavedWrite<TTransfer, TServer>['call']
|
|
911
|
+
): void {
|
|
912
|
+
const answer = { version: document.version, textHash: document.textHash };
|
|
913
|
+
const kept = this.unsavedWrites.get(uri);
|
|
914
|
+
if (kept && answer.version < kept.answer.version) {
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
const saved = this.savedVersions.get(uri);
|
|
918
|
+
if (saved !== undefined && answer.version <= saved) {
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
this.unsavedWrites.set(uri, { baseHash: kept ? kept.baseHash : baseHash, answer, call });
|
|
922
|
+
this.lastAnswers.set(uri, answer);
|
|
923
|
+
}
|
|
924
|
+
|
|
591
925
|
/** Run `call`, counted as in flight on each of `uris` until it settles. */
|
|
592
926
|
protected track<T>(uris: readonly string[], call: () => Promise<T>): Promise<T> {
|
|
593
927
|
const running = call();
|
package/src/transfer-document.ts
CHANGED
|
@@ -51,6 +51,18 @@ export interface TransferDocument<TTransfer extends TransferElement, TDiagnostic
|
|
|
51
51
|
* watching the document.
|
|
52
52
|
*/
|
|
53
53
|
dirty?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* What `textHash` makes of the text the server holds for this document
|
|
56
|
+
* when it sends it: set on every document a data server answers or sends
|
|
57
|
+
* that the server holds, and absent otherwise. Equal texts hash equal,
|
|
58
|
+
* whatever the version, so a client compares the text two snapshots hold,
|
|
59
|
+
* across a revert or a server restart that numbers versions afresh.
|
|
60
|
+
*
|
|
61
|
+
* Of the text alone. The data server's de-duplication fingerprint hashes the
|
|
62
|
+
* text or the transfer root together with the diagnostics, and changes with
|
|
63
|
+
* a diagnostic where this does not.
|
|
64
|
+
*/
|
|
65
|
+
textHash?: string;
|
|
54
66
|
}
|
|
55
67
|
|
|
56
68
|
/**
|
package/src/util.ts
CHANGED
|
@@ -249,14 +249,23 @@ export function getAt<T = unknown>(obj: unknown, path: string[]): T | undefined
|
|
|
249
249
|
return cur as T;
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
+
/**
|
|
253
|
+
* A stable, non-cryptographic digest of `text`, equal on every host for equal
|
|
254
|
+
* text: the `TransferDocument.textHash` a data server stamps, so a client can
|
|
255
|
+
* hash text of its own to compare. An array is read as its elements joined, so
|
|
256
|
+
* a large text is hashed in parts without concatenating it. It tells whether a
|
|
257
|
+
* text changed, not who could forge one.
|
|
258
|
+
*/
|
|
259
|
+
export function textHash(text: string | readonly string[]): string {
|
|
260
|
+
const { high, low } = cyrb53(typeof text === 'string' ? [text] : text);
|
|
261
|
+
return high.toString(16).padStart(8, '0') + low.toString(16).padStart(8, '0');
|
|
262
|
+
}
|
|
263
|
+
|
|
252
264
|
/**
|
|
253
265
|
* The two 32-bit words cyrb53 ends on, each unsigned, for `parts` read as one
|
|
254
|
-
* string.
|
|
255
|
-
* whether a text changed, not who can forge one, and needs no `node:*` module,
|
|
256
|
-
* so every host runs it. The parts are hashed in turn, so a caller never
|
|
257
|
-
* concatenates a large text to hash it with others.
|
|
266
|
+
* string. It needs no `node:*` module, so every host runs it.
|
|
258
267
|
*/
|
|
259
|
-
|
|
268
|
+
function cyrb53(parts: readonly string[]): { readonly high: number; readonly low: number } {
|
|
260
269
|
let h1 = 0xdeadbeef;
|
|
261
270
|
let h2 = 0x41c6ce57;
|
|
262
271
|
for (const part of parts) {
|