@marimo-team/islands 0.25.3-dev373 → 0.25.3-dev375

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.
Files changed (101) hide show
  1. package/dist/{ConnectedDataExplorerComponent-BzohsNSl.js → ConnectedDataExplorerComponent-BpCrAeX0.js} +9 -9
  2. package/dist/{ErrorBoundary-CBhEVnmj.js → ErrorBoundary-Bceanw8E.js} +6 -6
  3. package/dist/{abnfDiagram-VCTEODGH-ChqGZ2ry.js → abnfDiagram-VCTEODGH-ZqNgGZlz.js} +2 -2
  4. package/dist/{any-language-editor-jTRRyM00.js → any-language-editor-CFbAr7vc.js} +2 -2
  5. package/dist/{architectureDiagram-5GKGNRK7-CnN59kY1.js → architectureDiagram-5GKGNRK7-BEUaR96d.js} +1 -1
  6. package/dist/{assertNever-C77RgMvO.js → assertNever-5uYEbJc8.js} +2 -2
  7. package/dist/{chat-ui-DfIJW9DA.js → chat-ui-BKSYhGMh.js} +12 -12
  8. package/dist/{check-BTtJ1HuS.js → check-BbhVyViy.js} +1 -1
  9. package/dist/{chunk-IMKFNOWR-DW_he6xH.js → chunk-IMKFNOWR-BdEPmWQI.js} +1 -1
  10. package/dist/{chunk-SHT3W25Y-rRSc_SiF.js → chunk-SHT3W25Y-IvvXM3xf.js} +1 -1
  11. package/dist/{chunk-SVP7TREG-B_G3ACtc.js → chunk-SVP7TREG-C_qpJUv1.js} +1 -1
  12. package/dist/{chunk-TICWLB2K-Bdqk9L9O.js → chunk-TICWLB2K-C4agYVWP.js} +1 -1
  13. package/dist/{classDiagram-ZZMXUADV-bkoIJ4ZI.js → classDiagram-ZZMXUADV-CT2iq8uF.js} +2 -2
  14. package/dist/{classDiagram-v2-VYDZK3BY-QvUGvghM.js → classDiagram-v2-VYDZK3BY-Coe_dD0r.js} +2 -2
  15. package/dist/{code-visibility-P2WZ3qgF.js → code-visibility-CxtxLHrv.js} +5 -5
  16. package/dist/{common-BGrRKU0g.js → common-SqvtVOs2.js} +19 -19
  17. package/dist/{config-D9r7V8wm.js → config-DJ8M-gPL.js} +3 -3
  18. package/dist/{copy-icon-AmDlztRw.js → copy-icon-xWvA8ybp.js} +2 -2
  19. package/dist/{createReducer-iKO-TaDo.js → createReducer-fp0gX_9U.js} +1 -1
  20. package/dist/{cynefinDiagram-5FMLGOSQ-BtDkWTBE.js → cynefinDiagram-5FMLGOSQ-D0LKziUu.js} +1 -1
  21. package/dist/{dagre-GXQ25YYZ-D0at1MO5.js → dagre-GXQ25YYZ-Cmgziaw8.js} +1 -1
  22. package/dist/{data-grid-overlay-editor-DkDG8asb.js → data-grid-overlay-editor-QC6ZFp8P.js} +1 -1
  23. package/dist/{diagram-S7CK7UJ4-DeEaMqSV.js → diagram-S7CK7UJ4-D-lZsALU.js} +1 -1
  24. package/dist/{diagram-UQ7AKVKN-BbRfuaEy.js → diagram-UQ7AKVKN-TcbdsFoT.js} +1 -1
  25. package/dist/{diagram-VX7I27RA-B4Wp-rDT.js → diagram-VX7I27RA-D5BqZi1q.js} +1 -1
  26. package/dist/{diagram-Z3DM3KII-C_ae4cSF.js → diagram-Z3DM3KII-MNc7jqvr.js} +1 -1
  27. package/dist/{dialog-Daog9NU1.js → dialog-Cm0VGdxc.js} +2 -2
  28. package/dist/{dropdown-menu-CnoeAarE.js → dropdown-menu-DfamXjVI.js} +5 -5
  29. package/dist/{ebnfDiagram-PWID7BFC-BPTzY6rk.js → ebnfDiagram-PWID7BFC-Cxt6ywyX.js} +2 -2
  30. package/dist/{erDiagram-RLTQ6QDP-udqrzZ59.js → erDiagram-RLTQ6QDP-BLF-askI.js} +1 -1
  31. package/dist/{error-banner-ChszWQ3h.js → error-banner-k06CAftu.js} +1 -1
  32. package/dist/{error-entries-IU7i5tD8.js → error-entries-rtPWQkJk.js} +10 -10
  33. package/dist/{feedback-button-hJmlXItP.js → feedback-button-VpqMqF9W.js} +8 -8
  34. package/dist/{flowDiagram-HODETNUW-CU1S_qjB.js → flowDiagram-HODETNUW-CsLw2mWn.js} +2 -2
  35. package/dist/{formats-B85hYR8d.js → formats-DQ-qq-dU.js} +1 -1
  36. package/dist/{glide-data-editor-DTflymx8.js → glide-data-editor-Bb6KYYMs.js} +11 -11
  37. package/dist/{glide-utils-DPwU6aTw.js → glide-utils-CHrghEu-.js} +2 -2
  38. package/dist/{globals-DyhQVCCv.js → globals-BarVvbhM.js} +3 -3
  39. package/dist/{html-to-image-BiNaAyYX.js → html-to-image-B5nTOeMI.js} +13 -13
  40. package/dist/{infoDiagram-27XIBGKW-DKkp_SC7.js → infoDiagram-27XIBGKW-CdSApBWg.js} +1 -1
  41. package/dist/{ishikawaDiagram-5VMMS53U-C_TlrS-s.js → ishikawaDiagram-5VMMS53U-BnzQ8SI5.js} +1 -1
  42. package/dist/{kanban-definition-UXKFOSKX-CoqGlHyo.js → kanban-definition-UXKFOSKX-D2llTpeX.js} +1 -1
  43. package/dist/main.js +3332 -3295
  44. package/dist/{mermaid-DD6lvLSi.js → mermaid-BcNMyKmf.js} +34 -34
  45. package/dist/{mindmap-definition-YA3MSWOX-DQ3RIRq_.js → mindmap-definition-YA3MSWOX-uoEsfD5R.js} +1 -1
  46. package/dist/{number-overlay-editor-D88EwUgZ.js → number-overlay-editor-CzSb_u5N.js} +1 -1
  47. package/dist/{pegDiagram-XKGWAZYB-DjP6v_UE.js → pegDiagram-XKGWAZYB-DTWrE0S4.js} +2 -2
  48. package/dist/{pencil-YvDg98Vt.js → pencil-weySvN0N.js} +1 -1
  49. package/dist/{pieDiagram-E7YTZNPT-Cq2pOY9y.js → pieDiagram-E7YTZNPT-COME2oGN.js} +1 -1
  50. package/dist/{process-output-BA7BGk1s.js → process-output-DNxpsd4n.js} +4 -4
  51. package/dist/{railroadDiagram-O6MQD6OU-BgTlJnKs.js → railroadDiagram-O6MQD6OU-CUeFcNTY.js} +2 -2
  52. package/dist/{requirementDiagram-BXWQKSXE-ZJoJ9EfE.js → requirementDiagram-BXWQKSXE-BUR4lDZG.js} +1 -1
  53. package/dist/{reveal-component-CfyL5u-u.js → reveal-component-BYskmwso.js} +8 -8
  54. package/dist/{select-BoGBoXrZ.js → select-Ca-POp4c.js} +4 -4
  55. package/dist/{sequenceDiagram-WJ2MYXX4-BJvdtgI2.js → sequenceDiagram-WJ2MYXX4-CSPmml3q.js} +1 -1
  56. package/dist/{spec-C7YYRfV5.js → spec-BgSMwzks.js} +4 -4
  57. package/dist/{stateDiagram-D77RDMKH-UBhd6uK-.js → stateDiagram-D77RDMKH-BDUC8Pi9.js} +2 -2
  58. package/dist/{stateDiagram-v2-MP3YSRHH-Cx2Xr13h.js → stateDiagram-v2-MP3YSRHH-BH-qyk8a.js} +2 -2
  59. package/dist/{static-state-Dn05_alZ.js → static-state-Cqzv5X7B.js} +1 -1
  60. package/dist/{swimlanes-42K2YHIH-uVbva14y.js → swimlanes-42K2YHIH-CLE9Kiir.js} +1 -1
  61. package/dist/{swimlanesDiagram-VR7AAH4N-D2gn-4Ks.js → swimlanesDiagram-VR7AAH4N-Cv8kWAeA.js} +2 -2
  62. package/dist/{timeline-definition-24CTP7MA-DmIHJmrk.js → timeline-definition-24CTP7MA-fbRGX21i.js} +1 -1
  63. package/dist/{toast-CXcT9TJm.js → toast-0xspcdmv.js} +3 -3
  64. package/dist/{tooltip-DbRnGlK2.js → tooltip-BwlFTaNy.js} +1 -1
  65. package/dist/{use-restore-focus-hhbpbJwV.js → use-restore-focus-fKgXR0nb.js} +1 -1
  66. package/dist/{useDateFormatter-BhUfu7Zv.js → useDateFormatter-B7LvPmsw.js} +2 -2
  67. package/dist/{useDeepCompareMemoize-Crc0VFdZ.js → useDeepCompareMemoize-OhBYt-q2.js} +1 -1
  68. package/dist/{useLifecycle-BCXa5UQv.js → useLifecycle-D9xcFAIN.js} +1 -1
  69. package/dist/{vega-component-DqWRZerK.js → vega-component-Chak9Aon.js} +6 -6
  70. package/dist/{vennDiagram-4TSXK5OY-BYps24XX.js → vennDiagram-4TSXK5OY-BzKtg8gL.js} +1 -1
  71. package/dist/{wardleyDiagram-VM6X3IG4-eJTaJjCK.js → wardleyDiagram-VM6X3IG4-R0SgDvz1.js} +1 -1
  72. package/dist/{xychartDiagram-S5SC5T6Z-DUHbPn-K.js → xychartDiagram-S5SC5T6Z-DRskTHEM.js} +1 -1
  73. package/package.json +1 -1
  74. package/src/__mocks__/document.tsx +1 -0
  75. package/src/__mocks__/executions.ts +3 -1
  76. package/src/components/editor/actions/__tests__/name-cell-input.test.tsx +1 -0
  77. package/src/components/editor/actions/__tests__/useCellActionButton.test.tsx +1 -0
  78. package/src/components/editor/chrome/wrapper/__tests__/document-sync-status.test.tsx +3 -1
  79. package/src/core/cells/__tests__/flush-document.test.ts +3 -1
  80. package/src/core/config/__tests__/app-config.test.ts +1 -1
  81. package/src/core/executions/atoms.ts +2 -2
  82. package/src/core/islands/bootstrap.ts +1 -0
  83. package/src/core/meta/__tests__/code-visibility.test.tsx +1 -1
  84. package/src/core/notebook/document/__tests__/codemirror.test.ts +1 -0
  85. package/src/core/notebook/document/__tests__/hooks.test.tsx +1 -0
  86. package/src/core/notebook/document/__tests__/model.test.ts +6 -1
  87. package/src/core/notebook/document/__tests__/scenario-fixture.ts +1 -0
  88. package/src/core/notebook/document/__tests__/scenarios.test.ts +61 -0
  89. package/src/core/notebook/document/__tests__/state.test.ts +1 -0
  90. package/src/core/notebook/document/__tests__/submission.test.ts +1 -0
  91. package/src/core/notebook/document/__tests__/test-notebook.ts +14 -2
  92. package/src/core/notebook/document/model.ts +6 -3
  93. package/src/core/notebook/document/state.ts +38 -15
  94. package/src/core/notebook/replica/__tests__/document.test.ts +12 -7
  95. package/src/core/notebook/replica/__tests__/replica.test.ts +69 -5
  96. package/src/core/notebook/replica/replica.ts +71 -24
  97. package/src/core/notebook/replica/resources/document.ts +1 -1
  98. package/src/core/saving/__tests__/state.test.ts +1 -1
  99. package/src/core/session/active-notebook.ts +5 -4
  100. package/src/core/session/useNotebookSession.tsx +1 -0
  101. package/src/core/transports/__tests__/server.test.ts +5 -1
