@marimo-team/frontend 0.25.3-dev100 → 0.25.3-dev102
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/package.json +1 -1
- package/src/core/document/__tests__/lifecycle-scenarios.test.ts +61 -0
- package/src/core/document/__tests__/model.test.ts +6 -331
- package/src/core/document/__tests__/protocol-fixtures.ts +38 -0
- package/src/core/document/__tests__/recovery-scenarios.test.ts +230 -0
- package/src/core/document/__tests__/scenario-fixture.ts +42 -0
- package/src/core/document/__tests__/scenarios.test.ts +205 -0
- package/src/core/document/__tests__/state.test.ts +5 -13
- package/src/core/document/__tests__/submission.test.ts +24 -167
- package/src/core/document/__tests__/test-host.test.ts +92 -0
- package/src/core/document/__tests__/test-host.ts +247 -0
- package/src/core/document/__tests__/test-notebook.ts +139 -0
- package/src/core/document/__tests__/user.ts +61 -0
- package/src/core/document/index.ts +3 -1
- package/src/core/document/model.ts +7 -8
- package/src/core/document/select.ts +32 -0
- package/src/core/document/state.ts +3 -30
- package/src/core/document/sync.ts +29 -25
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import type { Result } from "@marimo-team/client";
|
|
4
|
+
import type * as Protocol from "@marimo-team/client/protocol";
|
|
5
|
+
import { atom, type createStore } from "jotai/vanilla";
|
|
6
|
+
import { vi } from "vitest";
|
|
7
|
+
import { Deferred } from "@/utils/Deferred";
|
|
8
|
+
import { invariant } from "@/utils/invariant";
|
|
9
|
+
import type { Document } from "../../host/resources/document";
|
|
10
|
+
import type { DocumentHost } from "../model";
|
|
11
|
+
import { createTestHost, type EditRequest } from "./test-host";
|
|
12
|
+
|
|
13
|
+
export interface PendingRequest extends EditRequest {
|
|
14
|
+
readonly signal: AbortSignal;
|
|
15
|
+
process(): void;
|
|
16
|
+
deliverResponse(): void;
|
|
17
|
+
loseResponse(): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createTestNotebook(
|
|
21
|
+
store: ReturnType<typeof createStore>,
|
|
22
|
+
initial: Document,
|
|
23
|
+
) {
|
|
24
|
+
const host = createTestHost(initial);
|
|
25
|
+
const source = atom<Document | null>(initial);
|
|
26
|
+
const requests: PendingRequest[] = [];
|
|
27
|
+
const waiting: Deferred<PendingRequest>[] = [];
|
|
28
|
+
const documents: Document[] = [];
|
|
29
|
+
function apply(changes: readonly Protocol.Change[]) {
|
|
30
|
+
const { result, update } = host.apply(changes);
|
|
31
|
+
invariant(result.ok, "Another writer's edit was rejected");
|
|
32
|
+
if (update !== undefined) {
|
|
33
|
+
documents.push(update);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const edit = vi.fn<DocumentHost["edit"]>((notebookId, changes, options) => {
|
|
37
|
+
options.signal.throwIfAborted();
|
|
38
|
+
const reply = new Deferred<Result<Protocol.EditResponseOutput>>();
|
|
39
|
+
let outcome: Result<Protocol.EditResponseOutput> | undefined;
|
|
40
|
+
const abort = () => reply.reject(options.signal.reason);
|
|
41
|
+
const finish = () => options.signal.removeEventListener("abort", abort);
|
|
42
|
+
options.signal.addEventListener("abort", abort, { once: true });
|
|
43
|
+
void reply.promise.then(finish, finish);
|
|
44
|
+
const request: PendingRequest = {
|
|
45
|
+
notebookId,
|
|
46
|
+
changes: structuredClone(changes),
|
|
47
|
+
idempotencyKey: options.idempotencyKey,
|
|
48
|
+
signal: options.signal,
|
|
49
|
+
process() {
|
|
50
|
+
invariant(outcome === undefined, "Request was already processed");
|
|
51
|
+
// Cancellation of the client's wait does not roll back a server commit.
|
|
52
|
+
const processed = host.process(request);
|
|
53
|
+
outcome = processed.result;
|
|
54
|
+
if (processed.update !== undefined) {
|
|
55
|
+
documents.push(processed.update);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
deliverResponse() {
|
|
59
|
+
invariant(
|
|
60
|
+
outcome !== undefined,
|
|
61
|
+
"Process the request before delivering its response",
|
|
62
|
+
);
|
|
63
|
+
reply.resolve(outcome);
|
|
64
|
+
},
|
|
65
|
+
loseResponse() {
|
|
66
|
+
reply.reject(new Error("Connection lost"));
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
const next = waiting.shift();
|
|
70
|
+
if (next === undefined) {
|
|
71
|
+
requests.push(request);
|
|
72
|
+
} else {
|
|
73
|
+
next.resolve(request);
|
|
74
|
+
}
|
|
75
|
+
return reply.promise;
|
|
76
|
+
});
|
|
77
|
+
const notebook = { documentAtom: source, edit } satisfies DocumentHost;
|
|
78
|
+
return {
|
|
79
|
+
notebook,
|
|
80
|
+
control: {
|
|
81
|
+
document: host.document,
|
|
82
|
+
nextRequest(): Promise<PendingRequest> {
|
|
83
|
+
const next = requests.shift();
|
|
84
|
+
if (next !== undefined) {
|
|
85
|
+
return Promise.resolve(next);
|
|
86
|
+
}
|
|
87
|
+
const deferred = new Deferred<PendingRequest>();
|
|
88
|
+
waiting.push(deferred);
|
|
89
|
+
return deferred.promise;
|
|
90
|
+
},
|
|
91
|
+
changeCell(
|
|
92
|
+
id: Protocol.CellId,
|
|
93
|
+
patch: Partial<Omit<Protocol.CellInput, "id">>,
|
|
94
|
+
) {
|
|
95
|
+
const cell = host.document().cells.get(id);
|
|
96
|
+
invariant(
|
|
97
|
+
cell !== undefined && cell.code !== null,
|
|
98
|
+
`No host source for cell ${id}`,
|
|
99
|
+
);
|
|
100
|
+
apply([
|
|
101
|
+
{
|
|
102
|
+
kind: "put_cell",
|
|
103
|
+
base_etag: cell.etag,
|
|
104
|
+
cell: {
|
|
105
|
+
...cell,
|
|
106
|
+
code: cell.code,
|
|
107
|
+
...patch,
|
|
108
|
+
config: { ...cell.config, ...patch.config },
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
]);
|
|
112
|
+
},
|
|
113
|
+
createCell(cell: Protocol.CellInput) {
|
|
114
|
+
apply([{ kind: "put_cell", base_etag: null, cell }]);
|
|
115
|
+
},
|
|
116
|
+
deleteCell(id: Protocol.CellId) {
|
|
117
|
+
const cell = host.document().cells.get(id);
|
|
118
|
+
invariant(cell !== undefined, `No host cell ${id}`);
|
|
119
|
+
apply([{ kind: "delete_cell", id, base_etag: cell.etag }]);
|
|
120
|
+
},
|
|
121
|
+
deliverDocument() {
|
|
122
|
+
const next = documents.shift();
|
|
123
|
+
invariant(
|
|
124
|
+
next !== undefined,
|
|
125
|
+
"No document update is waiting for delivery",
|
|
126
|
+
);
|
|
127
|
+
store.set(source, next);
|
|
128
|
+
},
|
|
129
|
+
reset() {
|
|
130
|
+
documents.length = 0;
|
|
131
|
+
store.set(source, null);
|
|
132
|
+
},
|
|
133
|
+
deliverSnapshot() {
|
|
134
|
+
documents.length = 0;
|
|
135
|
+
store.set(source, host.document());
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import type * as Protocol from "@marimo-team/client/protocol";
|
|
4
|
+
import type { createStore } from "jotai/vanilla";
|
|
5
|
+
import type { DocumentModel } from "../model";
|
|
6
|
+
|
|
7
|
+
/** Translate user actions and observations without keeping any expected state. */
|
|
8
|
+
export function createUser(
|
|
9
|
+
store: ReturnType<typeof createStore>,
|
|
10
|
+
model: DocumentModel,
|
|
11
|
+
) {
|
|
12
|
+
function observe(promise: Promise<Protocol.Document>) {
|
|
13
|
+
// A failed assertion may end a scenario before it awaits sync; teardown
|
|
14
|
+
// should not obscure that failure with an unhandled abort rejection.
|
|
15
|
+
void promise.catch(() => undefined);
|
|
16
|
+
return promise;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
changeCode(id: Protocol.CellId, code: string) {
|
|
20
|
+
store.set(model.editAtom, (tx) => tx.setCode(id, code));
|
|
21
|
+
},
|
|
22
|
+
changeCells(
|
|
23
|
+
changes: Record<Protocol.CellId, Partial<Omit<Protocol.CellInput, "id">>>,
|
|
24
|
+
) {
|
|
25
|
+
store.set(model.editAtom, (tx) => {
|
|
26
|
+
for (const [id, patch] of Object.entries(changes)) {
|
|
27
|
+
if (patch.code !== undefined) {
|
|
28
|
+
tx.setCode(id, patch.code);
|
|
29
|
+
}
|
|
30
|
+
if (patch.name !== undefined) {
|
|
31
|
+
tx.setName(id, patch.name);
|
|
32
|
+
}
|
|
33
|
+
if (patch.config !== undefined) {
|
|
34
|
+
tx.updateConfig(id, patch.config);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
createCell(cell: Protocol.CellInput) {
|
|
40
|
+
store.set(model.editAtom, (tx) => tx.createCell(cell));
|
|
41
|
+
},
|
|
42
|
+
deleteCell(id: Protocol.CellId) {
|
|
43
|
+
store.set(model.editAtom, (tx) => tx.deleteCell(id));
|
|
44
|
+
},
|
|
45
|
+
cell(id: Protocol.CellId) {
|
|
46
|
+
const cell = store.get(model.documentAtom)?.cells.get(id);
|
|
47
|
+
return cell === undefined
|
|
48
|
+
? undefined
|
|
49
|
+
: {
|
|
50
|
+
id: cell.id,
|
|
51
|
+
code: cell.code,
|
|
52
|
+
name: cell.name,
|
|
53
|
+
config: cell.config,
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
order: () => store.get(model.documentAtom)?.order ?? null,
|
|
57
|
+
status: () => store.get(model.syncStatusAtom),
|
|
58
|
+
sync: () => observe(model.flush()),
|
|
59
|
+
retry: () => observe(model.retry()),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -6,18 +6,19 @@ import { createDocumentState, type DocumentState } from "./state";
|
|
|
6
6
|
import {
|
|
7
7
|
createDocumentSync,
|
|
8
8
|
type DocumentHost,
|
|
9
|
-
type
|
|
9
|
+
type DocumentSyncStatus,
|
|
10
10
|
} from "./sync";
|
|
11
11
|
|
|
12
12
|
export type { DocumentEdit, DocumentEditor } from "./state";
|
|
13
|
-
export type {
|
|
13
|
+
export type { EditableCell, EditableDocument } from "./draft";
|
|
14
|
+
export type { DocumentHost, DocumentSyncStatus } from "./sync";
|
|
14
15
|
|
|
15
16
|
/** The editable document and its synchronization with the host. */
|
|
16
17
|
export interface DocumentModel extends Pick<
|
|
17
18
|
DocumentState,
|
|
18
|
-
"
|
|
19
|
+
"documentAtom" | "editAtom"
|
|
19
20
|
> {
|
|
20
|
-
readonly
|
|
21
|
+
readonly syncStatusAtom: Atom<DocumentSyncStatus>;
|
|
21
22
|
/** Drain local edits and return the confirmed revision. */
|
|
22
23
|
flush(): Promise<Protocol.Document>;
|
|
23
24
|
/** Retry a rejection against fresh state, or repeat an uncertain request exactly. */
|
|
@@ -42,11 +43,9 @@ export function createDocumentModel({
|
|
|
42
43
|
});
|
|
43
44
|
const sync = createDocumentSync({ store, notebook, state, signal });
|
|
44
45
|
return {
|
|
45
|
-
|
|
46
|
-
orderAtom: state.orderAtom,
|
|
47
|
-
appConfigAtom: state.appConfigAtom,
|
|
46
|
+
documentAtom: state.documentAtom,
|
|
48
47
|
editAtom: state.editAtom,
|
|
49
|
-
|
|
48
|
+
syncStatusAtom: sync.statusAtom,
|
|
50
49
|
flush: sync.flush,
|
|
51
50
|
retry: sync.retry,
|
|
52
51
|
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import type * as Protocol from "@marimo-team/client/protocol";
|
|
4
|
+
import { dequal } from "dequal";
|
|
5
|
+
import type { Atom } from "jotai/vanilla";
|
|
6
|
+
import { selectAtom } from "jotai/vanilla/utils";
|
|
7
|
+
import { type EditableCell, type EditableDocument, sameCell } from "./draft";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Select one cell of a document.
|
|
11
|
+
*
|
|
12
|
+
* The atom notifies when the cell's code, name, or config changes. A new host
|
|
13
|
+
* record with the same values leaves it unchanged. Each call creates an atom,
|
|
14
|
+
* so keep it for as long as its subscription.
|
|
15
|
+
*/
|
|
16
|
+
export function selectCell(
|
|
17
|
+
document: Atom<EditableDocument | null>,
|
|
18
|
+
id: Protocol.CellId,
|
|
19
|
+
): Atom<EditableCell | undefined> {
|
|
20
|
+
return selectAtom(
|
|
21
|
+
document,
|
|
22
|
+
(value) => value?.cells.get(id),
|
|
23
|
+
(a, b) => a === b || (a !== undefined && b !== undefined && sameCell(a, b)),
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Select a document's cell order, null while the document is unavailable. */
|
|
28
|
+
export function selectOrder(
|
|
29
|
+
document: Atom<EditableDocument | null>,
|
|
30
|
+
): Atom<readonly Protocol.CellId[] | null> {
|
|
31
|
+
return selectAtom(document, (value) => value?.order ?? null, dequal);
|
|
32
|
+
}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
|
-
import { dequal } from "dequal";
|
|
4
3
|
import {
|
|
5
4
|
atom,
|
|
6
5
|
type Atom,
|
|
7
6
|
type createStore,
|
|
8
7
|
type WritableAtom,
|
|
9
8
|
} from "jotai/vanilla";
|
|
10
|
-
import { selectAtom } from "jotai/vanilla/utils";
|
|
11
9
|
import type { Document } from "../host/resources/document";
|
|
12
10
|
import * as Protocol from "@marimo-team/client/protocol";
|
|
13
11
|
import {
|
|
@@ -16,14 +14,12 @@ import {
|
|
|
16
14
|
createCell,
|
|
17
15
|
deleteCell,
|
|
18
16
|
type Draft,
|
|
19
|
-
type EditableCell,
|
|
20
17
|
type EditableDocument,
|
|
21
18
|
editCell,
|
|
22
19
|
EMPTY_DRAFT,
|
|
23
20
|
isEmptyDraft,
|
|
24
21
|
type Placement,
|
|
25
22
|
prune,
|
|
26
|
-
sameCell,
|
|
27
23
|
setOrder,
|
|
28
24
|
toChanges,
|
|
29
25
|
viewDocument,
|
|
@@ -70,12 +66,8 @@ interface ModelState {
|
|
|
70
66
|
* Switching notebook identity invalidates it, retaining each notebook's draft.
|
|
71
67
|
*/
|
|
72
68
|
export interface DocumentState {
|
|
73
|
-
/**
|
|
74
|
-
|
|
75
|
-
readonly orderAtom: Atom<readonly Protocol.CellId[] | null>;
|
|
76
|
-
readonly appConfigAtom: Atom<Readonly<
|
|
77
|
-
Protocol.Document["app_config"]
|
|
78
|
-
> | null>;
|
|
69
|
+
/** Confirmed host state with local drafts applied, null while unavailable. */
|
|
70
|
+
readonly documentAtom: Atom<EditableDocument | null>;
|
|
79
71
|
/** Null when the document or source needed by a put is unavailable. */
|
|
80
72
|
readonly changesAtom: Atom<readonly Protocol.Change[] | null>;
|
|
81
73
|
/** Remains pending until both the response and its streamed revision arrive. */
|
|
@@ -158,23 +150,6 @@ export function createDocumentState({
|
|
|
158
150
|
? null
|
|
159
151
|
: viewDocument(state.confirmed, draftOf(state));
|
|
160
152
|
});
|
|
161
|
-
const cellAtom = (id: Protocol.CellId) =>
|
|
162
|
-
selectAtom(
|
|
163
|
-
documentAtom,
|
|
164
|
-
(document) => document?.cells.get(id),
|
|
165
|
-
(a, b) =>
|
|
166
|
-
a === b || (a !== undefined && b !== undefined && sameCell(a, b)),
|
|
167
|
-
);
|
|
168
|
-
const orderAtom = selectAtom(
|
|
169
|
-
documentAtom,
|
|
170
|
-
(document) => document?.order ?? null,
|
|
171
|
-
dequal,
|
|
172
|
-
);
|
|
173
|
-
const appConfigAtom = selectAtom(
|
|
174
|
-
stateAtom,
|
|
175
|
-
(state) => state.confirmed?.revision.app_config ?? null,
|
|
176
|
-
dequal,
|
|
177
|
-
);
|
|
178
153
|
const changesAtom = atom((get): readonly Protocol.Change[] | null => {
|
|
179
154
|
const state = get(stateAtom);
|
|
180
155
|
return state.confirmed === null
|
|
@@ -304,9 +279,7 @@ export function createDocumentState({
|
|
|
304
279
|
}
|
|
305
280
|
|
|
306
281
|
return {
|
|
307
|
-
|
|
308
|
-
orderAtom,
|
|
309
|
-
appConfigAtom,
|
|
282
|
+
documentAtom,
|
|
310
283
|
changesAtom,
|
|
311
284
|
pendingAtom,
|
|
312
285
|
editAtom,
|
|
@@ -9,7 +9,7 @@ import type { DocumentState, DocumentSubmission } from "./state";
|
|
|
9
9
|
/** The notebook state and operation needed by document editing. */
|
|
10
10
|
export type DocumentHost = Pick<NotebookClient, "documentAtom" | "edit">;
|
|
11
11
|
|
|
12
|
-
export type
|
|
12
|
+
export type DocumentSyncStatus =
|
|
13
13
|
| {
|
|
14
14
|
readonly status:
|
|
15
15
|
| "idle"
|
|
@@ -26,7 +26,7 @@ export type DocumentSyncState =
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
interface Submission {
|
|
29
|
-
readonly
|
|
29
|
+
readonly submission: DocumentSubmission;
|
|
30
30
|
readonly idempotencyKey: string;
|
|
31
31
|
readonly startedAt: number;
|
|
32
32
|
readonly controller: AbortController;
|
|
@@ -85,7 +85,7 @@ export function createDocumentSync({
|
|
|
85
85
|
}) {
|
|
86
86
|
signal.throwIfAborted();
|
|
87
87
|
const source = notebook.documentAtom;
|
|
88
|
-
const
|
|
88
|
+
const statusAtom = atom<DocumentSyncStatus>({ status: "idle" });
|
|
89
89
|
const observed = atom((get) => ({
|
|
90
90
|
document: get(source),
|
|
91
91
|
pending: get(state.pendingAtom),
|
|
@@ -93,7 +93,7 @@ export function createDocumentSync({
|
|
|
93
93
|
}));
|
|
94
94
|
let active: Submission | null = null;
|
|
95
95
|
let running: Promise<Protocol.Document> | null = null;
|
|
96
|
-
let failure: Extract<
|
|
96
|
+
let failure: Extract<DocumentSyncStatus, { status: "error" }> | null = null;
|
|
97
97
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
98
98
|
let refreshing = false;
|
|
99
99
|
|
|
@@ -118,7 +118,7 @@ export function createDocumentSync({
|
|
|
118
118
|
if (
|
|
119
119
|
active &&
|
|
120
120
|
current &&
|
|
121
|
-
current.revision.notebook_id !== active.
|
|
121
|
+
current.revision.notebook_id !== active.submission.notebookId
|
|
122
122
|
) {
|
|
123
123
|
active.controller.abort(
|
|
124
124
|
new Error("The notebook changed during synchronization"),
|
|
@@ -130,13 +130,13 @@ export function createDocumentSync({
|
|
|
130
130
|
clearTimer();
|
|
131
131
|
const changes = store.get(state.changesAtom);
|
|
132
132
|
if (changes === null) {
|
|
133
|
-
store.set(
|
|
133
|
+
store.set(statusAtom, { status: "unavailable" });
|
|
134
134
|
} else if (changes.length === 0) {
|
|
135
135
|
// Release unsent reversals instead of keeping an invisible override.
|
|
136
136
|
store.set(state.submitAtom);
|
|
137
|
-
store.set(
|
|
137
|
+
store.set(statusAtom, { status: "idle" });
|
|
138
138
|
} else {
|
|
139
|
-
store.set(
|
|
139
|
+
store.set(statusAtom, { status: "pending" });
|
|
140
140
|
timer = setTimeout(() => {
|
|
141
141
|
void flush().catch(() => undefined);
|
|
142
142
|
}, DEBOUNCE_MS);
|
|
@@ -155,7 +155,7 @@ export function createDocumentSync({
|
|
|
155
155
|
"The edit's retry window expired; its outcome is unknown",
|
|
156
156
|
);
|
|
157
157
|
}
|
|
158
|
-
store.set(
|
|
158
|
+
store.set(statusAtom, { status: "sending" });
|
|
159
159
|
const controller = new AbortController();
|
|
160
160
|
const abort = () => controller.abort(request.controller.signal.reason);
|
|
161
161
|
request.controller.signal.addEventListener("abort", abort, {
|
|
@@ -170,10 +170,14 @@ export function createDocumentSync({
|
|
|
170
170
|
signal.throwIfAborted();
|
|
171
171
|
request.controller.signal.throwIfAborted();
|
|
172
172
|
result = await abortable(
|
|
173
|
-
notebook.edit(
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
173
|
+
notebook.edit(
|
|
174
|
+
request.submission.notebookId,
|
|
175
|
+
request.submission.changes,
|
|
176
|
+
{
|
|
177
|
+
idempotencyKey: request.idempotencyKey,
|
|
178
|
+
signal: controller.signal,
|
|
179
|
+
},
|
|
180
|
+
),
|
|
177
181
|
controller.signal,
|
|
178
182
|
);
|
|
179
183
|
} catch (error) {
|
|
@@ -198,17 +202,17 @@ export function createDocumentSync({
|
|
|
198
202
|
cause: result.problem,
|
|
199
203
|
});
|
|
200
204
|
}
|
|
201
|
-
if (result.data.document.notebook_id !== request.
|
|
205
|
+
if (result.data.document.notebook_id !== request.submission.notebookId) {
|
|
202
206
|
throw new Error("Edit response belongs to another notebook");
|
|
203
207
|
}
|
|
204
208
|
request.response = result.data.document;
|
|
205
|
-
store.set(state.settleAtom, request.
|
|
209
|
+
store.set(state.settleAtom, request.submission, request.response);
|
|
206
210
|
return;
|
|
207
211
|
}
|
|
208
212
|
}
|
|
209
213
|
|
|
210
214
|
function confirm(request: Submission): Promise<void> {
|
|
211
|
-
store.set(
|
|
215
|
+
store.set(statusAtom, { status: "confirming" });
|
|
212
216
|
return new Promise((resolve, reject) => {
|
|
213
217
|
const check = () => {
|
|
214
218
|
if (signal.aborted || request.controller.signal.aborted) {
|
|
@@ -216,7 +220,7 @@ export function createDocumentSync({
|
|
|
216
220
|
reject(
|
|
217
221
|
signal.aborted ? signal.reason : request.controller.signal.reason,
|
|
218
222
|
);
|
|
219
|
-
} else if (store.get(state.pendingAtom) !== request.
|
|
223
|
+
} else if (store.get(state.pendingAtom) !== request.submission) {
|
|
220
224
|
cleanup();
|
|
221
225
|
resolve();
|
|
222
226
|
}
|
|
@@ -247,12 +251,12 @@ export function createDocumentSync({
|
|
|
247
251
|
if (current === null || store.get(state.changesAtom) === null) {
|
|
248
252
|
throw unavailable();
|
|
249
253
|
}
|
|
250
|
-
const
|
|
251
|
-
if (
|
|
254
|
+
const submission = store.set(state.submitAtom);
|
|
255
|
+
if (submission === null) {
|
|
252
256
|
return current.revision;
|
|
253
257
|
}
|
|
254
258
|
active = {
|
|
255
|
-
|
|
259
|
+
submission,
|
|
256
260
|
idempotencyKey: idempotencyKey(),
|
|
257
261
|
startedAt: Date.now(),
|
|
258
262
|
controller: new AbortController(),
|
|
@@ -282,7 +286,7 @@ export function createDocumentSync({
|
|
|
282
286
|
Date.now() - active.startedAt < RETRY_WINDOW_MS),
|
|
283
287
|
};
|
|
284
288
|
if (!signal.aborted) {
|
|
285
|
-
store.set(
|
|
289
|
+
store.set(statusAtom, failure);
|
|
286
290
|
}
|
|
287
291
|
throw error;
|
|
288
292
|
} finally {
|
|
@@ -305,7 +309,7 @@ export function createDocumentSync({
|
|
|
305
309
|
return running;
|
|
306
310
|
}
|
|
307
311
|
const notebookId =
|
|
308
|
-
active?.
|
|
312
|
+
active?.submission.notebookId ?? store.get(source)?.revision.notebook_id;
|
|
309
313
|
if (notebookId === undefined) {
|
|
310
314
|
return Promise.reject(unavailable());
|
|
311
315
|
}
|
|
@@ -329,14 +333,14 @@ export function createDocumentSync({
|
|
|
329
333
|
const rejectedAt = active.problem.edit?.document.version;
|
|
330
334
|
if (
|
|
331
335
|
!current ||
|
|
332
|
-
current.revision.notebook_id !== active.
|
|
336
|
+
current.revision.notebook_id !== active.submission.notebookId ||
|
|
333
337
|
(rejectedAt !== undefined && current.revision.version < rejectedAt)
|
|
334
338
|
) {
|
|
335
339
|
return Promise.reject(
|
|
336
340
|
new Error("Wait for the host's current document before retrying"),
|
|
337
341
|
);
|
|
338
342
|
}
|
|
339
|
-
store.set(state.settleAtom, active.
|
|
343
|
+
store.set(state.settleAtom, active.submission, null);
|
|
340
344
|
active = null;
|
|
341
345
|
}
|
|
342
346
|
failure = null;
|
|
@@ -355,5 +359,5 @@ export function createDocumentSync({
|
|
|
355
359
|
stop();
|
|
356
360
|
}
|
|
357
361
|
refresh();
|
|
358
|
-
return {
|
|
362
|
+
return { statusAtom, flush, retry };
|
|
359
363
|
}
|