@bendyline/docblocks 1.1.2 → 2.0.0
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 +76 -11
- package/dist/chunk-2D7Y4IR3.js +1293 -0
- package/dist/chunk-2D7Y4IR3.js.map +1 -0
- package/dist/chunk-5MYAICRE.js +957 -0
- package/dist/chunk-5MYAICRE.js.map +1 -0
- package/dist/chunk-75UUF6C4.js +802 -0
- package/dist/chunk-75UUF6C4.js.map +1 -0
- package/dist/chunk-AEHCVWU5.js +25 -0
- package/dist/chunk-AEHCVWU5.js.map +1 -0
- package/dist/chunk-AYZ4HN5M.js +20 -0
- package/dist/chunk-AYZ4HN5M.js.map +1 -0
- package/dist/chunk-EGNDADXB.js +111 -0
- package/dist/chunk-EGNDADXB.js.map +1 -0
- package/dist/chunk-EZKJZUNJ.js +13 -0
- package/dist/chunk-EZKJZUNJ.js.map +1 -0
- package/dist/chunk-FNW556FN.js +75 -0
- package/dist/chunk-FNW556FN.js.map +1 -0
- package/dist/chunk-GCPBEMMH.js +563 -0
- package/dist/chunk-GCPBEMMH.js.map +1 -0
- package/dist/chunk-J32GU3F3.js +1664 -0
- package/dist/chunk-J32GU3F3.js.map +1 -0
- package/dist/chunk-PGR7LK6D.js +1062 -0
- package/dist/chunk-PGR7LK6D.js.map +1 -0
- package/dist/chunk-RQJRQF46.js +787 -0
- package/dist/chunk-RQJRQF46.js.map +1 -0
- package/dist/{chunk-ME76RUMR.js → chunk-RVQHY3XU.js} +15 -1
- package/dist/chunk-RVQHY3XU.js.map +1 -0
- package/dist/chunk-SI2LU2RP.js +224 -0
- package/dist/chunk-SI2LU2RP.js.map +1 -0
- package/dist/chunk-W5UUUII5.js +142 -0
- package/dist/chunk-W5UUUII5.js.map +1 -0
- package/dist/document/document-session.d.ts +127 -0
- package/dist/document/document-session.d.ts.map +1 -0
- package/dist/document/document-session.js +749 -0
- package/dist/document/document-session.js.map +1 -0
- package/dist/document/filesystem-target.d.ts +17 -0
- package/dist/document/filesystem-target.d.ts.map +1 -0
- package/dist/document/filesystem-target.js +70 -0
- package/dist/document/filesystem-target.js.map +1 -0
- package/dist/document/index.d.ts +6 -0
- package/dist/document/index.d.ts.map +1 -0
- package/dist/document/index.js +4 -0
- package/dist/document/index.js.map +1 -0
- package/dist/document/recovery-journal.d.ts +125 -0
- package/dist/document/recovery-journal.d.ts.map +1 -0
- package/dist/document/recovery-journal.js +385 -0
- package/dist/document/recovery-journal.js.map +1 -0
- package/dist/document/types.d.ts +89 -0
- package/dist/document/types.d.ts.map +1 -0
- package/dist/document/types.js +9 -0
- package/dist/document/types.js.map +1 -0
- package/dist/filesystem/commit-lock.d.ts +3 -0
- package/dist/filesystem/commit-lock.d.ts.map +1 -0
- package/dist/filesystem/commit-lock.js +20 -0
- package/dist/filesystem/commit-lock.js.map +1 -0
- package/dist/filesystem/dbk-workspace.d.ts +34 -0
- package/dist/filesystem/dbk-workspace.d.ts.map +1 -0
- package/dist/filesystem/dbk-workspace.js +85 -0
- package/dist/filesystem/dbk-workspace.js.map +1 -0
- package/dist/filesystem/electron-provider-v2.d.ts +36 -0
- package/dist/filesystem/electron-provider-v2.d.ts.map +1 -0
- package/dist/filesystem/electron-provider-v2.js +503 -0
- package/dist/filesystem/electron-provider-v2.js.map +1 -0
- package/dist/filesystem/electron-provider.d.ts +6 -3
- package/dist/filesystem/electron-provider.d.ts.map +1 -1
- package/dist/filesystem/electron-provider.js +18 -13
- package/dist/filesystem/electron-provider.js.map +1 -1
- package/dist/filesystem/electron.d.ts +4 -0
- package/dist/filesystem/electron.d.ts.map +1 -0
- package/dist/filesystem/electron.js +4 -0
- package/dist/filesystem/electron.js.map +1 -0
- package/dist/filesystem/filesystem-content-container.d.ts +1 -1
- package/dist/filesystem/filesystem-content-container.d.ts.map +1 -1
- package/dist/filesystem/filesystem-content-container.js +50 -13
- package/dist/filesystem/filesystem-content-container.js.map +1 -1
- package/dist/filesystem/fs-error.d.ts +46 -0
- package/dist/filesystem/fs-error.d.ts.map +1 -0
- package/dist/filesystem/fs-error.js +170 -0
- package/dist/filesystem/fs-error.js.map +1 -0
- package/dist/filesystem/index.d.ts +15 -1
- package/dist/filesystem/index.d.ts.map +1 -1
- package/dist/filesystem/index.js +13 -0
- package/dist/filesystem/index.js.map +1 -1
- package/dist/filesystem/indexeddb-content-container.d.ts +3 -1
- package/dist/filesystem/indexeddb-content-container.d.ts.map +1 -1
- package/dist/filesystem/indexeddb-content-container.js +18 -11
- package/dist/filesystem/indexeddb-content-container.js.map +1 -1
- package/dist/filesystem/indexeddb-provider-v2.d.ts +96 -0
- package/dist/filesystem/indexeddb-provider-v2.d.ts.map +1 -0
- package/dist/filesystem/indexeddb-provider-v2.js +1241 -0
- package/dist/filesystem/indexeddb-provider-v2.js.map +1 -0
- package/dist/filesystem/indexeddb-provider.d.ts +21 -14
- package/dist/filesystem/indexeddb-provider.d.ts.map +1 -1
- package/dist/filesystem/indexeddb-provider.js +111 -261
- package/dist/filesystem/indexeddb-provider.js.map +1 -1
- package/dist/filesystem/indexeddb-store.d.ts +34 -0
- package/dist/filesystem/indexeddb-store.d.ts.map +1 -0
- package/dist/filesystem/indexeddb-store.js +165 -0
- package/dist/filesystem/indexeddb-store.js.map +1 -0
- package/dist/filesystem/indexeddb.d.ts +5 -0
- package/dist/filesystem/indexeddb.d.ts.map +1 -0
- package/dist/filesystem/indexeddb.js +5 -0
- package/dist/filesystem/indexeddb.js.map +1 -0
- package/dist/filesystem/legacy-v2-adapter.d.ts +62 -0
- package/dist/filesystem/legacy-v2-adapter.d.ts.map +1 -0
- package/dist/filesystem/legacy-v2-adapter.js +499 -0
- package/dist/filesystem/legacy-v2-adapter.js.map +1 -0
- package/dist/filesystem/memory-provider-v2.d.ts +92 -0
- package/dist/filesystem/memory-provider-v2.d.ts.map +1 -0
- package/dist/filesystem/memory-provider-v2.js +615 -0
- package/dist/filesystem/memory-provider-v2.js.map +1 -0
- package/dist/filesystem/memory-provider.d.ts +54 -0
- package/dist/filesystem/memory-provider.d.ts.map +1 -0
- package/dist/filesystem/memory-provider.js +132 -0
- package/dist/filesystem/memory-provider.js.map +1 -0
- package/dist/filesystem/memory.d.ts +4 -0
- package/dist/filesystem/memory.d.ts.map +1 -0
- package/dist/filesystem/memory.js +4 -0
- package/dist/filesystem/memory.js.map +1 -0
- package/dist/filesystem/move-entry.d.ts +9 -0
- package/dist/filesystem/move-entry.d.ts.map +1 -0
- package/dist/filesystem/move-entry.js +103 -0
- package/dist/filesystem/move-entry.js.map +1 -0
- package/dist/filesystem/move-error.d.ts +53 -0
- package/dist/filesystem/move-error.d.ts.map +1 -0
- package/dist/filesystem/move-error.js +99 -0
- package/dist/filesystem/move-error.js.map +1 -0
- package/dist/filesystem/native-provider-v2.d.ts +75 -0
- package/dist/filesystem/native-provider-v2.d.ts.map +1 -0
- package/dist/filesystem/native-provider-v2.js +849 -0
- package/dist/filesystem/native-provider-v2.js.map +1 -0
- package/dist/filesystem/native-provider.d.ts +9 -1
- package/dist/filesystem/native-provider.d.ts.map +1 -1
- package/dist/filesystem/native-provider.js +172 -37
- package/dist/filesystem/native-provider.js.map +1 -1
- package/dist/filesystem/native.d.ts +4 -0
- package/dist/filesystem/native.d.ts.map +1 -0
- package/dist/filesystem/native.js +4 -0
- package/dist/filesystem/native.js.map +1 -0
- package/dist/filesystem/types.d.ts +37 -4
- package/dist/filesystem/types.d.ts.map +1 -1
- package/dist/filesystem/types.js +11 -3
- package/dist/filesystem/types.js.map +1 -1
- package/dist/filesystem/v2.d.ts +172 -0
- package/dist/filesystem/v2.d.ts.map +1 -0
- package/dist/filesystem/v2.js +16 -0
- package/dist/filesystem/v2.js.map +1 -0
- package/dist/filesystem/workspace-path.d.ts +21 -0
- package/dist/filesystem/workspace-path.d.ts.map +1 -0
- package/dist/filesystem/workspace-path.js +80 -0
- package/dist/filesystem/workspace-path.js.map +1 -0
- package/dist/host/filesystem-v2.d.ts +45 -0
- package/dist/host/filesystem-v2.d.ts.map +1 -0
- package/dist/host/filesystem-v2.js +13 -0
- package/dist/host/filesystem-v2.js.map +1 -0
- package/dist/host/git.d.ts +200 -0
- package/dist/host/git.d.ts.map +1 -0
- package/dist/host/git.js +15 -0
- package/dist/host/git.js.map +1 -0
- package/dist/host/index.d.ts +4 -1
- package/dist/host/index.d.ts.map +1 -1
- package/dist/host/index.js +2 -0
- package/dist/host/index.js.map +1 -1
- package/dist/host/types.d.ts +114 -31
- package/dist/host/types.d.ts.map +1 -1
- package/dist/host/wire-policy.d.ts +22 -0
- package/dist/host/wire-policy.d.ts.map +1 -0
- package/dist/host/wire-policy.js +92 -0
- package/dist/host/wire-policy.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +4 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/types.d.ts +466 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +25 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/mcp/wire.d.ts +29 -0
- package/dist/mcp/wire.d.ts.map +1 -0
- package/dist/mcp/wire.js +1046 -0
- package/dist/mcp/wire.js.map +1 -0
- package/dist/mcp/zod.d.ts +5763 -0
- package/dist/mcp/zod.d.ts.map +1 -0
- package/dist/mcp/zod.js +908 -0
- package/dist/mcp/zod.js.map +1 -0
- package/dist/types-CX893hgH.d.ts +468 -0
- package/dist/types-CsX6EN2t.d.ts +301 -0
- package/dist/types-eOlt9J7n.d.ts +478 -0
- package/dist/vscode/index.d.ts +3 -0
- package/dist/vscode/index.d.ts.map +1 -0
- package/dist/vscode/index.js +3 -0
- package/dist/vscode/index.js.map +1 -0
- package/dist/vscode/messages.d.ts +204 -0
- package/dist/vscode/messages.d.ts.map +1 -0
- package/dist/vscode/messages.js +494 -0
- package/dist/vscode/messages.js.map +1 -0
- package/dist/workspace/index.d.ts +3 -2
- package/dist/workspace/index.d.ts.map +1 -1
- package/dist/workspace/index.js +2 -1
- package/dist/workspace/index.js.map +1 -1
- package/dist/workspace/reconcile-electron.d.ts +19 -0
- package/dist/workspace/reconcile-electron.d.ts.map +1 -0
- package/dist/workspace/reconcile-electron.js +42 -0
- package/dist/workspace/reconcile-electron.js.map +1 -0
- package/dist/workspace/types.d.ts +41 -1
- package/dist/workspace/types.d.ts.map +1 -1
- package/dist/workspace/types.js +0 -4
- package/dist/workspace/types.js.map +1 -1
- package/dist/workspace/workspace-manager.d.ts +17 -5
- package/dist/workspace/workspace-manager.d.ts.map +1 -1
- package/dist/workspace/workspace-manager.js +55 -11
- package/dist/workspace/workspace-manager.js.map +1 -1
- package/dist/workspace-path-CWVrcuPL.d.ts +21 -0
- package/package.json +43 -2
- package/src/document/document-session.ts +834 -0
- package/src/document/filesystem-target.ts +109 -0
- package/src/document/index.ts +41 -0
- package/src/document/recovery-journal.ts +536 -0
- package/src/document/types.ts +116 -0
- package/src/filesystem/commit-lock.ts +29 -0
- package/src/filesystem/dbk-workspace.ts +135 -0
- package/src/filesystem/electron-provider-v2.ts +630 -0
- package/src/filesystem/electron-provider.ts +25 -14
- package/src/filesystem/electron.ts +3 -0
- package/src/filesystem/filesystem-content-container.ts +53 -15
- package/src/filesystem/fs-error.ts +234 -0
- package/src/filesystem/index.ts +119 -0
- package/src/filesystem/indexeddb-content-container.ts +20 -12
- package/src/filesystem/indexeddb-provider-v2.ts +1740 -0
- package/src/filesystem/indexeddb-provider.ts +126 -300
- package/src/filesystem/indexeddb-store.ts +207 -0
- package/src/filesystem/indexeddb.ts +12 -0
- package/src/filesystem/legacy-v2-adapter.ts +779 -0
- package/src/filesystem/memory-provider-v2.ts +896 -0
- package/src/filesystem/memory-provider.ts +180 -0
- package/src/filesystem/memory.ts +15 -0
- package/src/filesystem/move-entry.ts +142 -0
- package/src/filesystem/move-error.ts +159 -0
- package/src/filesystem/native-provider-v2.ts +1165 -0
- package/src/filesystem/native-provider.ts +194 -41
- package/src/filesystem/native.ts +16 -0
- package/src/filesystem/types.ts +49 -4
- package/src/filesystem/v2.ts +231 -0
- package/src/filesystem/workspace-path.ts +92 -0
- package/src/host/filesystem-v2.ts +104 -0
- package/src/host/git.ts +246 -0
- package/src/host/index.ts +46 -0
- package/src/host/types.ts +155 -34
- package/src/host/wire-policy.ts +103 -0
- package/src/index.ts +2 -1
- package/src/mcp/index.ts +3 -0
- package/src/mcp/types.ts +594 -0
- package/src/mcp/wire.ts +1280 -0
- package/src/mcp/zod.ts +1000 -0
- package/src/vscode/index.ts +2 -0
- package/src/vscode/messages.ts +704 -0
- package/src/workspace/index.ts +8 -1
- package/src/workspace/reconcile-electron.ts +65 -0
- package/src/workspace/types.ts +39 -1
- package/src/workspace/workspace-manager.ts +71 -10
- package/dist/chunk-GUNM43XZ.js +0 -63
- package/dist/chunk-GUNM43XZ.js.map +0 -1
- package/dist/chunk-IM7LCQJ6.js +0 -850
- package/dist/chunk-IM7LCQJ6.js.map +0 -1
- package/dist/chunk-ME76RUMR.js.map +0 -1
- package/dist/index-pgBHBQ9G.d.ts +0 -213
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
import { isFileSystemMoveStateError } from '../filesystem/move-error.js';
|
|
2
|
+
const DEFAULT_AUTO_SAVE_RETRY_DELAYS_MS = [1000, 3000, 10000];
|
|
3
|
+
/**
|
|
4
|
+
* Raised by a commit target when optimistic external-change verification
|
|
5
|
+
* fails. The session converts it into durable conflict state.
|
|
6
|
+
*/
|
|
7
|
+
export class DocumentCommitConflictError extends Error {
|
|
8
|
+
constructor(message, externalContent, externalVersion = null) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'DocumentCommitConflictError';
|
|
11
|
+
this.externalContent = externalContent;
|
|
12
|
+
this.externalVersion = externalVersion;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class DocumentSessionConflictError extends Error {
|
|
16
|
+
constructor(conflict) {
|
|
17
|
+
super('The document changed outside DocBlocks while local edits were pending.');
|
|
18
|
+
this.name = 'DocumentSessionConflictError';
|
|
19
|
+
this.conflict = conflict;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A revisioned, serialized persistence boundary for one active document.
|
|
24
|
+
*
|
|
25
|
+
* Invariants:
|
|
26
|
+
* - only drain() calls target.commit();
|
|
27
|
+
* - at most one commit is in flight;
|
|
28
|
+
* - revisions never decrease, including across target transitions;
|
|
29
|
+
* - destructive operations wait for an in-flight commit and run last;
|
|
30
|
+
* - a target is never changed by a React effect cleanup.
|
|
31
|
+
*/
|
|
32
|
+
export class DocumentSession {
|
|
33
|
+
constructor(options = {}) {
|
|
34
|
+
this.listeners = new Set();
|
|
35
|
+
this.target = null;
|
|
36
|
+
this.content = '';
|
|
37
|
+
this.persistedContent = null;
|
|
38
|
+
this.revision = 0;
|
|
39
|
+
this.persistedRevision = 0;
|
|
40
|
+
this.savingRevision = null;
|
|
41
|
+
this.savingContent = null;
|
|
42
|
+
this.generation = 0;
|
|
43
|
+
this.externalSequence = -1;
|
|
44
|
+
this.lifecycle = 'open';
|
|
45
|
+
this.frozen = false;
|
|
46
|
+
this.error = null;
|
|
47
|
+
this.conflict = null;
|
|
48
|
+
this.autoSaveTimer = null;
|
|
49
|
+
this.autoSaveRetryAttempt = 0;
|
|
50
|
+
this.drainPromise = null;
|
|
51
|
+
this.haltDrain = false;
|
|
52
|
+
this.operationTail = Promise.resolve();
|
|
53
|
+
this.subscribe = (listener) => {
|
|
54
|
+
this.listeners.add(listener);
|
|
55
|
+
return () => this.listeners.delete(listener);
|
|
56
|
+
};
|
|
57
|
+
this.getSnapshot = () => this.snapshot;
|
|
58
|
+
this.autoSaveDelayMs = Math.max(0, options.autoSaveDelayMs ?? 500);
|
|
59
|
+
this.autoSaveEnabled = options.autoSaveEnabled ?? true;
|
|
60
|
+
this.autoSaveRetryDelaysMs = normalizeRetryDelays(options.autoSaveRetryDelaysMs ?? DEFAULT_AUTO_SAVE_RETRY_DELAYS_MS);
|
|
61
|
+
this.recoveryJournal = options.recoveryJournal ?? null;
|
|
62
|
+
this.snapshot = this.createSnapshot();
|
|
63
|
+
}
|
|
64
|
+
/** Enable or disable automatic persistence without weakening explicit or lifecycle flushes. */
|
|
65
|
+
setAutoSaveEnabled(enabled) {
|
|
66
|
+
if (this.autoSaveEnabled === enabled)
|
|
67
|
+
return;
|
|
68
|
+
this.autoSaveEnabled = enabled;
|
|
69
|
+
if (!enabled) {
|
|
70
|
+
this.clearAutoSaveTimer();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (this.target &&
|
|
74
|
+
this.lifecycle === 'open' &&
|
|
75
|
+
!this.frozen &&
|
|
76
|
+
!this.conflict &&
|
|
77
|
+
this.persistedRevision < this.revision) {
|
|
78
|
+
this.scheduleAutoSave();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Record a complete local snapshot and schedule its persistence.
|
|
83
|
+
* Returns the assigned monotonic revision.
|
|
84
|
+
*/
|
|
85
|
+
edit(content, scope) {
|
|
86
|
+
if (!this.target || this.lifecycle !== 'open' || this.frozen) {
|
|
87
|
+
throw new Error('Cannot edit a document session that is not open.');
|
|
88
|
+
}
|
|
89
|
+
if (scope.targetKey !== this.target.key || scope.generation !== this.generation) {
|
|
90
|
+
throw new Error('Cannot apply an edit from an obsolete document instance.');
|
|
91
|
+
}
|
|
92
|
+
if (content === this.content)
|
|
93
|
+
return this.revision;
|
|
94
|
+
this.content = content;
|
|
95
|
+
this.revision += 1;
|
|
96
|
+
this.error = null;
|
|
97
|
+
this.autoSaveRetryAttempt = 0;
|
|
98
|
+
if (this.conflict) {
|
|
99
|
+
this.conflict = {
|
|
100
|
+
...this.conflict,
|
|
101
|
+
localContent: content,
|
|
102
|
+
localRevision: this.revision,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
this.writeRecoverySnapshot();
|
|
106
|
+
if (!this.conflict)
|
|
107
|
+
this.scheduleAutoSave();
|
|
108
|
+
this.emit();
|
|
109
|
+
return this.revision;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Persist the revision current when flush() is called. A later edit may
|
|
113
|
+
* advance the session again, but commits remain strictly serialized.
|
|
114
|
+
*/
|
|
115
|
+
async flush(reason = 'manual') {
|
|
116
|
+
this.clearAutoSaveTimer();
|
|
117
|
+
if (!this.target)
|
|
118
|
+
return this.snapshot;
|
|
119
|
+
if (this.conflict)
|
|
120
|
+
throw new DocumentSessionConflictError(this.conflict);
|
|
121
|
+
const requiredRevision = this.revision;
|
|
122
|
+
while (this.target && this.persistedRevision < requiredRevision) {
|
|
123
|
+
if (this.conflict)
|
|
124
|
+
throw new DocumentSessionConflictError(this.conflict);
|
|
125
|
+
if (this.haltDrain) {
|
|
126
|
+
throw new Error('Document persistence was cancelled before the requested revision saved.');
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
await this.ensureDrain(reason);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
if (this.conflict)
|
|
133
|
+
throw new DocumentSessionConflictError(this.conflict);
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (this.conflict)
|
|
138
|
+
throw new DocumentSessionConflictError(this.conflict);
|
|
139
|
+
if (this.persistedRevision < requiredRevision) {
|
|
140
|
+
throw new Error('The document target closed before the requested revision saved.');
|
|
141
|
+
}
|
|
142
|
+
return this.snapshot;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Flush the old document, then replace it with a new clean baseline.
|
|
146
|
+
* UI state should change only after this promise resolves.
|
|
147
|
+
*/
|
|
148
|
+
transitionTo(target, content) {
|
|
149
|
+
return this.enqueueOperation(() => this.performTransition(async () => ({ target, content })).then((snapshot) => {
|
|
150
|
+
if (!snapshot)
|
|
151
|
+
throw new Error('A fixed document transition cannot be cancelled.');
|
|
152
|
+
return snapshot;
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Freeze and flush the current document before reading the next one. The
|
|
157
|
+
* loader may return null when a navigation request was superseded, leaving
|
|
158
|
+
* the current target active and clean.
|
|
159
|
+
*/
|
|
160
|
+
transitionWithLoad(load) {
|
|
161
|
+
return this.enqueueOperation(() => this.performTransition(load));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Flush the current path, run the storage move, and only then swap the
|
|
165
|
+
* commit target. No edit can enter between those steps.
|
|
166
|
+
*/
|
|
167
|
+
retarget(nextTarget, moveStorage) {
|
|
168
|
+
return this.enqueueOperation(async () => {
|
|
169
|
+
this.assertNotPreparingClose();
|
|
170
|
+
if (!this.target)
|
|
171
|
+
throw new Error('Cannot retarget a session without a document.');
|
|
172
|
+
this.lifecycle = 'transitioning';
|
|
173
|
+
this.frozen = true;
|
|
174
|
+
this.emit();
|
|
175
|
+
try {
|
|
176
|
+
this.haltDrain = false;
|
|
177
|
+
await this.flush('transition');
|
|
178
|
+
await moveStorage();
|
|
179
|
+
this.generation += 1;
|
|
180
|
+
this.externalSequence = -1;
|
|
181
|
+
this.target = nextTarget;
|
|
182
|
+
this.error = null;
|
|
183
|
+
this.conflict = null;
|
|
184
|
+
this.lifecycle = 'open';
|
|
185
|
+
this.frozen = false;
|
|
186
|
+
this.emit();
|
|
187
|
+
return this.snapshot;
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
if (isFileSystemMoveStateError(error)) {
|
|
191
|
+
if (error.documentLocation === 'destination') {
|
|
192
|
+
this.generation += 1;
|
|
193
|
+
this.externalSequence = -1;
|
|
194
|
+
this.target = nextTarget;
|
|
195
|
+
}
|
|
196
|
+
this.haltDrain = true;
|
|
197
|
+
this.error = error;
|
|
198
|
+
this.lifecycle = 'open';
|
|
199
|
+
// The provider could not establish one authoritative compound move.
|
|
200
|
+
// Keep editing frozen until navigation/recovery chooses a real path.
|
|
201
|
+
this.frozen = true;
|
|
202
|
+
this.emit();
|
|
203
|
+
throw error;
|
|
204
|
+
}
|
|
205
|
+
this.lifecycle = 'open';
|
|
206
|
+
this.frozen = false;
|
|
207
|
+
this.emit();
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Cancel queued writes, wait for an already-running write, then delete.
|
|
214
|
+
* The deletion is therefore the final storage operation for this target.
|
|
215
|
+
*/
|
|
216
|
+
delete(deleteStorage) {
|
|
217
|
+
return this.enqueueOperation(async () => {
|
|
218
|
+
this.assertNotPreparingClose();
|
|
219
|
+
this.lifecycle = 'transitioning';
|
|
220
|
+
this.frozen = true;
|
|
221
|
+
this.haltDrain = true;
|
|
222
|
+
this.clearAutoSaveTimer();
|
|
223
|
+
this.emit();
|
|
224
|
+
await this.awaitActiveDrain();
|
|
225
|
+
try {
|
|
226
|
+
await deleteStorage();
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
this.haltDrain = false;
|
|
230
|
+
this.lifecycle = 'open';
|
|
231
|
+
this.frozen = false;
|
|
232
|
+
if (this.target && this.persistedRevision < this.revision && !this.conflict) {
|
|
233
|
+
this.scheduleAutoSave();
|
|
234
|
+
}
|
|
235
|
+
this.emit();
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
238
|
+
this.discardRecoverySnapshot();
|
|
239
|
+
this.detach();
|
|
240
|
+
return this.snapshot;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Discard queued changes and detach without mutating storage. This is used
|
|
245
|
+
* for explicitly abandoned sessions, never as an implicit effect cleanup.
|
|
246
|
+
*/
|
|
247
|
+
cancel() {
|
|
248
|
+
return this.enqueueOperation(async () => {
|
|
249
|
+
this.lifecycle = 'transitioning';
|
|
250
|
+
this.frozen = true;
|
|
251
|
+
this.haltDrain = true;
|
|
252
|
+
this.clearAutoSaveTimer();
|
|
253
|
+
this.emit();
|
|
254
|
+
await this.awaitActiveDrain();
|
|
255
|
+
this.discardRecoverySnapshot();
|
|
256
|
+
this.detach();
|
|
257
|
+
return this.snapshot;
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Observe a filesystem/host change captured for targetKey. Clean sessions
|
|
262
|
+
* adopt it. Dirty or saving sessions preserve local text and enter conflict.
|
|
263
|
+
*/
|
|
264
|
+
observeExternal(change) {
|
|
265
|
+
if (!this.target || change.targetKey !== this.target.key || this.lifecycle === 'closed') {
|
|
266
|
+
return 'ignored';
|
|
267
|
+
}
|
|
268
|
+
if (change.sequence !== undefined) {
|
|
269
|
+
if (!Number.isSafeInteger(change.sequence) || change.sequence <= this.externalSequence) {
|
|
270
|
+
return 'ignored';
|
|
271
|
+
}
|
|
272
|
+
this.externalSequence = change.sequence;
|
|
273
|
+
}
|
|
274
|
+
// While conflicted, a current observation equal to the latest local
|
|
275
|
+
// snapshot proves that revision durable without requiring another write.
|
|
276
|
+
// Never acknowledge while a commit is still running; its eventual result
|
|
277
|
+
// remains authoritative for the serialized drain.
|
|
278
|
+
if (this.conflict &&
|
|
279
|
+
change.content === this.content &&
|
|
280
|
+
this.savingRevision === null &&
|
|
281
|
+
this.lifecycle === 'open') {
|
|
282
|
+
this.clearAutoSaveTimer();
|
|
283
|
+
this.autoSaveRetryAttempt = 0;
|
|
284
|
+
this.persistedRevision = this.revision;
|
|
285
|
+
this.persistedContent = this.content;
|
|
286
|
+
this.haltDrain = false;
|
|
287
|
+
this.error = null;
|
|
288
|
+
this.conflict = null;
|
|
289
|
+
this.discardRecoverySnapshot();
|
|
290
|
+
this.emit();
|
|
291
|
+
return 'applied';
|
|
292
|
+
}
|
|
293
|
+
// Once a conflict exists, every later accepted observation refreshes its
|
|
294
|
+
// external branch before ordinary duplicate suppression. In particular,
|
|
295
|
+
// A -> local C -> external B -> external A must offer A, not stale B, when
|
|
296
|
+
// the user chooses Reload external.
|
|
297
|
+
if (this.conflict) {
|
|
298
|
+
const externalVersion = change.version ?? null;
|
|
299
|
+
if (this.conflict.localContent === this.content &&
|
|
300
|
+
this.conflict.localRevision === this.revision &&
|
|
301
|
+
this.conflict.externalContent === change.content &&
|
|
302
|
+
this.conflict.externalVersion === externalVersion) {
|
|
303
|
+
return 'ignored';
|
|
304
|
+
}
|
|
305
|
+
this.clearAutoSaveTimer();
|
|
306
|
+
this.haltDrain = true;
|
|
307
|
+
this.conflict = {
|
|
308
|
+
targetKey: change.targetKey,
|
|
309
|
+
localContent: this.content,
|
|
310
|
+
localRevision: this.revision,
|
|
311
|
+
externalContent: change.content,
|
|
312
|
+
externalVersion,
|
|
313
|
+
};
|
|
314
|
+
this.error = null;
|
|
315
|
+
this.writeRecoverySnapshot();
|
|
316
|
+
this.emit();
|
|
317
|
+
return 'conflict';
|
|
318
|
+
}
|
|
319
|
+
if (change.content === this.content ||
|
|
320
|
+
change.content === this.persistedContent ||
|
|
321
|
+
(this.savingRevision !== null && change.content === this.savingContent)) {
|
|
322
|
+
return 'ignored';
|
|
323
|
+
}
|
|
324
|
+
const isClean = this.persistedRevision === this.revision &&
|
|
325
|
+
this.savingRevision === null &&
|
|
326
|
+
this.error === null &&
|
|
327
|
+
this.conflict === null;
|
|
328
|
+
if (isClean && change.content !== null) {
|
|
329
|
+
this.content = change.content;
|
|
330
|
+
this.revision += 1;
|
|
331
|
+
this.persistedRevision = this.revision;
|
|
332
|
+
this.persistedContent = change.content;
|
|
333
|
+
this.generation += 1;
|
|
334
|
+
this.discardRecoverySnapshot();
|
|
335
|
+
this.emit();
|
|
336
|
+
return 'applied';
|
|
337
|
+
}
|
|
338
|
+
this.clearAutoSaveTimer();
|
|
339
|
+
this.haltDrain = true;
|
|
340
|
+
this.conflict = {
|
|
341
|
+
targetKey: change.targetKey,
|
|
342
|
+
localContent: this.content,
|
|
343
|
+
localRevision: this.revision,
|
|
344
|
+
externalContent: change.content,
|
|
345
|
+
externalVersion: change.version ?? null,
|
|
346
|
+
};
|
|
347
|
+
this.error = null;
|
|
348
|
+
this.writeRecoverySnapshot();
|
|
349
|
+
this.emit();
|
|
350
|
+
return 'conflict';
|
|
351
|
+
}
|
|
352
|
+
resolveConflict(strategy) {
|
|
353
|
+
return this.enqueueOperation(async () => {
|
|
354
|
+
if (!this.conflict)
|
|
355
|
+
return this.snapshot;
|
|
356
|
+
this.clearAutoSaveTimer();
|
|
357
|
+
this.haltDrain = true;
|
|
358
|
+
await this.awaitActiveDrain();
|
|
359
|
+
// A watcher may refresh or converge the conflict while an in-flight
|
|
360
|
+
// commit settles. Resolve the branch that is current now, never the
|
|
361
|
+
// snapshot captured before the await.
|
|
362
|
+
const currentConflict = this.conflict;
|
|
363
|
+
if (!currentConflict)
|
|
364
|
+
return this.snapshot;
|
|
365
|
+
if (strategy === 'use-external') {
|
|
366
|
+
this.discardRecoverySnapshot();
|
|
367
|
+
this.generation += 1;
|
|
368
|
+
this.revision += 1;
|
|
369
|
+
this.conflict = null;
|
|
370
|
+
this.error = null;
|
|
371
|
+
this.haltDrain = false;
|
|
372
|
+
if (currentConflict.externalContent === null) {
|
|
373
|
+
this.target = null;
|
|
374
|
+
this.content = '';
|
|
375
|
+
this.persistedContent = null;
|
|
376
|
+
this.persistedRevision = this.revision;
|
|
377
|
+
this.lifecycle = 'closed';
|
|
378
|
+
this.frozen = true;
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
this.content = currentConflict.externalContent;
|
|
382
|
+
this.persistedContent = currentConflict.externalContent;
|
|
383
|
+
this.persistedRevision = this.revision;
|
|
384
|
+
this.lifecycle = 'open';
|
|
385
|
+
this.frozen = false;
|
|
386
|
+
}
|
|
387
|
+
this.emit();
|
|
388
|
+
return this.snapshot;
|
|
389
|
+
}
|
|
390
|
+
// Keep the local branch, but make the observed external snapshot the
|
|
391
|
+
// optimistic baseline for the next conditional commit.
|
|
392
|
+
this.persistedContent = currentConflict.externalContent;
|
|
393
|
+
// A clean external deletion/conflict has no unsaved local revision.
|
|
394
|
+
// Keeping local still requires a fresh commit that recreates/overwrites
|
|
395
|
+
// the target, so make that intent explicit with a new revision.
|
|
396
|
+
this.revision += 1;
|
|
397
|
+
this.conflict = null;
|
|
398
|
+
this.error = null;
|
|
399
|
+
this.haltDrain = false;
|
|
400
|
+
this.lifecycle = 'open';
|
|
401
|
+
this.frozen = false;
|
|
402
|
+
this.writeRecoverySnapshot();
|
|
403
|
+
this.emit();
|
|
404
|
+
return this.flush('conflict');
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Freeze edits and flush through the latest revision. A successful call
|
|
409
|
+
* remains frozen until cancelClose(); the owning host may now destroy it.
|
|
410
|
+
*/
|
|
411
|
+
prepareClose() {
|
|
412
|
+
return this.enqueueOperation(async () => {
|
|
413
|
+
if (this.lifecycle === 'closing')
|
|
414
|
+
return this.snapshot;
|
|
415
|
+
this.lifecycle = 'closing';
|
|
416
|
+
this.frozen = true;
|
|
417
|
+
this.haltDrain = false;
|
|
418
|
+
this.emit();
|
|
419
|
+
try {
|
|
420
|
+
await this.flush('close');
|
|
421
|
+
return this.snapshot;
|
|
422
|
+
}
|
|
423
|
+
catch (error) {
|
|
424
|
+
this.lifecycle = 'open';
|
|
425
|
+
this.frozen = false;
|
|
426
|
+
this.emit();
|
|
427
|
+
throw error;
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
/** Resume editing when a host cancels a prepared close. */
|
|
432
|
+
cancelClose() {
|
|
433
|
+
if (this.lifecycle !== 'closing')
|
|
434
|
+
return;
|
|
435
|
+
this.lifecycle = 'open';
|
|
436
|
+
this.frozen = false;
|
|
437
|
+
this.haltDrain = false;
|
|
438
|
+
if (this.target && this.persistedRevision < this.revision && !this.conflict) {
|
|
439
|
+
this.scheduleAutoSave();
|
|
440
|
+
}
|
|
441
|
+
this.emit();
|
|
442
|
+
}
|
|
443
|
+
async performTransition(load) {
|
|
444
|
+
this.assertNotPreparingClose();
|
|
445
|
+
this.lifecycle = 'transitioning';
|
|
446
|
+
this.frozen = true;
|
|
447
|
+
this.emit();
|
|
448
|
+
try {
|
|
449
|
+
this.haltDrain = false;
|
|
450
|
+
await this.flush('transition');
|
|
451
|
+
this.clearAutoSaveTimer();
|
|
452
|
+
const next = await load();
|
|
453
|
+
if (!next) {
|
|
454
|
+
this.lifecycle = 'open';
|
|
455
|
+
this.frozen = false;
|
|
456
|
+
this.emit();
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
this.generation += 1;
|
|
460
|
+
this.externalSequence = -1;
|
|
461
|
+
this.target = next.target;
|
|
462
|
+
this.content = next.content;
|
|
463
|
+
this.revision += 1;
|
|
464
|
+
this.persistedRevision = this.revision;
|
|
465
|
+
this.persistedContent = next.target ? next.content : null;
|
|
466
|
+
this.savingRevision = null;
|
|
467
|
+
this.savingContent = null;
|
|
468
|
+
this.error = null;
|
|
469
|
+
this.conflict = null;
|
|
470
|
+
this.haltDrain = false;
|
|
471
|
+
this.lifecycle = 'open';
|
|
472
|
+
this.frozen = false;
|
|
473
|
+
const recoveryState = this.restoreRecoverySnapshot();
|
|
474
|
+
if (recoveryState === 'dirty')
|
|
475
|
+
this.scheduleAutoSave();
|
|
476
|
+
this.emit();
|
|
477
|
+
return this.snapshot;
|
|
478
|
+
}
|
|
479
|
+
catch (error) {
|
|
480
|
+
this.lifecycle = 'open';
|
|
481
|
+
this.frozen = false;
|
|
482
|
+
this.emit();
|
|
483
|
+
throw error;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
scheduleAutoSave(delayMs = this.autoSaveDelayMs) {
|
|
487
|
+
this.clearAutoSaveTimer();
|
|
488
|
+
if (!this.autoSaveEnabled)
|
|
489
|
+
return;
|
|
490
|
+
this.autoSaveTimer = setTimeout(() => {
|
|
491
|
+
this.autoSaveTimer = null;
|
|
492
|
+
const target = this.target;
|
|
493
|
+
const generation = this.generation;
|
|
494
|
+
void this.flush('autosave').then(() => {
|
|
495
|
+
this.autoSaveRetryAttempt = 0;
|
|
496
|
+
}, () => {
|
|
497
|
+
// Preserve honest error/conflict state while making ordinary I/O
|
|
498
|
+
// failures recoverable. A newer edit already owns a fresh debounce
|
|
499
|
+
// timer, and lifecycle operations cancel this retry path.
|
|
500
|
+
if (this.autoSaveTimer !== null ||
|
|
501
|
+
!target ||
|
|
502
|
+
target !== this.target ||
|
|
503
|
+
generation !== this.generation ||
|
|
504
|
+
this.lifecycle !== 'open' ||
|
|
505
|
+
this.frozen ||
|
|
506
|
+
this.haltDrain ||
|
|
507
|
+
this.conflict ||
|
|
508
|
+
this.persistedRevision >= this.revision) {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const retryDelay = this.autoSaveRetryDelaysMs[this.autoSaveRetryAttempt];
|
|
512
|
+
if (retryDelay === undefined)
|
|
513
|
+
return;
|
|
514
|
+
this.autoSaveRetryAttempt += 1;
|
|
515
|
+
this.scheduleAutoSave(retryDelay);
|
|
516
|
+
});
|
|
517
|
+
}, delayMs);
|
|
518
|
+
}
|
|
519
|
+
clearAutoSaveTimer() {
|
|
520
|
+
if (this.autoSaveTimer === null)
|
|
521
|
+
return;
|
|
522
|
+
clearTimeout(this.autoSaveTimer);
|
|
523
|
+
this.autoSaveTimer = null;
|
|
524
|
+
}
|
|
525
|
+
ensureDrain(reason) {
|
|
526
|
+
if (this.drainPromise)
|
|
527
|
+
return this.drainPromise;
|
|
528
|
+
const running = this.drain(reason);
|
|
529
|
+
this.drainPromise = running;
|
|
530
|
+
const clear = () => {
|
|
531
|
+
if (this.drainPromise === running)
|
|
532
|
+
this.drainPromise = null;
|
|
533
|
+
};
|
|
534
|
+
void running.then(clear, clear);
|
|
535
|
+
return running;
|
|
536
|
+
}
|
|
537
|
+
async drain(reason) {
|
|
538
|
+
while (this.target &&
|
|
539
|
+
!this.haltDrain &&
|
|
540
|
+
!this.conflict &&
|
|
541
|
+
this.persistedRevision < this.revision) {
|
|
542
|
+
const target = this.target;
|
|
543
|
+
const generation = this.generation;
|
|
544
|
+
const saveRevision = this.revision;
|
|
545
|
+
const saveContent = this.content;
|
|
546
|
+
const previousPersistedRevision = this.persistedRevision;
|
|
547
|
+
const previousPersistedContent = this.persistedContent;
|
|
548
|
+
this.savingRevision = saveRevision;
|
|
549
|
+
this.savingContent = saveContent;
|
|
550
|
+
this.error = null;
|
|
551
|
+
this.emit();
|
|
552
|
+
try {
|
|
553
|
+
await target.commit({
|
|
554
|
+
targetKey: target.key,
|
|
555
|
+
content: saveContent,
|
|
556
|
+
revision: saveRevision,
|
|
557
|
+
persistedRevision: previousPersistedRevision,
|
|
558
|
+
persistedContent: previousPersistedContent,
|
|
559
|
+
reason,
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
catch (error) {
|
|
563
|
+
this.savingRevision = null;
|
|
564
|
+
this.savingContent = null;
|
|
565
|
+
if (error instanceof DocumentCommitConflictError) {
|
|
566
|
+
this.haltDrain = true;
|
|
567
|
+
this.conflict = {
|
|
568
|
+
targetKey: target.key,
|
|
569
|
+
localContent: this.content,
|
|
570
|
+
localRevision: this.revision,
|
|
571
|
+
externalContent: error.externalContent,
|
|
572
|
+
externalVersion: error.externalVersion,
|
|
573
|
+
};
|
|
574
|
+
this.error = null;
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
this.error = toError(error);
|
|
578
|
+
}
|
|
579
|
+
this.emit();
|
|
580
|
+
throw error;
|
|
581
|
+
}
|
|
582
|
+
if (generation !== this.generation || target !== this.target)
|
|
583
|
+
return;
|
|
584
|
+
this.persistedRevision = Math.max(this.persistedRevision, saveRevision);
|
|
585
|
+
this.persistedContent = saveContent;
|
|
586
|
+
this.autoSaveRetryAttempt = 0;
|
|
587
|
+
this.savingRevision = null;
|
|
588
|
+
this.savingContent = null;
|
|
589
|
+
this.error = null;
|
|
590
|
+
const activeConflict = this.getSnapshot().conflict;
|
|
591
|
+
if (activeConflict?.externalContent === saveContent && this.content === saveContent) {
|
|
592
|
+
// An observation that matched the in-flight local snapshot could not
|
|
593
|
+
// be acknowledged until the commit settled. Its successful return now
|
|
594
|
+
// proves both branches converged on the same durable revision.
|
|
595
|
+
this.conflict = null;
|
|
596
|
+
this.haltDrain = false;
|
|
597
|
+
}
|
|
598
|
+
this.recoveryJournal?.acknowledge({
|
|
599
|
+
targetKey: target.key,
|
|
600
|
+
generation,
|
|
601
|
+
persistedRevision: this.persistedRevision,
|
|
602
|
+
});
|
|
603
|
+
this.emit();
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
async awaitActiveDrain() {
|
|
607
|
+
const active = this.drainPromise;
|
|
608
|
+
if (!active)
|
|
609
|
+
return;
|
|
610
|
+
try {
|
|
611
|
+
await active;
|
|
612
|
+
}
|
|
613
|
+
catch {
|
|
614
|
+
// A destructive cancellation still waits for the operation to settle,
|
|
615
|
+
// but a failed obsolete write must not prevent the requested deletion.
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Rehydrate a synchronous crash snapshot after the durable baseline has
|
|
620
|
+
* been read. A matching baseline is safe to resume as dirty; a changed
|
|
621
|
+
* baseline becomes an explicit conflict and is never auto-overwritten.
|
|
622
|
+
*/
|
|
623
|
+
restoreRecoverySnapshot() {
|
|
624
|
+
if (!this.target || !this.recoveryJournal)
|
|
625
|
+
return 'none';
|
|
626
|
+
const recovered = this.recoveryJournal.lookup(this.target.key);
|
|
627
|
+
if (!recovered)
|
|
628
|
+
return 'none';
|
|
629
|
+
if (recovered.content === this.content) {
|
|
630
|
+
this.recoveryJournal.acknowledge({
|
|
631
|
+
targetKey: recovered.targetKey,
|
|
632
|
+
generation: recovered.generation,
|
|
633
|
+
persistedRevision: recovered.revision,
|
|
634
|
+
});
|
|
635
|
+
return 'none';
|
|
636
|
+
}
|
|
637
|
+
const externalContent = this.content;
|
|
638
|
+
this.generation = Math.max(this.generation, recovered.generation) + 1;
|
|
639
|
+
this.revision = Math.max(this.revision, recovered.revision) + 1;
|
|
640
|
+
this.content = recovered.content;
|
|
641
|
+
if (recovered.persistedContent === externalContent) {
|
|
642
|
+
this.writeRecoverySnapshot();
|
|
643
|
+
return 'dirty';
|
|
644
|
+
}
|
|
645
|
+
// Preserve the journal's prior baseline so a second crash continues to
|
|
646
|
+
// reopen as a conflict until the user explicitly chooses a branch.
|
|
647
|
+
this.persistedContent = recovered.persistedContent;
|
|
648
|
+
this.haltDrain = true;
|
|
649
|
+
this.conflict = {
|
|
650
|
+
targetKey: this.target.key,
|
|
651
|
+
localContent: recovered.content,
|
|
652
|
+
localRevision: this.revision,
|
|
653
|
+
externalContent,
|
|
654
|
+
externalVersion: null,
|
|
655
|
+
};
|
|
656
|
+
this.writeRecoverySnapshot();
|
|
657
|
+
return 'conflict';
|
|
658
|
+
}
|
|
659
|
+
writeRecoverySnapshot() {
|
|
660
|
+
if (!this.target || !this.recoveryJournal)
|
|
661
|
+
return;
|
|
662
|
+
if (this.persistedRevision >= this.revision && !this.conflict)
|
|
663
|
+
return;
|
|
664
|
+
this.recoveryJournal.write({
|
|
665
|
+
targetKey: this.target.key,
|
|
666
|
+
generation: this.generation,
|
|
667
|
+
revision: this.revision,
|
|
668
|
+
content: this.content,
|
|
669
|
+
persistedContent: this.persistedContent,
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
discardRecoverySnapshot() {
|
|
673
|
+
if (!this.target)
|
|
674
|
+
return;
|
|
675
|
+
this.recoveryJournal?.discard(this.target.key);
|
|
676
|
+
}
|
|
677
|
+
detach() {
|
|
678
|
+
this.clearAutoSaveTimer();
|
|
679
|
+
this.autoSaveRetryAttempt = 0;
|
|
680
|
+
this.generation += 1;
|
|
681
|
+
this.externalSequence = -1;
|
|
682
|
+
this.target = null;
|
|
683
|
+
this.persistedContent = null;
|
|
684
|
+
this.savingRevision = null;
|
|
685
|
+
this.savingContent = null;
|
|
686
|
+
this.error = null;
|
|
687
|
+
this.conflict = null;
|
|
688
|
+
this.haltDrain = false;
|
|
689
|
+
this.lifecycle = 'closed';
|
|
690
|
+
this.frozen = true;
|
|
691
|
+
this.emit();
|
|
692
|
+
}
|
|
693
|
+
assertNotPreparingClose() {
|
|
694
|
+
if (this.lifecycle === 'closing') {
|
|
695
|
+
throw new Error('The document is already prepared for close.');
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
enqueueOperation(operation) {
|
|
699
|
+
const run = this.operationTail.then(operation, operation);
|
|
700
|
+
this.operationTail = run.then(() => undefined, () => undefined);
|
|
701
|
+
return run;
|
|
702
|
+
}
|
|
703
|
+
createSnapshot() {
|
|
704
|
+
return Object.freeze({
|
|
705
|
+
targetKey: this.target?.key ?? null,
|
|
706
|
+
content: this.content,
|
|
707
|
+
revision: this.revision,
|
|
708
|
+
persistedRevision: this.persistedRevision,
|
|
709
|
+
persistedContent: this.persistedContent,
|
|
710
|
+
savingRevision: this.savingRevision,
|
|
711
|
+
generation: this.generation,
|
|
712
|
+
status: this.computeStatus(),
|
|
713
|
+
lifecycle: this.lifecycle,
|
|
714
|
+
frozen: this.frozen,
|
|
715
|
+
error: this.error,
|
|
716
|
+
conflict: this.conflict,
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
computeStatus() {
|
|
720
|
+
if (this.lifecycle === 'closed')
|
|
721
|
+
return 'closed';
|
|
722
|
+
if (!this.target)
|
|
723
|
+
return 'idle';
|
|
724
|
+
if (this.conflict)
|
|
725
|
+
return 'conflict';
|
|
726
|
+
if (this.error)
|
|
727
|
+
return 'error';
|
|
728
|
+
if (this.savingRevision !== null)
|
|
729
|
+
return 'saving';
|
|
730
|
+
return this.persistedRevision < this.revision ? 'dirty' : 'saved';
|
|
731
|
+
}
|
|
732
|
+
emit() {
|
|
733
|
+
this.snapshot = this.createSnapshot();
|
|
734
|
+
for (const listener of [...this.listeners])
|
|
735
|
+
listener();
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
function toError(error) {
|
|
739
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
740
|
+
}
|
|
741
|
+
function normalizeRetryDelays(delays) {
|
|
742
|
+
return delays.map((delay) => {
|
|
743
|
+
if (!Number.isFinite(delay) || delay < 0) {
|
|
744
|
+
throw new TypeError('Autosave retry delays must be finite, non-negative numbers.');
|
|
745
|
+
}
|
|
746
|
+
return Math.floor(delay);
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
//# sourceMappingURL=document-session.js.map
|