@@ -3,7 +3,7 @@ import { t } from "./src-D5uZaZib.js";
3
3
  import { n } from "./chunk-Y2CYZVJY-DKKS1VyV.js";
4
4
  import { $ as r, H as i, K as a, U as o, a as s, b as c, c as l, et as u, f as d, nt as f, tt as p, v as m, w as h, y as g } from "./chunk-DU6HZSFF-4SDIhUTY.js";
5
5
  import { t as _ } from "./rough.esm-CtV9AnV1.js";
6
- import { p as v } from "./mermaid-DD6lvLSi.js";
6
+ import { p as v } from "./mermaid-BcNMyKmf.js";
7
7
  //#region ../node_modules/.pnpm/khroma@2.1.0/node_modules/khroma/dist/methods/transparentize.js
8
8
  var y = (e, t) => p(e, "a", -t), b = 1e-10;
9
9
  function x(e, t) {
@@ -2,7 +2,7 @@ import { i as e } from "./chunk-75Z2AOVW-76L6My3P.js";
2
2
  import { n as t } from "./src-D5uZaZib.js";
3
3
  import { n } from "./chunk-Y2CYZVJY-DKKS1VyV.js";
4
4
  import { D as r, H as i, K as a, U as o, a as s, b as c, c as l, v as u, w as d, x as f, y as p } from "./chunk-DU6HZSFF-4SDIhUTY.js";
5
- import { p as m } from "./mermaid-DD6lvLSi.js";
5
+ import { p as m } from "./mermaid-BcNMyKmf.js";
6
6
  import { n as h } from "./mermaid-parser.core-DtOnl7m4.js";
7
7
  import { t as g } from "./chunk-JWPE2WC7-DJvv1t8U.js";
8
8
  //#region ../node_modules/.pnpm/mermaid@11.17.2/node_modules/mermaid/dist/chunks/mermaid.core/wardleyDiagram-VM6X3IG4.mjs
@@ -9,7 +9,7 @@ import { n as s } from "./chunk-Y2CYZVJY-DKKS1VyV.js";
9
9
  import { D as c, H as l, K as u, U as d, a as f, b as p, c as m, f as h, v as g, w as _, y as v, z as y } from "./chunk-DU6HZSFF-4SDIhUTY.js";
10
10
  import "./chunk-PWAF6VOD-CRu_RGP2.js";
11
11
  import { t as b } from "./chunk-GMAD6QVW-BJGF8QZL.js";
12
- import { p as x } from "./mermaid-DD6lvLSi.js";
12
+ import { p as x } from "./mermaid-BcNMyKmf.js";
13
13
  //#region ../node_modules/.pnpm/d3-scale@4.0.2/node_modules/d3-scale/src/band.js
14
14
  function S() {
15
15
  var e = t().unknown(void 0), r = e.domain, i = e.range, o = 0, s = 1, c, l, u = !1, d = 0, f = 0, p = .5;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marimo-team/islands",
3
- "version": "0.25.3-dev373",
3
+ "version": "0.25.3-dev375",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -29,6 +29,7 @@ export function mockDocumentModel(
29
29
  const model = createDocumentModel({
30
30
  store,
31
31
  documentAtom,
32
+ shownDocumentAtom: documentAtom,
32
33
  backendAtom: atom(fakeBackend()),
33
34
  });
34
35
  model.start({ signal: controller.signal });
@@ -198,6 +198,7 @@ export const MockExecutions = {
198
198
  document: Document | null = null,
199
199
  ) {
200
200
  const current = store.get(activeNotebookReplicaAtom);
201
+ const documentAtom = atom<Document | null>(document);
201
202
  const replica = {
202
203
  attachmentsAtom: atom([]),
203
204
  connect: () => () => undefined,
@@ -208,7 +209,8 @@ export const MockExecutions = {
208
209
  connectionAtom: atom<ConnectionStatus>({ state: ConnectionState.OPEN }),
209
210
  backendAtom: atom<NotebookBackend | null>(null),
210
211
  notebookAtom: atom<Protocol.Notebook | null>(MockExecutions.notebook()),
211
- documentAtom: atom<Document | null>(document),
212
+ documentAtom,
213
+ shownDocumentAtom: documentAtom,
212
214
  runtimeAtom: atom(runtime),
213
215
  executionsAtom: atom(mirror(records)),
214
216
  resultsAtom: atom(results(records)),
@@ -37,6 +37,7 @@ describe("NameCellContentEditable", () => {
37
37
  const model = createDocumentModel({
38
38
  store,
39
39
  documentAtom: source,
40
+ shownDocumentAtom: source,
40
41
  backendAtom: atom(fakeBackend()),
41
42
  });
42
43
  const controller = new AbortController();
@@ -34,6 +34,7 @@ function renderActions(cellId: CellId, name: string, edit?: Edit) {
34
34
  activeNotebookReplicaAtom,
35
35
  partial<NotebookReplica>({
36
36
  documentAtom,
37
+ shownDocumentAtom: documentAtom,
37
38
  notebookAtom: atom(
38
39
  partial<NotebookOutput>({ operations: ["notebook.edit"] }),
39
40
  ),
@@ -35,9 +35,11 @@ const conflict = {
35
35
 
36
36
  function setup(edit: FakeEdit) {
37
37
  const store = createStore();
38
+ const source = atom(document());
38
39
  const model = createDocumentModel({
39
40
  store,
40
- documentAtom: atom(document()),
41
+ documentAtom: source,
42
+ shownDocumentAtom: source,
41
43
  backendAtom: atom(fakeBackend({ edit })),
42
44
  });
43
45
  const controller = new AbortController();
@@ -22,9 +22,11 @@ afterEach(() => {
22
22
  describe("flushDocument", () => {
23
23
  it("sends nothing while the document is unavailable", async () => {
24
24
  const edit = vi.fn<FakeEdit>();
25
+ const source = atom<Document | null>(null);
25
26
  const model = createDocumentModel({
26
27
  store,
27
- documentAtom: atom<Document | null>(null),
28
+ documentAtom: source,
29
+ shownDocumentAtom: source,
28
30
  backendAtom: atom(fakeBackend({ edit })),
29
31
  });
30
32
  const controller = new AbortController();
@@ -26,7 +26,7 @@ function document(app_config: Record<string, unknown>, version = 1): Document {
26
26
  function connect(store: ReturnType<typeof createStore>, current: Document) {
27
27
  store.set(
28
28
  activeNotebookReplicaAtom,
29
- partial<NotebookReplica>({ documentAtom: atom(current) }, "replica"),
29
+ partial<NotebookReplica>({ shownDocumentAtom: atom(current) }, "replica"),
30
30
  );
31
31
  }
32
32
 
@@ -143,7 +143,7 @@ export const cellExecutionInputsAtom = atom((get): CellExecutionInputs => {
143
143
  const replica = get(activeNotebookReplicaAtom);
144
144
  const notebookId =
145
145
  replica === null ? null : (get(replica.notebookAtom)?.id ?? null);
146
- const document = replica === null ? null : get(replica.documentAtom);
146
+ const document = replica === null ? null : get(replica.shownDocumentAtom);
147
147
  return {
148
148
  executions: replica === null ? NO_EXECUTIONS : get(replica.executionsAtom),
149
149
  results: replica === null ? NO_RESULTS : get(replica.resultsAtom),
@@ -155,7 +155,7 @@ export const cellExecutionInputsAtom = atom((get): CellExecutionInputs => {
155
155
 
156
156
  const cellFreshnessInputsAtom = atom((get): CellFreshnessInputs => {
157
157
  const replica = get(activeNotebookReplicaAtom);
158
- const document = replica === null ? null : get(replica.documentAtom);
158
+ const document = replica === null ? null : get(replica.shownDocumentAtom);
159
159
  // Results can arrive before the confirmed document on a snapshot. Until
160
160
  // the document is ready there is nothing to compare them against.
161
161
  if (replica === null || document === null) {
@@ -140,6 +140,7 @@ export async function initializeIslands(
140
140
  const documentModel = createDocumentModel({
141
141
  store,
142
142
  documentAtom: replica.documentAtom,
143
+ shownDocumentAtom: replica.shownDocumentAtom,
143
144
  backendAtom: replica.backendAtom,
144
145
  });
145
146
  const lifetime = new AbortController();
@@ -73,7 +73,7 @@ function makeStore({
73
73
  store.set(
74
74
  activeNotebookReplicaAtom,
75
75
  partial<NotebookReplica>(
76
- { documentAtom: atom(document(codes)) },
76
+ { shownDocumentAtom: atom(document(codes)) },
77
77
  "replica",
78
78
  ),
79
79
  );
@@ -128,6 +128,7 @@ function setup({
128
128
  const model = createDocumentModel({
129
129
  store,
130
130
  documentAtom: source,
131
+ shownDocumentAtom: source,
131
132
  backendAtom: atom(fakeBackend({ edit: () => new Promise(() => {}) })),
132
133
  });
133
134
  const controller = new AbortController();
@@ -35,6 +35,7 @@ function setup(
35
35
  const model = createDocumentModel({
36
36
  store,
37
37
  documentAtom: source,
38
+ shownDocumentAtom: source,
38
39
  backendAtom: atom(fakeBackend({ edit })),
39
40
  });
40
41
  const controller = new AbortController();
@@ -55,6 +55,7 @@ function setup({ connected = true } = {}) {
55
55
  const model = createDocumentModel({
56
56
  store,
57
57
  documentAtom: source,
58
+ shownDocumentAtom: source,
58
59
  backendAtom,
59
60
  });
60
61
  const controller = new AbortController();
@@ -94,6 +95,7 @@ describe("document synchronization", () => {
94
95
  const model = createDocumentModel({
95
96
  store,
96
97
  documentAtom: source,
98
+ shownDocumentAtom: source,
97
99
  backendAtom: atom(fakeBackend({ edit })),
98
100
  });
99
101
  store.set(model.editAtom, (tx) => tx.setCode("c1", "local"));
@@ -113,6 +115,7 @@ describe("document synchronization", () => {
113
115
  const model = createDocumentModel({
114
116
  store,
115
117
  documentAtom: source,
118
+ shownDocumentAtom: source,
116
119
  backendAtom: atom(fakeBackend({ edit: vi.fn<FakeEdit>() })),
117
120
  });
118
121
  const reason = new Error("Session ended");
@@ -124,9 +127,11 @@ describe("document synchronization", () => {
124
127
 
125
128
  it("starts again after a subscriber aborts during startup", async () => {
126
129
  const store = createStore();
130
+ const source = atom(document());
127
131
  const model = createDocumentModel({
128
132
  store,
129
- documentAtom: atom(document()),
133
+ documentAtom: source,
134
+ shownDocumentAtom: source,
130
135
  backendAtom: atom(null),
131
136
  });
132
137
  const controller = new AbortController();
@@ -14,6 +14,7 @@ function createSession(initial: Document, controller: AbortController) {
14
14
  const model = createDocumentModel({
15
15
  store,
16
16
  documentAtom: notebook.documentAtom,
17
+ shownDocumentAtom: notebook.shownDocumentAtom,
17
18
  backendAtom: notebook.backendAtom,
18
19
  });
19
20
  model.start({ signal: controller.signal });
@@ -1,6 +1,7 @@
1
1
  /* Copyright 2026 Marimo. All rights reserved. */
2
2
 
3
3
  import { vi } from "vitest";
4
+ import { document } from "./fixtures";
4
5
  import { test } from "./scenario-fixture";
5
6
 
6
7
  test("waits for a pause in typing before sending the latest edit", async ({
@@ -134,6 +135,66 @@ test.for(["before reset", "during reset", "after snapshot"] as const)(
134
135
  },
135
136
  );
136
137
 
138
+ test("holds edits made during a reset until the replacement arrives", async ({
139
+ expect,
140
+ session: { user, host, notebook },
141
+ }) => {
142
+ host.reset();
143
+ user.changeCode("c3", "typed during reset");
144
+ expect(user.cell("c3")?.code).toBe("typed during reset");
145
+ await vi.advanceTimersByTimeAsync(2000);
146
+ expect(notebook.edit).not.toHaveBeenCalled();
147
+ expect(user.status()).toEqual({ status: "unavailable" });
148
+ await expect(user.sync()).rejects.toThrow("unavailable");
149
+
150
+ host.changeCell("c1", { code: "unrelated edit" });
151
+ host.deliverSnapshot();
152
+ const synced = user.sync();
153
+ const request = await host.nextRequest();
154
+ request.process();
155
+ request.deliverResponse();
156
+ host.deliverDocument();
157
+ await expect(synced).resolves.toEqual(host.document().revision);
158
+ expect(host.document().cells.get("c3")?.code).toBe("typed during reset");
159
+ expect(user.cell("c1")?.code).toBe("unrelated edit");
160
+ });
161
+
162
+ test("does not submit a draft for the notebook a reset replaced", async ({
163
+ expect,
164
+ session: { user, host, notebook },
165
+ }) => {
166
+ host.reset();
167
+ user.changeCode("c3", "for the earlier notebook");
168
+ host.deliverSnapshotOf(document("n2"));
169
+ await vi.advanceTimersByTimeAsync(2000);
170
+ expect(notebook.edit).not.toHaveBeenCalled();
171
+ expect(user.cell("c3")).toBeUndefined();
172
+ expect(user.cell("c1")?.code).toBe("x = 1");
173
+ expect(user.status()).toEqual({ status: "idle" });
174
+ });
175
+
176
+ test("confirms a receipt that arrives during a reset only against the replacement", async ({
177
+ expect,
178
+ session: { user, host },
179
+ }) => {
180
+ user.changeCode("c3", "sent");
181
+ const flushed = user.sync();
182
+ const finished = vi.fn();
183
+ void flushed.then(finished, finished);
184
+ const request = await host.nextRequest();
185
+ request.process();
186
+ // What stays on show already includes the receipt's revision.
187
+ host.deliverDocument();
188
+ host.reset();
189
+ request.deliverResponse();
190
+ await vi.advanceTimersByTimeAsync(0);
191
+ expect(finished).not.toHaveBeenCalled();
192
+
193
+ host.deliverSnapshot();
194
+ await expect(flushed).resolves.toEqual(host.document().revision);
195
+ expect(user.status()).toEqual({ status: "idle" });
196
+ });
197
+
137
198
  test.for(["response", "document"] as const)(
138
199
  "does not resurrect a cancelled creation when the %s arrives first",
139
200
  async (first, { expect, session: { user, host } }) => {
@@ -24,6 +24,7 @@ function setup(initial: Document | null = document()) {
24
24
  const model = createDocumentState({
25
25
  store,
26
26
  source,
27
+ shown: source,
27
28
  signal: controller.signal,
28
29
  });
29
30
  return {
@@ -20,6 +20,7 @@ function setup() {
20
20
  const model = createDocumentState({
21
21
  store,
22
22
  source,
23
+ shown: source,
23
24
  signal: controller.signal,
24
25
  });
25
26
  return {
@@ -23,6 +23,12 @@ export function createTestNotebook(
23
23
  ) {
24
24
  const host = createTestHost(initial);
25
25
  const source = atom<Document | null>(initial);
26
+ // Like the replica, a reset empties the source but keeps what is shown.
27
+ const shown = atom<Document | null>(initial);
28
+ function deliver(document: Document) {
29
+ store.set(source, document);
30
+ store.set(shown, document);
31
+ }
26
32
  const requests: PendingRequest[] = [];
27
33
  const waiting: Deferred<PendingRequest>[] = [];
28
34
  const documents: Document[] = [];
@@ -82,6 +88,7 @@ export function createTestNotebook(
82
88
  });
83
89
  const notebook = {
84
90
  documentAtom: source,
91
+ shownDocumentAtom: shown,
85
92
  backendAtom: atom(fakeBackend({ edit })),
86
93
  edit,
87
94
  };
@@ -135,7 +142,7 @@ export function createTestNotebook(
135
142
  next !== undefined,
136
143
  "No document update is waiting for delivery",
137
144
  );
138
- store.set(source, next);
145
+ deliver(next);
139
146
  },
140
147
  reset() {
141
148
  documents.length = 0;
@@ -143,7 +150,12 @@ export function createTestNotebook(
143
150
  },
144
151
  deliverSnapshot() {
145
152
  documents.length = 0;
146
- store.set(source, host.document());
153
+ deliver(host.document());
154
+ },
155
+ /** Complete a reset with another notebook, as session brokering can. */
156
+ deliverSnapshotOf(document: Document) {
157
+ documents.length = 0;
158
+ deliver(document);
147
159
  },
148
160
  },
149
161
  };
@@ -22,7 +22,7 @@ export type { DocumentSyncStatus } from "./sync";
22
22
 
23
23
  /** The editable document and its synchronization with the host. */
24
24
  export interface DocumentModel {
25
- /** Confirmed host state with local drafts applied, null while unavailable or stopped. */
25
+ /** Shown host state with local drafts applied, null while unavailable or stopped. */
26
26
  readonly documentAtom: Atom<EditableDocument | null>;
27
27
  /** Ignored while stopped. */
28
28
  readonly editAtom: WritableAtom<null, [edit: DocumentEdit], void>;
@@ -47,11 +47,14 @@ interface Running {
47
47
  export function createDocumentModel({
48
48
  store,
49
49
  documentAtom: source,
50
+ shownDocumentAtom: shown,
50
51
  backendAtom,
51
52
  }: {
52
53
  store: ReturnType<typeof createStore>;
53
- /** The document the host last confirmed. */
54
+ /** The document the host last confirmed, which edits submit against. */
54
55
  documentAtom: Atom<Document | null>;
56
+ /** The document on show, which outlasts `documentAtom` through a reset. */
57
+ shownDocumentAtom: Atom<Document | null>;
55
58
  /** The backend that confirmed it, whose commands reach its host. */
56
59
  backendAtom: Atom<NotebookBackend | null>;
57
60
  }): DocumentModel {
@@ -83,7 +86,7 @@ export function createDocumentModel({
83
86
  if (store.get(runningAtom)) {
84
87
  throw new Error("The document model is already started");
85
88
  }
86
- const state = createDocumentState({ store, source, signal });
89
+ const state = createDocumentState({ store, source, shown, signal });
87
90
  const sync = createDocumentSync({
88
91
  store,
89
92
  documentAtom: source,
@@ -56,7 +56,10 @@ interface PendingSubmission {
56
56
  }
57
57
 
58
58
  interface ModelState {
59
+ /** The host's document on the current stream, null through a reset. */
59
60
  readonly confirmed: Document | null;
61
+ /** What drafts show over while `confirmed` is null, such as through a reset. */
62
+ readonly shown: Document | null;
60
63
  readonly drafts: ReadonlyMap<Protocol.NotebookId, Draft>;
61
64
  readonly pending: PendingSubmission | null;
62
65
  }
@@ -65,10 +68,12 @@ interface ModelState {
65
68
  * Local edits over confirmed host state, with one submission in flight.
66
69
  *
67
70
  * A reset retains the submission so its receipt can reconcile a new snapshot.
68
- * Switching notebook identity invalidates it, retaining each notebook's draft.
71
+ * Until then drafts show and edit over the document the reset replaced, but
72
+ * nothing submits or settles against it. Switching notebook identity
73
+ * invalidates the submission, retaining each notebook's draft.
69
74
  */
70
75
  export interface DocumentState {
71
- /** Confirmed host state with local drafts applied, null while unavailable. */
76
+ /** Shown host state with local drafts applied, null while unavailable. */
72
77
  readonly documentAtom: Atom<EditableDocument | null>;
73
78
  /** Null when the document or source needed by a put is unavailable. */
74
79
  readonly changesAtom: Atom<readonly Protocol.Change[] | null>;
@@ -92,18 +97,25 @@ export interface DocumentState {
92
97
  >;
93
98
  }
94
99
 
100
+ /** The document drafts apply over. */
101
+ function baseOf(state: ModelState): Document | null {
102
+ return state.confirmed ?? state.shown;
103
+ }
104
+
95
105
  function draftOf(state: ModelState): Draft {
96
- return state.confirmed === null
106
+ const base = baseOf(state);
107
+ return base === null
97
108
  ? EMPTY_DRAFT
98
- : (state.drafts.get(state.confirmed.revision.notebook_id) ?? EMPTY_DRAFT);
109
+ : (state.drafts.get(base.revision.notebook_id) ?? EMPTY_DRAFT);
99
110
  }
100
111
 
101
112
  function withDraft(state: ModelState, draft: Draft): ModelState {
102
- if (state.confirmed === null || draft === draftOf(state)) {
113
+ const base = baseOf(state);
114
+ if (base === null || draft === draftOf(state)) {
103
115
  return state;
104
116
  }
105
117
  const drafts = new Map(state.drafts);
106
- const notebookId = state.confirmed.revision.notebook_id;
118
+ const notebookId = base.revision.notebook_id;
107
119
  if (isEmptyDraft(draft)) {
108
120
  drafts.delete(notebookId);
109
121
  } else {
@@ -134,23 +146,30 @@ function reconcile(state: ModelState): ModelState {
134
146
  export function createDocumentState({
135
147
  store,
136
148
  source,
149
+ shown: shownSource,
137
150
  signal,
138
151
  }: {
139
152
  store: ReturnType<typeof createStore>;
153
+ /** The document edits submit against. */
140
154
  source: Atom<Document | null>;
155
+ /** The document on show, which outlasts `source` through a reset. */
156
+ shown: Atom<Document | null>;
141
157
  signal: AbortSignal;
142
158
  }): DocumentState {
143
159
  signal.throwIfAborted();
160
+ const sourcesAtom = atom((get) => ({
161
+ confirmed: get(source),
162
+ shown: get(shownSource),
163
+ }));
144
164
  const stateAtom = atom<ModelState>({
145
- confirmed: store.get(source),
165
+ ...store.get(sourcesAtom),
146
166
  drafts: new Map(),
147
167
  pending: null,
148
168
  });
149
169
  const documentAtom = atom((get): EditableDocument | null => {
150
170
  const state = get(stateAtom);
151
- return state.confirmed === null
152
- ? null
153
- : viewDocument(state.confirmed, draftOf(state));
171
+ const base = baseOf(state);
172
+ return base === null ? null : viewDocument(base, draftOf(state));
154
173
  });
155
174
  const changesAtom = atom((get): readonly Protocol.Change[] | null => {
156
175
  const state = get(stateAtom);
@@ -160,12 +179,13 @@ export function createDocumentState({
160
179
  });
161
180
  const pendingAtom = atom((get) => get(stateAtom).pending?.submission ?? null);
162
181
 
163
- const unsubscribe = store.sub(source, () => {
164
- const confirmed = store.get(source);
182
+ const unsubscribe = store.sub(sourcesAtom, () => {
183
+ const { confirmed, shown } = store.get(sourcesAtom);
165
184
  store.set(stateAtom, (state) =>
166
185
  reconcile({
167
186
  ...state,
168
187
  confirmed,
188
+ shown,
169
189
  pending:
170
190
  confirmed !== null &&
171
191
  state.pending !== null &&
@@ -182,13 +202,14 @@ export function createDocumentState({
182
202
  throw new Error("Document transactions cannot be nested");
183
203
  }
184
204
  const state = get(stateAtom);
185
- if (signal.aborted || state.confirmed === null) {
205
+ const base = baseOf(state);
206
+ if (signal.aborted || base === null) {
186
207
  return;
187
208
  }
188
209
  editing = true;
189
210
  try {
190
211
  const draft = editDocument(
191
- state.confirmed,
212
+ base,
192
213
  draftOf(state),
193
214
  edit,
194
215
  state.pending?.draft,
@@ -196,7 +217,8 @@ export function createDocumentState({
196
217
  if (
197
218
  signal.aborted ||
198
219
  get(stateAtom) !== state ||
199
- get(source) !== state.confirmed
220
+ get(source) !== state.confirmed ||
221
+ get(shownSource) !== state.shown
200
222
  ) {
201
223
  throw new Error("The document changed during the transaction");
202
224
  }
@@ -271,6 +293,7 @@ export function createDocumentState({
271
293
  signal.removeEventListener("abort", stop);
272
294
  store.set(stateAtom, {
273
295
  confirmed: null,
296
+ shown: null,
274
297
  drafts: new Map(),
275
298
  pending: null,
276
299
  });
@@ -92,11 +92,14 @@ describe("document resource", () => {
92
92
  }
93
93
  const partial = replay([cell, removed], state);
94
94
  expect(current(partial)).toBe(current(state));
95
- const completed = replay(
96
- [published, { event: "ready", data: {}, id: "113" }],
97
- partial,
98
- );
95
+ const resumed = reducer.resume(partial);
96
+ const completed = reducer.reduce(resumed, published);
99
97
  expect(current(completed)?.revision).toEqual(published.data.document);
98
+ expect(
99
+ current(
100
+ reducer.reduce(completed, { event: "ready", data: {}, id: "113" }),
101
+ ),
102
+ ).toEqual(current(completed));
100
103
  });
101
104
 
102
105
  it("removes a notebook without waiting for individual cell removals", () => {
@@ -119,8 +122,10 @@ describe("document resource", () => {
119
122
  }),
120
123
  ).toThrow("Document records do not match");
121
124
  const partial = reducer.reduce(state, edits[0]);
122
- expect(() =>
123
- reducer.reduce(partial, { event: "ready", data: {}, id: "200" }),
124
- ).toThrow("incomplete document update");
125
+ for (const pending of [partial, reducer.resume(partial)]) {
126
+ expect(() =>
127
+ reducer.reduce(pending, { event: "ready", data: {}, id: "200" }),
128
+ ).toThrow("incomplete document update");
129
+ }
125
130
  });
126
131
  });