@marimo-team/frontend 0.25.3-dev91 → 0.25.3-dev94
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/dist/assets/{CellStatus-DYRvLOiP.js → CellStatus-CplYFNTy.js} +1 -1
- package/dist/assets/{ConnectedDataExplorerComponent-D9UK8o9A.js → ConnectedDataExplorerComponent-Dft8R6l5.js} +1 -1
- package/dist/assets/{ErrorBoundary-BKQo68Df.js → ErrorBoundary-CdGuz4a_.js} +2 -2
- package/dist/assets/{JsonOutput-DryOywaN.js → JsonOutput-Csbov8Fx.js} +3 -3
- package/dist/assets/{MarimoErrorOutput-XrTcOYTI.js → MarimoErrorOutput-czclLlOn.js} +1 -1
- package/dist/assets/{RenderHTML-DaVbCAsb.js → RenderHTML-CNDedpS4.js} +1 -1
- package/dist/assets/{RunButton-D3ScLvwb.js → RunButton-CRfdOxVd.js} +1 -1
- package/dist/assets/__vite-browser-external-8vDMR0Kk.js +1 -0
- package/dist/assets/__vite-browser-external-mKcnhO20.js +1 -0
- package/dist/assets/{add-cell-with-ai-BKk0pvon.js → add-cell-with-ai-BAIX7r0P.js} +1 -1
- package/dist/assets/{add-connection-dialog-CNIwGJVL.js → add-connection-dialog-DrDuM-tI.js} +1 -1
- package/dist/assets/{agent-panel-6t16-QG0.js → agent-panel-CoXq_rlA.js} +1 -1
- package/dist/assets/{ai-model-dropdown-prnCk5hq.js → ai-model-dropdown-COvWn46z.js} +2 -2
- package/dist/assets/{app-config-button-O_yI5nzB.js → app-config-button-CtxoYG4F.js} +1 -1
- package/dist/assets/{blob-FjxwDE1i.js → blob-DCjrjZKW.js} +1 -1
- package/dist/assets/{cache-panel-Byolv2ht.js → cache-panel-CbWZcBu0.js} +1 -1
- package/dist/assets/{cell-editor-CM_kQ_VW.js → cell-editor-BnL0Sorl.js} +1 -1
- package/dist/assets/{cell-link-D8EbRFJJ.js → cell-link-BFbgKiRH.js} +1 -1
- package/dist/assets/{cells-DfF1DLV_.js → cells-yOs72eZm.js} +2 -2
- package/dist/assets/{chat-display-BtQI9LeB.js → chat-display-DwRM41t_.js} +1 -1
- package/dist/assets/{chat-panel-tuUN0wet.js → chat-panel-DeTEdjxF.js} +1 -1
- package/dist/assets/{chat-ui-Bb-HxAv4.js → chat-ui-D6XucD07.js} +1 -1
- package/dist/assets/{code-visibility-ny5n3-GH.js → code-visibility-DYDagVy7.js} +1 -1
- package/dist/assets/{column-preview-CqFSXTtf.js → column-preview-DXM8SqGY.js} +1 -1
- package/dist/assets/{command-palette-CkDj29px.js → command-palette-CA6yfNLb.js} +1 -1
- package/dist/assets/{common-D2oGA09G.js → common-B-ZYnO1x.js} +1 -1
- package/dist/assets/{components-CJRBUI2s.js → components-CBE8cOLK.js} +1 -1
- package/dist/assets/{components-DrU8dOzy.js → components-DCo7mAWO.js} +1 -1
- package/dist/assets/{components-1zsrzJ_K.js → components-vu2jLG08.js} +1 -1
- package/dist/assets/{config-BWKfkUWA.js → config-CD1IuZFJ.js} +1 -1
- package/dist/assets/{config-CP8Y8_R0.js → config-DMz7oN0r.js} +1 -1
- package/dist/assets/{config-DGX7JYor.js → config-DpY86_Yq.js} +1 -1
- package/dist/assets/{createReducer-HKXUbqyz.js → createReducer-CaI_Oa0v.js} +1 -1
- package/dist/assets/{datasource-rV_-mqbc.js → datasource-BakEOWsf.js} +1 -1
- package/dist/assets/{dependency-graph-panel-By-cv0Nw.js → dependency-graph-panel-DCGB3_id.js} +1 -1
- package/dist/assets/{documentation-panel-DHMjvOPt.js → documentation-panel-CpGZ2Dfn.js} +1 -1
- package/dist/assets/{download-CMTzX3rG.js → download-CV1k4XQ4.js} +1 -1
- package/dist/assets/{edit-page-Bgkov1Jy.js → edit-page-By7n2M84.js} +5 -5
- package/dist/assets/{error-entries-BGCjRkVy.js → error-entries-JgrZYMbe.js} +1 -1
- package/dist/assets/{error-panel-BDTFHLEt.js → error-panel-DS7HWph-.js} +1 -1
- package/dist/assets/feedback-button-Dgs_HOOC.js +1 -0
- package/dist/assets/{feedback-button-C-ScLxY2.js → feedback-button-eYsTCK-g.js} +1 -1
- package/dist/assets/{file-explorer-panel-B9jBSwhj.js → file-explorer-panel-BTLwwwD4.js} +1 -1
- package/dist/assets/{file-name-input-DIvrcC82.js → file-name-input-BaR4h2Ud.js} +1 -1
- package/dist/assets/{floating-outline-DJnZ45VA.js → floating-outline-Drd73Quo.js} +1 -1
- package/dist/assets/{gallery-page-CHJD1mdU.js → gallery-page-DXUD11Rm.js} +1 -1
- package/dist/assets/{glide-data-editor-BsE9Em-a.js → glide-data-editor-DqBBoQBl.js} +1 -1
- package/dist/assets/{home-page-CCbdig5p.js → home-page-DrRto2-v.js} +1 -1
- package/dist/assets/{hooks-yjSoxKTw.js → hooks-D25kP4Sr.js} +1 -1
- package/dist/assets/{index-0pnwTVch.js → index-vBuAUWEe.js} +6 -6
- package/dist/assets/{input-BZdRe7pb.js → input-DmHMEZGh.js} +1 -1
- package/dist/assets/kiosk-mode-DFS6HopF.js +1 -0
- package/dist/assets/{layout-B2MJFBFp.js → layout-DLHIGZTK.js} +3 -3
- package/dist/assets/links-DX2w97N9.js +1 -0
- package/dist/assets/{logs-panel-B34vxRV7.js → logs-panel-D5AAD2s9.js} +1 -1
- package/dist/assets/{markdown-renderer-CkN_xAjW.js → markdown-renderer--CwVm4kJ.js} +1 -1
- package/dist/assets/{mermaid-B6mqMm5_.js → mermaid-BKXlWk4F.js} +1 -1
- package/dist/assets/{name-cell-input-eyYneuGK.js → name-cell-input-BFKZaNI5.js} +1 -1
- package/dist/assets/{outline-panel-rpjpMDYX.js → outline-panel-qcwRnl0R.js} +1 -1
- package/dist/assets/{package-data-dv4FBHxR.js → package-data-CKPmBDtg.js} +1 -1
- package/dist/assets/{packages-panel-gXdFgCro.js → packages-panel-DnSryNcz.js} +1 -1
- package/dist/assets/{panel-accordion-state-CImIsdc8.js → panel-accordion-state-BwJpDMVt.js} +1 -1
- package/dist/assets/{panels-DI8gAi31.js → panels-Aqzwb3UR.js} +1 -1
- package/dist/assets/{process-output-BClx23Jv.js → process-output-DuUfHYSr.js} +1 -1
- package/dist/assets/{readonly-code-display-JC_YsLL_.js → readonly-code-display-BY-9I_Yk.js} +1 -1
- package/dist/assets/{readonly-python-code-DeBED4OB.js → readonly-python-code-DSyr-yE2.js} +1 -1
- package/dist/assets/{renderShortcut-H60uiHBp.js → renderShortcut-rUwGqKgC.js} +1 -1
- package/dist/assets/{requests-lt0JK8Jt.js → requests-BIFR-h4M.js} +1 -1
- package/dist/assets/requests-DuLJN9Tf.js +1 -0
- package/dist/assets/{reveal-component-BoGgZrdo.js → reveal-component-C_Lb7Mom.js} +1 -1
- package/dist/assets/{run-page-DnvqkxCy.js → run-page-ZTw-kCU-.js} +1 -1
- package/dist/assets/{runs-6lJxKV1w.js → runs-C49hleBy.js} +1 -1
- package/dist/assets/{runtime-details-state-7JHj3efu.js → runtime-details-state-B7gpnuXx.js} +1 -1
- package/dist/assets/{save-worker-BbUSwZRG.js → save-worker-pCkSHhuz.js} +1 -1
- package/dist/assets/{scratchpad-panel-DZDmNIPw.js → scratchpad-panel-D2IHkKoG.js} +1 -1
- package/dist/assets/{secrets-panel-Dh3v7XPE.js → secrets-panel-bcfEKxRx.js} +1 -1
- package/dist/assets/{session-B8Y-GJnS.js → session-CMLIzZKw.js} +1 -1
- package/dist/assets/session-panel-cOSO3WCX.js +1 -0
- package/dist/assets/{snippets-panel-BNjtDbnR.js → snippets-panel-BNadUJLU.js} +1 -1
- package/dist/assets/{state-BY4_P6ko.js → state-3mcyTS9q.js} +1 -1
- package/dist/assets/{state-C0irhlOZ.js → state-B_MCKzBL.js} +1 -1
- package/dist/assets/state-BaYudep3.js +3 -0
- package/dist/assets/{state-DaWgt-rn.js → state-C5WAeuTu.js} +1 -1
- package/dist/assets/{state-Ck7FMD5A.js → state-Cv2JW3l9.js} +1 -1
- package/dist/assets/{state-DhaPGGBO.js → state-_1LTGJQM.js} +1 -1
- package/dist/assets/{switch-IoWE8PbW.js → switch-DYjBfpNJ.js} +1 -1
- package/dist/assets/{terminal-BwLC9HS5.js → terminal-Ca-kubKb.js} +1 -1
- package/dist/assets/{tracing-Ch4LR62-.js → tracing-DbTgS4u9.js} +1 -1
- package/dist/assets/{tracing-panel-B8fX4PhX.js → tracing-panel-CjlrpXPy.js} +2 -2
- package/dist/assets/{tree-actions-Zg3Ycse4.js → tree-actions-C_KYlaSv.js} +1 -1
- package/dist/assets/{useAddCell-BX9lq_0J.js → useAddCell-C0J8quGE.js} +1 -1
- package/dist/assets/{useAsyncData-DhhdOGGd.js → useAsyncData-qvoPYHtx.js} +1 -1
- package/dist/assets/{useBoolean-WABMLM83.js → useBoolean-Dd2nWXFJ.js} +1 -1
- package/dist/assets/{useCellActionButton-D7Ruyes-.js → useCellActionButton-CbkOj6BE.js} +1 -1
- package/dist/assets/{useDebounce-k9QTpQ9-.js → useDebounce-DwTQ_1Pu.js} +1 -1
- package/dist/assets/{useDeepCompareMemoize-DFIVN9dn.js → useDeepCompareMemoize-B2DVK2De.js} +1 -1
- package/dist/assets/{useDeleteCell-AbkQI6-c.js → useDeleteCell-DHkF5wgS.js} +1 -1
- package/dist/assets/{useDependencyPanelTab-B0b2KkvS.js → useDependencyPanelTab--yXJW6X5.js} +1 -1
- package/dist/assets/{useEvent-Dfcbgjhg.js → useEvent-BDjgaBtz.js} +1 -1
- package/dist/assets/{useInstallPackage-CWcPxSLP.js → useInstallPackage-wcDq9hYT.js} +1 -1
- package/dist/assets/{useNotebookActions-DJuAUAGI.js → useNotebookActions-dJQalORg.js} +1 -1
- package/dist/assets/{useRunCells-CV81rge-.js → useRunCells-C_IwVJR_.js} +1 -1
- package/dist/assets/{useSplitCell-BTwCB3JS.js → useSplitCell-Ci4MEUj8.js} +1 -1
- package/dist/assets/{useTheme-DLtPc-39.js → useTheme-BAA27lh7.js} +1 -1
- package/dist/assets/{vega-component-CY6HmM06.js → vega-component-BeyxAJad.js} +1 -1
- package/dist/assets/{worker-DTQ7oH2A.js → worker-bSCjCvwz.js} +1 -1
- package/dist/assets/{write-secret-modal-7DzehGyr.js → write-secret-modal-BEx-EUlh.js} +1 -1
- package/dist/index.html +40 -40
- package/package.json +1 -1
- package/src/components/chat/acp/__tests__/state.test.ts +2 -2
- package/src/components/data-table/cell-selection/feature.ts +2 -2
- package/src/components/data-table/cell-selection/types.ts +2 -2
- package/src/components/data-table/column-summary/column-summary.tsx +2 -4
- package/src/components/datasources/__tests__/table-list.test.tsx +118 -0
- package/src/components/datasources/datasources.tsx +35 -21
- package/src/components/editor/SortableCell.tsx +1 -3
- package/src/components/editor/chrome/wrapper/footer-items/runtime-settings.tsx +1 -7
- package/src/components/editor/chrome/wrapper/storage.ts +1 -1
- package/src/components/editor/output/MarimoTracebackOutput.tsx +1 -1
- package/src/components/icons/spinner.tsx +1 -1
- package/src/components/ui/alert-dialog.tsx +1 -1
- package/src/core/ai/context/providers/datasource.ts +6 -2
- package/src/core/cells/session.ts +1 -1
- package/src/core/codemirror/__tests__/setup.test.ts +1 -1
- package/src/core/codemirror/keymaps/vim.ts +2 -6
- package/src/core/codemirror/language/languages/markdown.ts +1 -1
- package/src/core/codemirror/language/languages/python.ts +3 -3
- package/src/core/document/__tests__/draft.test.ts +322 -0
- package/src/core/document/__tests__/fixtures.ts +92 -0
- package/src/core/document/__tests__/model.test.ts +434 -0
- package/src/core/document/__tests__/submission.test.ts +334 -0
- package/src/core/document/draft.ts +472 -0
- package/src/core/document/model.ts +395 -0
- package/src/core/kernel/__tests__/RuntimeState.test.ts +1 -1
- package/src/core/state/__tests__/jotai.test.tsx +108 -0
- package/src/core/state/jotai.ts +1 -1
- package/src/core/wasm/bridge.ts +2 -2
- package/src/core/wasm/store.ts +3 -3
- package/src/core/wasm/worker/bootstrap.ts +1 -1
- package/src/core/wasm/worker/getMarimoWheel.ts +1 -1
- package/src/plugins/impl/CodeEditorPlugin.tsx +1 -1
- package/src/plugins/impl/RangeSliderPlugin.tsx +0 -1
- package/src/plugins/impl/data-explorer/ConnectedDataExplorerComponent.tsx +1 -1
- package/src/plugins/impl/data-explorer/state/reducer.ts +1 -1
- package/src/plugins/impl/data-frames/DataFramePlugin.tsx +0 -1
- package/src/plugins/impl/panel/PanelPlugin.tsx +1 -1
- package/src/plugins/impl/vega/make-selectable.ts +1 -1
- package/src/plugins/layout/RoutesPlugin.tsx +1 -1
- package/src/stories/form-wrapper.stories.tsx +2 -2
- package/src/stories/select.stories.tsx +2 -2
- package/src/stories/switchable-multi-select.stories.tsx +1 -1
- package/src/utils/__tests__/batch-requests.test.ts +3 -3
- package/src/utils/blob.ts +1 -1
- package/src/utils/zod-utils.ts +1 -3
- package/dist/assets/__vite-browser-external-CYO_P3S8.js +0 -1
- package/dist/assets/__vite-browser-external-FeDVGZU8.js +0 -1
- package/dist/assets/feedback-button-CLRAP9DE.js +0 -1
- package/dist/assets/kiosk-mode-BGmMSc7A.js +0 -1
- package/dist/assets/links-CXcpNIM0.js +0 -1
- package/dist/assets/requests-CgSY1X4a.js +0 -1
- package/dist/assets/session-panel-B1QB5LN7.js +0 -1
- package/dist/assets/state-DUP4ToPd.js +0 -3
- package/src/core/state/__tests__/jotai.test.ts +0 -25
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { dequal } from "dequal";
|
|
4
|
+
import {
|
|
5
|
+
atom,
|
|
6
|
+
type Atom,
|
|
7
|
+
type createStore,
|
|
8
|
+
type WritableAtom,
|
|
9
|
+
} from "jotai/vanilla";
|
|
10
|
+
import { selectAtom } from "jotai/vanilla/utils";
|
|
11
|
+
import type { Document } from "../host/resources/document";
|
|
12
|
+
import * as Protocol from "@marimo-team/client/protocol";
|
|
13
|
+
import {
|
|
14
|
+
acknowledge,
|
|
15
|
+
type CellEdit,
|
|
16
|
+
createCell,
|
|
17
|
+
deleteCell,
|
|
18
|
+
type Draft,
|
|
19
|
+
type EditableCell,
|
|
20
|
+
type EditableDocument,
|
|
21
|
+
editCell,
|
|
22
|
+
EMPTY_DRAFT,
|
|
23
|
+
isEmptyDraft,
|
|
24
|
+
type Placement,
|
|
25
|
+
prune,
|
|
26
|
+
sameCell,
|
|
27
|
+
setOrder,
|
|
28
|
+
toChanges,
|
|
29
|
+
viewDocument,
|
|
30
|
+
} from "./draft";
|
|
31
|
+
|
|
32
|
+
/** Operations on a working draft, valid only during the edit callback. */
|
|
33
|
+
export interface DocumentEditor {
|
|
34
|
+
setCode(id: Protocol.CellId, code: string): void;
|
|
35
|
+
setName(id: Protocol.CellId, name: string): void;
|
|
36
|
+
updateConfig(id: Protocol.CellId, patch: Partial<Protocol.CellConfig>): void;
|
|
37
|
+
createCell(cell: Protocol.CellInput, placement?: Placement): void;
|
|
38
|
+
deleteCell(id: Protocol.CellId): void;
|
|
39
|
+
/** A permutation of the cells present at this point in the transaction. */
|
|
40
|
+
setOrder(order: readonly Protocol.CellId[]): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Synchronous; a thrown error discards the entire transaction. */
|
|
44
|
+
export type DocumentEdit = (editor: DocumentEditor) => void;
|
|
45
|
+
|
|
46
|
+
/** A captured edit request. Return this same object when reporting its result. */
|
|
47
|
+
export interface DocumentSubmission {
|
|
48
|
+
readonly notebookId: Protocol.NotebookId;
|
|
49
|
+
readonly changes: readonly Protocol.Change[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type ResponseRevision = Pick<Protocol.Document, "notebook_id" | "version">;
|
|
53
|
+
|
|
54
|
+
interface PendingSubmission {
|
|
55
|
+
readonly submission: DocumentSubmission;
|
|
56
|
+
readonly draft: Draft;
|
|
57
|
+
readonly responseRevision: ResponseRevision | null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface ModelState {
|
|
61
|
+
readonly confirmed: Document | null;
|
|
62
|
+
readonly drafts: ReadonlyMap<Protocol.NotebookId, Draft>;
|
|
63
|
+
readonly pending: PendingSubmission | null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Local edits over confirmed host state, with one submission in flight.
|
|
68
|
+
*
|
|
69
|
+
* A reset retains the submission so its receipt can reconcile a new snapshot.
|
|
70
|
+
* Switching notebook identity invalidates it, retaining each notebook's draft.
|
|
71
|
+
*/
|
|
72
|
+
export interface DocumentModel {
|
|
73
|
+
/** Hold onto the selector; its value is stable until the cell changes. */
|
|
74
|
+
cellAtom(id: Protocol.CellId): Atom<EditableCell | undefined>;
|
|
75
|
+
readonly orderAtom: Atom<readonly Protocol.CellId[] | null>;
|
|
76
|
+
readonly appConfigAtom: Atom<Readonly<
|
|
77
|
+
Protocol.Document["app_config"]
|
|
78
|
+
> | null>;
|
|
79
|
+
/** Null when the document or source needed by a put is unavailable. */
|
|
80
|
+
readonly changesAtom: Atom<readonly Protocol.Change[] | null>;
|
|
81
|
+
/** Remains pending until both the response and its streamed revision arrive. */
|
|
82
|
+
readonly pendingAtom: Atom<DocumentSubmission | null>;
|
|
83
|
+
readonly editAtom: WritableAtom<null, [edit: DocumentEdit], void>;
|
|
84
|
+
/** Capture a request, or null when unavailable, unchanged, or already sending. */
|
|
85
|
+
readonly submitAtom: WritableAtom<null, [], DocumentSubmission | null>;
|
|
86
|
+
/**
|
|
87
|
+
* Report the response revision, or null for a rejected request.
|
|
88
|
+
*
|
|
89
|
+
* Success waits for a complete streamed document at that version or later.
|
|
90
|
+
* Duplicate replies and replies for another notebook are ignored. Retry an
|
|
91
|
+
* uncertain request with the same body and idempotency key; do not reject it
|
|
92
|
+
* or capture a new request until its outcome is known.
|
|
93
|
+
*/
|
|
94
|
+
readonly settleAtom: WritableAtom<
|
|
95
|
+
null,
|
|
96
|
+
[submission: DocumentSubmission, responseRevision: ResponseRevision | null],
|
|
97
|
+
void
|
|
98
|
+
>;
|
|
99
|
+
/** Stop observing the source and invalidate pending work. Safe to repeat. */
|
|
100
|
+
dispose(): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function draftOf(state: ModelState): Draft {
|
|
104
|
+
return state.confirmed === null
|
|
105
|
+
? EMPTY_DRAFT
|
|
106
|
+
: (state.drafts.get(state.confirmed.revision.notebook_id) ?? EMPTY_DRAFT);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function withDraft(state: ModelState, draft: Draft): ModelState {
|
|
110
|
+
if (state.confirmed === null || draft === draftOf(state)) {
|
|
111
|
+
return state;
|
|
112
|
+
}
|
|
113
|
+
const drafts = new Map(state.drafts);
|
|
114
|
+
const notebookId = state.confirmed.revision.notebook_id;
|
|
115
|
+
if (isEmptyDraft(draft)) {
|
|
116
|
+
drafts.delete(notebookId);
|
|
117
|
+
} else {
|
|
118
|
+
drafts.set(notebookId, draft);
|
|
119
|
+
}
|
|
120
|
+
return { ...state, drafts };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Publish the confirmed document and any released fields together. */
|
|
124
|
+
function reconcile(state: ModelState): ModelState {
|
|
125
|
+
const { confirmed, pending } = state;
|
|
126
|
+
if (confirmed === null) {
|
|
127
|
+
return state;
|
|
128
|
+
}
|
|
129
|
+
let draft = draftOf(state);
|
|
130
|
+
if (
|
|
131
|
+
pending !== null &&
|
|
132
|
+
pending.responseRevision !== null &&
|
|
133
|
+
confirmed.revision.version >= pending.responseRevision.version
|
|
134
|
+
) {
|
|
135
|
+
draft = acknowledge(draft, pending.draft);
|
|
136
|
+
state = { ...state, pending: null };
|
|
137
|
+
}
|
|
138
|
+
return withDraft(state, prune(confirmed, draft, state.pending?.draft));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Create once per source and store; the owner must dispose the model. */
|
|
142
|
+
export function createDocumentModel(
|
|
143
|
+
store: ReturnType<typeof createStore>,
|
|
144
|
+
source: Atom<Document | null>,
|
|
145
|
+
): DocumentModel {
|
|
146
|
+
const stateAtom = atom<ModelState>({
|
|
147
|
+
confirmed: store.get(source),
|
|
148
|
+
drafts: new Map(),
|
|
149
|
+
pending: null,
|
|
150
|
+
});
|
|
151
|
+
let disposed = false;
|
|
152
|
+
const documentAtom = atom((get): EditableDocument | null => {
|
|
153
|
+
const state = get(stateAtom);
|
|
154
|
+
return state.confirmed === null
|
|
155
|
+
? null
|
|
156
|
+
: viewDocument(state.confirmed, draftOf(state));
|
|
157
|
+
});
|
|
158
|
+
const cellAtom = (id: Protocol.CellId) =>
|
|
159
|
+
selectAtom(
|
|
160
|
+
documentAtom,
|
|
161
|
+
(document) => document?.cells.get(id),
|
|
162
|
+
(a, b) =>
|
|
163
|
+
a === b || (a !== undefined && b !== undefined && sameCell(a, b)),
|
|
164
|
+
);
|
|
165
|
+
const orderAtom = selectAtom(
|
|
166
|
+
documentAtom,
|
|
167
|
+
(document) => document?.order ?? null,
|
|
168
|
+
dequal,
|
|
169
|
+
);
|
|
170
|
+
const appConfigAtom = selectAtom(
|
|
171
|
+
stateAtom,
|
|
172
|
+
(state) => state.confirmed?.revision.app_config ?? null,
|
|
173
|
+
dequal,
|
|
174
|
+
);
|
|
175
|
+
const changesAtom = atom((get): readonly Protocol.Change[] | null => {
|
|
176
|
+
const state = get(stateAtom);
|
|
177
|
+
return state.confirmed === null
|
|
178
|
+
? null
|
|
179
|
+
: toChanges(state.confirmed, draftOf(state));
|
|
180
|
+
});
|
|
181
|
+
const pendingAtom = atom((get) => get(stateAtom).pending?.submission ?? null);
|
|
182
|
+
|
|
183
|
+
const unsubscribe = store.sub(source, () => {
|
|
184
|
+
const confirmed = store.get(source);
|
|
185
|
+
store.set(stateAtom, (state) =>
|
|
186
|
+
reconcile({
|
|
187
|
+
...state,
|
|
188
|
+
confirmed,
|
|
189
|
+
pending:
|
|
190
|
+
confirmed !== null &&
|
|
191
|
+
state.pending !== null &&
|
|
192
|
+
confirmed.revision.notebook_id !== state.pending.submission.notebookId
|
|
193
|
+
? null
|
|
194
|
+
: state.pending,
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
let editing = false;
|
|
200
|
+
const editAtom = atom(null, (get, set, edit: DocumentEdit) => {
|
|
201
|
+
if (editing) {
|
|
202
|
+
throw new Error("Document transactions cannot be nested");
|
|
203
|
+
}
|
|
204
|
+
const state = get(stateAtom);
|
|
205
|
+
if (disposed || state.confirmed === null) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
editing = true;
|
|
209
|
+
try {
|
|
210
|
+
const draft = editDocument(
|
|
211
|
+
state.confirmed,
|
|
212
|
+
draftOf(state),
|
|
213
|
+
edit,
|
|
214
|
+
state.pending?.draft,
|
|
215
|
+
);
|
|
216
|
+
if (
|
|
217
|
+
disposed ||
|
|
218
|
+
get(stateAtom) !== state ||
|
|
219
|
+
get(source) !== state.confirmed
|
|
220
|
+
) {
|
|
221
|
+
throw new Error("The document changed during the transaction");
|
|
222
|
+
}
|
|
223
|
+
set(stateAtom, withDraft(state, draft));
|
|
224
|
+
} finally {
|
|
225
|
+
editing = false;
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const submitAtom = atom(null, (get, set): DocumentSubmission | null => {
|
|
230
|
+
const state = get(stateAtom);
|
|
231
|
+
if (disposed || state.confirmed === null || state.pending !== null) {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
const draft = draftOf(state);
|
|
235
|
+
const changes = toChanges(state.confirmed, draft);
|
|
236
|
+
if (changes === null) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
if (changes.length === 0) {
|
|
240
|
+
set(stateAtom, withDraft(state, EMPTY_DRAFT));
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
const submission = {
|
|
244
|
+
notebookId: state.confirmed.revision.notebook_id,
|
|
245
|
+
changes,
|
|
246
|
+
};
|
|
247
|
+
set(stateAtom, {
|
|
248
|
+
...state,
|
|
249
|
+
pending: { submission, draft, responseRevision: null },
|
|
250
|
+
});
|
|
251
|
+
return submission;
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
const settleAtom = atom(
|
|
255
|
+
null,
|
|
256
|
+
(
|
|
257
|
+
get,
|
|
258
|
+
set,
|
|
259
|
+
submission: DocumentSubmission,
|
|
260
|
+
responseRevision: ResponseRevision | null,
|
|
261
|
+
) => {
|
|
262
|
+
const state = get(stateAtom);
|
|
263
|
+
if (
|
|
264
|
+
disposed ||
|
|
265
|
+
state.pending?.submission !== submission ||
|
|
266
|
+
state.pending.responseRevision !== null
|
|
267
|
+
) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (
|
|
271
|
+
responseRevision !== null &&
|
|
272
|
+
responseRevision.notebook_id !== submission.notebookId
|
|
273
|
+
) {
|
|
274
|
+
throw new Error("Edit response belongs to another notebook");
|
|
275
|
+
}
|
|
276
|
+
set(
|
|
277
|
+
stateAtom,
|
|
278
|
+
reconcile({
|
|
279
|
+
...state,
|
|
280
|
+
pending:
|
|
281
|
+
responseRevision === null
|
|
282
|
+
? null
|
|
283
|
+
: { ...state.pending, responseRevision },
|
|
284
|
+
}),
|
|
285
|
+
);
|
|
286
|
+
},
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
return {
|
|
290
|
+
cellAtom,
|
|
291
|
+
orderAtom,
|
|
292
|
+
appConfigAtom,
|
|
293
|
+
changesAtom,
|
|
294
|
+
pendingAtom,
|
|
295
|
+
editAtom,
|
|
296
|
+
submitAtom,
|
|
297
|
+
settleAtom,
|
|
298
|
+
dispose() {
|
|
299
|
+
if (!disposed) {
|
|
300
|
+
disposed = true;
|
|
301
|
+
unsubscribe();
|
|
302
|
+
store.set(stateAtom, {
|
|
303
|
+
confirmed: null,
|
|
304
|
+
drafts: new Map(),
|
|
305
|
+
pending: null,
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function editDocument(
|
|
313
|
+
confirmed: Document,
|
|
314
|
+
draft: Draft,
|
|
315
|
+
edit: DocumentEdit,
|
|
316
|
+
sent: Draft = EMPTY_DRAFT,
|
|
317
|
+
): Draft {
|
|
318
|
+
let next = draft;
|
|
319
|
+
let active = true;
|
|
320
|
+
|
|
321
|
+
function assertActive() {
|
|
322
|
+
if (!active) {
|
|
323
|
+
throw new Error("The document transaction has ended");
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function hasCell(id: Protocol.CellId): boolean {
|
|
328
|
+
const local = next.cells.get(id);
|
|
329
|
+
return (
|
|
330
|
+
local?.kind === "create" ||
|
|
331
|
+
(local?.kind !== "delete" && confirmed.cells.has(id))
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function requireCell(id: Protocol.CellId) {
|
|
336
|
+
assertActive();
|
|
337
|
+
if (!hasCell(id)) {
|
|
338
|
+
throw new Error(`Cell ${id} does not exist`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function updateCell(id: Protocol.CellId, patch: CellEdit) {
|
|
343
|
+
requireCell(id);
|
|
344
|
+
next = editCell(confirmed, next, id, patch);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const editor: DocumentEditor = {
|
|
348
|
+
setCode: (id, code) => updateCell(id, { code }),
|
|
349
|
+
setName: (id, name) => updateCell(id, { name }),
|
|
350
|
+
updateConfig: (id, patch) =>
|
|
351
|
+
updateCell(id, { config: Protocol.CellConfig.partial().parse(patch) }),
|
|
352
|
+
createCell: (cell, placement = {}) => {
|
|
353
|
+
assertActive();
|
|
354
|
+
if (confirmed.cells.has(cell.id) || hasCell(cell.id)) {
|
|
355
|
+
throw new Error(`Cell ${cell.id} already exists`);
|
|
356
|
+
}
|
|
357
|
+
if (placement.after !== undefined && placement.before !== undefined) {
|
|
358
|
+
throw new Error("Specify either before or after for cell placement");
|
|
359
|
+
}
|
|
360
|
+
const anchor = placement.after ?? placement.before;
|
|
361
|
+
if (anchor !== undefined) {
|
|
362
|
+
requireCell(anchor);
|
|
363
|
+
}
|
|
364
|
+
next = createCell(next, cell, { ...placement });
|
|
365
|
+
},
|
|
366
|
+
deleteCell: (id) => {
|
|
367
|
+
requireCell(id);
|
|
368
|
+
next = deleteCell(next, id, sent);
|
|
369
|
+
},
|
|
370
|
+
setOrder: (order) => {
|
|
371
|
+
assertActive();
|
|
372
|
+
const { cells } = viewDocument(confirmed, next);
|
|
373
|
+
if (
|
|
374
|
+
order.length !== cells.size ||
|
|
375
|
+
new Set(order).size !== cells.size ||
|
|
376
|
+
order.some((id) => !cells.has(id))
|
|
377
|
+
) {
|
|
378
|
+
throw new Error("Cell order must contain every cell exactly once");
|
|
379
|
+
}
|
|
380
|
+
next = setOrder(next, order);
|
|
381
|
+
},
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
try {
|
|
385
|
+
const result: unknown = edit(editor);
|
|
386
|
+
if (result instanceof Promise) {
|
|
387
|
+
// Observe a rejected async callback without publishing its partial draft.
|
|
388
|
+
result.catch(() => undefined);
|
|
389
|
+
throw new Error("Document edit callbacks must be synchronous");
|
|
390
|
+
}
|
|
391
|
+
return next;
|
|
392
|
+
} finally {
|
|
393
|
+
active = false;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
@@ -25,7 +25,7 @@ describe("RuntimeState", () => {
|
|
|
25
25
|
|
|
26
26
|
beforeEach(() => {
|
|
27
27
|
vi.clearAllMocks();
|
|
28
|
-
mockSendComponentValues = vi.fn((
|
|
28
|
+
mockSendComponentValues = vi.fn(() => Promise.resolve(null));
|
|
29
29
|
uiElementRegistry = UIElementRegistry.INSTANCE;
|
|
30
30
|
uiElementRegistry.entries.clear();
|
|
31
31
|
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { act, renderHook } from "@testing-library/react";
|
|
4
|
+
import { atom, Provider } from "jotai";
|
|
5
|
+
import type React from "react";
|
|
6
|
+
import { describe, expect, it, vi } from "vitest";
|
|
7
|
+
import { store, useJotaiEffect, waitFor } from "../jotai";
|
|
8
|
+
|
|
9
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
10
|
+
<Provider store={store}>{children}</Provider>
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
describe("waitFor function", () => {
|
|
14
|
+
it("should resolve when the atom satisfies the predicate", async () => {
|
|
15
|
+
const testAtom = atom(0);
|
|
16
|
+
store.set(testAtom, 10);
|
|
17
|
+
|
|
18
|
+
const result = await waitFor(testAtom, (value) => value === 10);
|
|
19
|
+
expect(result).toBe(10);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should resolve when the atom changes to satisfy the predicate", async () => {
|
|
23
|
+
const testAtom = atom(0);
|
|
24
|
+
|
|
25
|
+
setTimeout(() => store.set(testAtom, 15), 5);
|
|
26
|
+
setTimeout(() => store.set(testAtom, 20), 10);
|
|
27
|
+
|
|
28
|
+
const result = await waitFor(testAtom, (value) => value === 20);
|
|
29
|
+
expect(result).toBe(20);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("useJotaiEffect", () => {
|
|
34
|
+
it("calls the effect with the new and previous values", () => {
|
|
35
|
+
const testAtom = atom(0);
|
|
36
|
+
const effect = vi.fn();
|
|
37
|
+
renderHook(() => useJotaiEffect(testAtom, effect), { wrapper });
|
|
38
|
+
|
|
39
|
+
act(() => store.set(testAtom, 1));
|
|
40
|
+
act(() => store.set(testAtom, 2));
|
|
41
|
+
|
|
42
|
+
expect(effect.mock.calls).toEqual([
|
|
43
|
+
[1, 0],
|
|
44
|
+
[2, 1],
|
|
45
|
+
]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("calls only the latest callback after a rerender", () => {
|
|
49
|
+
const testAtom = atom(0);
|
|
50
|
+
const first = vi.fn();
|
|
51
|
+
const second = vi.fn();
|
|
52
|
+
const { rerender } = renderHook(
|
|
53
|
+
({ effect }) => useJotaiEffect(testAtom, effect),
|
|
54
|
+
{ initialProps: { effect: first }, wrapper },
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
rerender({ effect: second });
|
|
58
|
+
act(() => store.set(testAtom, 1));
|
|
59
|
+
|
|
60
|
+
expect(first).not.toHaveBeenCalled();
|
|
61
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("subscribes once per rerender with an inline callback", () => {
|
|
65
|
+
const testAtom = atom(0);
|
|
66
|
+
const calls: number[] = [];
|
|
67
|
+
const { rerender } = renderHook(
|
|
68
|
+
() => useJotaiEffect(testAtom, (value) => calls.push(value)),
|
|
69
|
+
{ wrapper },
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
rerender();
|
|
73
|
+
rerender();
|
|
74
|
+
act(() => store.set(testAtom, 1));
|
|
75
|
+
|
|
76
|
+
expect(calls).toEqual([1]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("stops listening to the previous atom when the atom changes", () => {
|
|
80
|
+
const firstAtom = atom(0);
|
|
81
|
+
const secondAtom = atom(0);
|
|
82
|
+
const effect = vi.fn();
|
|
83
|
+
const { rerender } = renderHook(
|
|
84
|
+
({ target }) => useJotaiEffect(target, effect),
|
|
85
|
+
{ initialProps: { target: firstAtom }, wrapper },
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
rerender({ target: secondAtom });
|
|
89
|
+
act(() => store.set(firstAtom, 1));
|
|
90
|
+
expect(effect).not.toHaveBeenCalled();
|
|
91
|
+
|
|
92
|
+
act(() => store.set(secondAtom, 1));
|
|
93
|
+
expect(effect).toHaveBeenCalledTimes(1);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("unsubscribes on unmount", () => {
|
|
97
|
+
const testAtom = atom(0);
|
|
98
|
+
const effect = vi.fn();
|
|
99
|
+
const { unmount } = renderHook(() => useJotaiEffect(testAtom, effect), {
|
|
100
|
+
wrapper,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
unmount();
|
|
104
|
+
act(() => store.set(testAtom, 1));
|
|
105
|
+
|
|
106
|
+
expect(effect).not.toHaveBeenCalled();
|
|
107
|
+
});
|
|
108
|
+
});
|
package/src/core/state/jotai.ts
CHANGED
|
@@ -42,7 +42,7 @@ export function useJotaiEffect<T>(
|
|
|
42
42
|
const store = useStore();
|
|
43
43
|
useEffect(() => {
|
|
44
44
|
let prevValue = store.get(atom);
|
|
45
|
-
store.sub(atom, () => {
|
|
45
|
+
return store.sub(atom, () => {
|
|
46
46
|
const value = store.get(atom);
|
|
47
47
|
effect(value, prevValue);
|
|
48
48
|
prevValue = value;
|
package/src/core/wasm/bridge.ts
CHANGED
|
@@ -407,7 +407,7 @@ export class PyodideBridge implements RunRequests, EditRequests {
|
|
|
407
407
|
return response as Snippets;
|
|
408
408
|
};
|
|
409
409
|
|
|
410
|
-
public openFile: EditRequests["openFile"] = async (
|
|
410
|
+
public openFile: EditRequests["openFile"] = async () => {
|
|
411
411
|
const url = createShareableLink({
|
|
412
412
|
code: null,
|
|
413
413
|
baseUrl: window.location.origin,
|
|
@@ -451,7 +451,7 @@ export class PyodideBridge implements RunRequests, EditRequests {
|
|
|
451
451
|
return null;
|
|
452
452
|
};
|
|
453
453
|
|
|
454
|
-
public sendInstantiate: RunRequests["sendInstantiate"] = async (
|
|
454
|
+
public sendInstantiate: RunRequests["sendInstantiate"] = async () => {
|
|
455
455
|
return null;
|
|
456
456
|
};
|
|
457
457
|
|
package/src/core/wasm/store.ts
CHANGED
|
@@ -41,7 +41,7 @@ const urlFileStore: FileStore = {
|
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
export const domElementFileStore: FileStore = {
|
|
44
|
-
saveFile(
|
|
44
|
+
saveFile(_contents: string) {
|
|
45
45
|
// Do nothing
|
|
46
46
|
},
|
|
47
47
|
readFile() {
|
|
@@ -54,7 +54,7 @@ export const domElementFileStore: FileStore = {
|
|
|
54
54
|
};
|
|
55
55
|
|
|
56
56
|
const remoteDefaultFileStore: FileStore = {
|
|
57
|
-
saveFile(
|
|
57
|
+
saveFile(_contents: string) {
|
|
58
58
|
// Do nothing
|
|
59
59
|
},
|
|
60
60
|
readFile() {
|
|
@@ -79,7 +79,7 @@ export const mountConfigFileStore: FileStore = {
|
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
const emptyFileStore: FileStore = {
|
|
82
|
-
saveFile(
|
|
82
|
+
saveFile(_contents: string) {
|
|
83
83
|
// Do nothing
|
|
84
84
|
},
|
|
85
85
|
readFile() {
|
|
@@ -71,7 +71,7 @@ const CodeEditorComponent = (props: CodeEditorComponentProps) => {
|
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
const handleChange = useEvent((newValue: string) => {
|
|
74
|
-
setLocalValue(
|
|
74
|
+
setLocalValue(newValue);
|
|
75
75
|
if (typeof props.debounce === "number") {
|
|
76
76
|
setValueDebounced(newValue);
|
|
77
77
|
} else if (!props.debounce) {
|
|
@@ -47,7 +47,7 @@ const ConnectedDataExplorerComponent = (props: DataTableProps): JSX.Element => {
|
|
|
47
47
|
// Subscribe to the store
|
|
48
48
|
const unsub = store.sub(chartSpecAtom, () => {
|
|
49
49
|
const value = store.get(chartSpecAtom);
|
|
50
|
-
const { schema, ...withoutSchema } = value;
|
|
50
|
+
const { schema: _schema, ...withoutSchema } = value;
|
|
51
51
|
props.setValue(withoutSchema);
|
|
52
52
|
});
|
|
53
53
|
|
|
@@ -47,7 +47,7 @@ const { valueAtom: chartSpecAtom, useActions } = createReducerAndAtoms(
|
|
|
47
47
|
},
|
|
48
48
|
set: (state, next: ChartSpec) => {
|
|
49
49
|
// remove schema
|
|
50
|
-
const { schema, ...rest } = next;
|
|
50
|
+
const { schema: _schema, ...rest } = next;
|
|
51
51
|
return { ...state, ...rest };
|
|
52
52
|
},
|
|
53
53
|
},
|
|
@@ -78,7 +78,7 @@ type T = Record<string, unknown>;
|
|
|
78
78
|
|
|
79
79
|
// oxlint-disable-next-line typescript/consistent-type-definitions
|
|
80
80
|
type PluginFunctions = {
|
|
81
|
-
send_to_widget:
|
|
81
|
+
send_to_widget: (req: {
|
|
82
82
|
message?: unknown;
|
|
83
83
|
buffers?: unknown;
|
|
84
84
|
}) => Promise<null | undefined>;
|
|
@@ -56,7 +56,7 @@ const RoutesComponent = ({
|
|
|
56
56
|
|
|
57
57
|
useEffect(() => {
|
|
58
58
|
// Listen for route changes
|
|
59
|
-
const listener = (
|
|
59
|
+
const listener = () => {
|
|
60
60
|
handleFindMatch(window.location);
|
|
61
61
|
};
|
|
62
62
|
window.addEventListener("hashchange", listener);
|
|
@@ -20,7 +20,7 @@ type Story = StoryObj<typeof FormWrapper>;
|
|
|
20
20
|
const props: FormWrapperProps<string> = {
|
|
21
21
|
currentValue: "currentValue",
|
|
22
22
|
newValue: "currentValue",
|
|
23
|
-
setValue: (
|
|
23
|
+
setValue: () => {
|
|
24
24
|
toast({
|
|
25
25
|
title: "Form submitted",
|
|
26
26
|
});
|
|
@@ -133,7 +133,7 @@ export const Validate: Story = {
|
|
|
133
133
|
<FormWrapper
|
|
134
134
|
{...props}
|
|
135
135
|
shouldValidate={true}
|
|
136
|
-
validate={async (
|
|
136
|
+
validate={async () => {
|
|
137
137
|
const random = Math.random();
|
|
138
138
|
if (random < 0.8) {
|
|
139
139
|
return `Failed to validate. Random number was ${random}`;
|