@marimo-team/islands 0.26.1-dev104 → 0.26.1-dev105
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/{ConnectedDataExplorerComponent-BJBdnd0O.js → ConnectedDataExplorerComponent-C6dY5fiI.js} +1 -1
- package/dist/{ErrorBoundary-DLR6xE2E.js → ErrorBoundary-Bn664FZa.js} +1 -1
- package/dist/{chat-ui-Bs3rgm2m.js → chat-ui-zGgiq5zN.js} +3 -3
- package/dist/{code-visibility-BhWW3jkv.js → code-visibility-CMgSFUs7.js} +2 -2
- package/dist/{common-i4mVHMGl.js → common-DWnR77Qk.js} +5 -5
- package/dist/{createReducer-BUQhgLOI.js → createReducer-m7yAXmDu.js} +16 -19
- package/dist/{data-grid-overlay-editor-7durYJXa.js → data-grid-overlay-editor-zS1h3iMI.js} +1 -1
- package/dist/{error-entries-BcAyFIeh.js → error-entries-DVclFjaE.js} +23 -26
- package/dist/{feedback-button-CeuzSBrq.js → feedback-button-BMGqp8Ff.js} +3 -3
- package/dist/{glide-data-editor-BLGr2vQF.js → glide-data-editor-Bra6h4KB.js} +3 -3
- package/dist/{globals-DorvG5dC.js → globals-_bXqC0R8.js} +1 -1
- package/dist/{html-to-image-BmLTjaGw.js → html-to-image-Cst4BlIm.js} +5685 -5678
- package/dist/main.js +36 -36
- package/dist/{number-overlay-editor-DQAdvZrn.js → number-overlay-editor-XLUhOVa7.js} +1 -1
- package/dist/{process-output-CcNpl5Qu.js → process-output-DizFa5IE.js} +2 -2
- package/dist/{reveal-component-B6xeeht1.js → reveal-component-llpC324_.js} +2 -2
- package/package.json +1 -1
- package/src/components/editor/cell/__tests__/cell-actions.test.tsx +74 -0
- package/src/components/editor/cell/cell-action-body.tsx +28 -0
- package/src/components/editor/cell/cell-actions.tsx +60 -72
- package/src/components/editor/cell/cell-context-menu.tsx +36 -62
- package/src/components/editor/navigation/navigation.ts +1 -1
- package/src/core/cells/__tests__/use-cell-actions.test.tsx +48 -0
- package/src/core/cells/actions.ts +68 -89
- package/src/core/cells/cells.ts +2 -0
- package/src/utils/__tests__/createReducer.test.tsx +4 -3
- package/src/utils/createReducer.ts +19 -12
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
import React, { Fragment } from "react";
|
|
12
12
|
import { Slot } from "radix-ui";
|
|
13
13
|
import useEvent from "react-use-event-hook";
|
|
14
|
-
import { renderMinimalShortcut } from "@/components/shortcuts/renderShortcut";
|
|
15
14
|
import {
|
|
16
15
|
ContextMenu,
|
|
17
16
|
ContextMenuContent,
|
|
@@ -20,7 +19,6 @@ import {
|
|
|
20
19
|
ContextMenuTrigger,
|
|
21
20
|
} from "@/components/ui/context-menu";
|
|
22
21
|
import { menuItemVariants } from "@/components/ui/menu-items";
|
|
23
|
-
import { Tooltip } from "@/components/ui/tooltip";
|
|
24
22
|
import { toast } from "@/components/ui/use-toast";
|
|
25
23
|
import { useRestoreFocus } from "@/components/ui/use-restore-focus";
|
|
26
24
|
import { useCellData, useCellRuntime } from "@/core/cells/cells";
|
|
@@ -37,6 +35,7 @@ import {
|
|
|
37
35
|
type CellActionButtonProps,
|
|
38
36
|
useCellActionButtons,
|
|
39
37
|
} from "../actions/useCellActionButton";
|
|
38
|
+
import { CellActionBody } from "./cell-action-body";
|
|
40
39
|
|
|
41
40
|
interface Props extends Pick<
|
|
42
41
|
CellActionButtonProps,
|
|
@@ -328,67 +327,42 @@ const CellMenuContent = ({
|
|
|
328
327
|
}}
|
|
329
328
|
>
|
|
330
329
|
{visibleActions.flatMap((group, i) => [
|
|
331
|
-
...group.map((action) =>
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
330
|
+
...group.map((action) => (
|
|
331
|
+
<Fragment key={action.label}>
|
|
332
|
+
{
|
|
333
|
+
// Set disableClick items such as cell name input
|
|
334
|
+
// to div to prevent roving focus
|
|
335
|
+
action.disableClick ? (
|
|
336
|
+
<div
|
|
337
|
+
className={menuItemVariants({
|
|
338
|
+
className: action.disabled ? "opacity-50!" : "",
|
|
339
|
+
variant: action.variant,
|
|
340
|
+
})}
|
|
341
|
+
onKeyDown={(evt) => {
|
|
342
|
+
evt.stopPropagation();
|
|
343
|
+
}}
|
|
344
|
+
// Prevent keydown propagation, that focus does not jump to shortcut which start with same letter
|
|
345
|
+
// e.g. input "C", then focus jump to "Copy"
|
|
346
|
+
>
|
|
347
|
+
<CellActionBody action={action} />
|
|
337
348
|
</div>
|
|
338
|
-
)
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
return (
|
|
356
|
-
<Fragment key={action.label}>
|
|
357
|
-
{
|
|
358
|
-
// Set disableClick items such as cell name input
|
|
359
|
-
// to div to prevent roving focus
|
|
360
|
-
action.disableClick ? (
|
|
361
|
-
<div
|
|
362
|
-
className={menuItemVariants({
|
|
363
|
-
className: action.disabled ? "opacity-50!" : "",
|
|
364
|
-
variant: action.variant,
|
|
365
|
-
})}
|
|
366
|
-
onKeyDown={(evt) => {
|
|
367
|
-
evt.stopPropagation();
|
|
368
|
-
}}
|
|
369
|
-
// Prevent keydown propagation, that focus does not jump to shortcut which start with same letter
|
|
370
|
-
// e.g. input "C", then focus jump to "Copy"
|
|
371
|
-
>
|
|
372
|
-
{body}
|
|
373
|
-
</div>
|
|
374
|
-
) : (
|
|
375
|
-
<ContextMenuItem
|
|
376
|
-
className={action.disabled ? "opacity-50!" : ""}
|
|
377
|
-
onSelect={(evt) => {
|
|
378
|
-
if (action.disableClick || action.disabled) {
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
|
-
pendingAction.current = () => action.handle(evt);
|
|
382
|
-
}}
|
|
383
|
-
variant={action.variant}
|
|
384
|
-
>
|
|
385
|
-
{body}
|
|
386
|
-
</ContextMenuItem>
|
|
387
|
-
)
|
|
388
|
-
}
|
|
389
|
-
</Fragment>
|
|
390
|
-
);
|
|
391
|
-
}),
|
|
349
|
+
) : (
|
|
350
|
+
<ContextMenuItem
|
|
351
|
+
className={action.disabled ? "opacity-50!" : ""}
|
|
352
|
+
onSelect={(evt) => {
|
|
353
|
+
if (action.disableClick || action.disabled) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
pendingAction.current = () => action.handle(evt);
|
|
357
|
+
}}
|
|
358
|
+
variant={action.variant}
|
|
359
|
+
>
|
|
360
|
+
<CellActionBody action={action} />
|
|
361
|
+
</ContextMenuItem>
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
</Fragment>
|
|
365
|
+
)),
|
|
392
366
|
i < visibleActions.length - 1 && (
|
|
393
367
|
<ContextMenuSeparator key={`separator-${group[0].label}`} />
|
|
394
368
|
),
|
|
@@ -205,7 +205,7 @@ export function useCellNavigationProps(
|
|
|
205
205
|
const edit = useNotebookDocumentEdit();
|
|
206
206
|
const setAiCompletionCell = useSetAtom(aiCompletionCellAtom);
|
|
207
207
|
const actions = useCellActions();
|
|
208
|
-
const { createCell, moveToNextCell } =
|
|
208
|
+
const { createCell, moveToNextCell } = actions;
|
|
209
209
|
const store = useStore();
|
|
210
210
|
const showHiddenCode = useSetAtom(showHiddenCodeAtom);
|
|
211
211
|
const runCells = useRunCells();
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
import { fireEvent, render, renderHook } from "@testing-library/react";
|
|
3
|
+
import { createStore, Provider } from "jotai";
|
|
4
|
+
import type { PropsWithChildren } from "react";
|
|
5
|
+
import { afterEach, expect, it } from "vitest";
|
|
6
|
+
import { cellId } from "@/__tests__/branded";
|
|
7
|
+
import { useCellActions } from "../actions";
|
|
8
|
+
import { useReducerActions } from "../cells";
|
|
9
|
+
import { setupNotebook } from "./harness";
|
|
10
|
+
|
|
11
|
+
const stops: Array<() => void> = [];
|
|
12
|
+
afterEach(() => stops.splice(0).forEach((stop) => stop()));
|
|
13
|
+
|
|
14
|
+
it("shares cell actions across consumers of the same store", () => {
|
|
15
|
+
const store = createStore();
|
|
16
|
+
const wrapper = ({ children }: PropsWithChildren) => (
|
|
17
|
+
<Provider store={store}>{children}</Provider>
|
|
18
|
+
);
|
|
19
|
+
const first = renderHook(useCellActions, { wrapper });
|
|
20
|
+
const second = renderHook(useCellActions, { wrapper });
|
|
21
|
+
const session = renderHook(useReducerActions, { wrapper });
|
|
22
|
+
expect(second.result.current).toBe(first.result.current);
|
|
23
|
+
expect(first.result.current.clearLogs).toBe(session.result.current.clearLogs);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("writes to the current notebook when the Provider switches stores", () => {
|
|
27
|
+
const first = setupNotebook(stops);
|
|
28
|
+
const second = setupNotebook(stops);
|
|
29
|
+
const id = cellId("new-cell");
|
|
30
|
+
function CreateCellButton() {
|
|
31
|
+
const { createCell } = useCellActions();
|
|
32
|
+
return (
|
|
33
|
+
<button type="button" onClick={() => createCell({ id, focus: false })}>
|
|
34
|
+
Add cell
|
|
35
|
+
</button>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
const buttonIn = (store: typeof first.store) => (
|
|
39
|
+
<Provider store={store}>
|
|
40
|
+
<CreateCellButton />
|
|
41
|
+
</Provider>
|
|
42
|
+
);
|
|
43
|
+
const view = render(buttonIn(first.store));
|
|
44
|
+
view.rerender(buttonIn(second.store));
|
|
45
|
+
fireEvent.click(view.getByRole("button", { name: "Add cell" }));
|
|
46
|
+
expect(second.order()).toContain(id);
|
|
47
|
+
expect(first.order()).not.toContain(id);
|
|
48
|
+
});
|
|
@@ -3,8 +3,13 @@
|
|
|
3
3
|
/** Notebook operations that edit the document and then adjust the editor. */
|
|
4
4
|
|
|
5
5
|
import { historyField } from "@codemirror/commands";
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
atom,
|
|
8
|
+
type Getter,
|
|
9
|
+
type Setter,
|
|
10
|
+
useStore,
|
|
11
|
+
type WritableAtom,
|
|
12
|
+
} from "jotai";
|
|
8
13
|
import { scrollAndHighlightCell } from "@/components/editor/links/cell-link";
|
|
9
14
|
import {
|
|
10
15
|
cellExecutionInputsAtom,
|
|
@@ -20,7 +25,7 @@ import { splitEditor } from "../codemirror/language/utils";
|
|
|
20
25
|
import type { SerializedEditorState } from "../codemirror/types";
|
|
21
26
|
import { canCollapseOutline } from "../dom/outline";
|
|
22
27
|
import type { CellConfig } from "../network/types";
|
|
23
|
-
import { store } from "../state/jotai";
|
|
28
|
+
import { type JotaiStore, store } from "../state/jotai";
|
|
24
29
|
import {
|
|
25
30
|
addColumnAtom,
|
|
26
31
|
addColumnBreakpointAtom,
|
|
@@ -39,10 +44,10 @@ import {
|
|
|
39
44
|
} from "./arrange";
|
|
40
45
|
import {
|
|
41
46
|
type DeletedCell,
|
|
47
|
+
getNotebookActionsForStore,
|
|
42
48
|
notebookAtom,
|
|
43
49
|
notebookReducer,
|
|
44
50
|
releaseCellAtoms,
|
|
45
|
-
useReducerActions,
|
|
46
51
|
} from "./cells";
|
|
47
52
|
import { resolveColumns } from "./columns";
|
|
48
53
|
import { consoleExecution } from "./execution-console";
|
|
@@ -646,98 +651,72 @@ export const undoDeleteCellAtom = atom(null, (get, set): void => {
|
|
|
646
651
|
}));
|
|
647
652
|
});
|
|
648
653
|
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
// notebook through `useReducerActions`; components get the operations.
|
|
654
|
+
function bindCellActions(store: JotaiStore) {
|
|
655
|
+
const bind =
|
|
656
|
+
<Value, Args extends unknown[], Result>(
|
|
657
|
+
atom: WritableAtom<Value, Args, Result>,
|
|
658
|
+
) =>
|
|
659
|
+
(...args: Args) =>
|
|
660
|
+
store.set(atom, ...args);
|
|
661
|
+
|
|
658
662
|
const {
|
|
659
663
|
addLogs: _addLogs,
|
|
660
664
|
reconcileDocument: _reconcileDocument,
|
|
661
665
|
resetCellRuntimes: _resetCellRuntimes,
|
|
662
666
|
...operations
|
|
663
|
-
} =
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
()
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
splitCell,
|
|
702
|
-
undoSplitCell,
|
|
703
|
-
addColumn,
|
|
704
|
-
addColumnBreakpoint,
|
|
705
|
-
deleteColumn,
|
|
706
|
-
mergeAllColumns,
|
|
707
|
-
compactColumns,
|
|
708
|
-
moveColumn,
|
|
709
|
-
moveCell,
|
|
710
|
-
moveCellToIndex,
|
|
711
|
-
moveCellsRelativeTo,
|
|
712
|
-
dropCellOverCell,
|
|
713
|
-
dropCellOverColumn,
|
|
714
|
-
sendToTop,
|
|
715
|
-
sendToBottom,
|
|
716
|
-
createCell,
|
|
717
|
-
moveToNextCell,
|
|
718
|
-
addSetupCell,
|
|
719
|
-
addOrAppendSetupCell,
|
|
720
|
-
deleteCells,
|
|
721
|
-
undoDeleteCell,
|
|
722
|
-
focusCell,
|
|
723
|
-
focusTopCell,
|
|
724
|
-
focusBottomCell,
|
|
725
|
-
collapseCell,
|
|
726
|
-
expandCell,
|
|
727
|
-
collapseAllCells,
|
|
728
|
-
expandAllCells,
|
|
729
|
-
clearCellOutput,
|
|
730
|
-
clearCellConsoleOutput,
|
|
731
|
-
clearAllCellOutputs,
|
|
732
|
-
showCellIfHidden,
|
|
733
|
-
}),
|
|
734
|
-
// The reducer's actions and the atom setters are stable for the store's lifetime.
|
|
735
|
-
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
736
|
-
[],
|
|
737
|
-
);
|
|
667
|
+
} = getNotebookActionsForStore(store);
|
|
668
|
+
|
|
669
|
+
return {
|
|
670
|
+
...operations,
|
|
671
|
+
createCell: bind(createCellAtom),
|
|
672
|
+
moveToNextCell: bind(moveToNextCellAtom),
|
|
673
|
+
addSetupCell: bind(addSetupCellAtom),
|
|
674
|
+
addOrAppendSetupCell: bind(addOrAppendSetupCellAtom),
|
|
675
|
+
deleteCells: bind(deleteCellsAtom),
|
|
676
|
+
undoDeleteCell: bind(undoDeleteCellAtom),
|
|
677
|
+
focusCell: bind(focusCellAtom),
|
|
678
|
+
focusTopCell: bind(focusTopCellAtom),
|
|
679
|
+
focusBottomCell: bind(focusBottomCellAtom),
|
|
680
|
+
collapseCell: bind(collapseCellAtom),
|
|
681
|
+
expandCell: bind(expandCellAtom),
|
|
682
|
+
collapseAllCells: bind(collapseAllCellsAtom),
|
|
683
|
+
expandAllCells: bind(expandAllCellsAtom),
|
|
684
|
+
clearCellOutput: bind(clearCellOutputAtom),
|
|
685
|
+
clearCellConsoleOutput: bind(clearCellConsoleOutputAtom),
|
|
686
|
+
clearAllCellOutputs: bind(clearAllCellOutputsAtom),
|
|
687
|
+
showCellIfHidden: bind(showCellIfHiddenAtom),
|
|
688
|
+
moveCell: bind(moveCellAtom),
|
|
689
|
+
moveCellToIndex: bind(moveCellToIndexAtom),
|
|
690
|
+
moveCellsRelativeTo: bind(moveCellsRelativeToAtom),
|
|
691
|
+
dropCellOverCell: bind(dropCellOverCellAtom),
|
|
692
|
+
dropCellOverColumn: bind(dropCellOverColumnAtom),
|
|
693
|
+
sendToTop: bind(sendToTopAtom),
|
|
694
|
+
sendToBottom: bind(sendToBottomAtom),
|
|
695
|
+
addColumn: bind(addColumnAtom),
|
|
696
|
+
addColumnBreakpoint: bind(addColumnBreakpointAtom),
|
|
697
|
+
deleteColumn: bind(deleteColumnAtom),
|
|
698
|
+
mergeAllColumns: bind(mergeAllColumnsAtom),
|
|
699
|
+
compactColumns: bind(compactColumnsAtom),
|
|
700
|
+
moveColumn: bind(moveColumnAtom),
|
|
701
|
+
setCellCode: bind(setCellCodeAtom),
|
|
702
|
+
splitCell: bind(splitCellAtom),
|
|
703
|
+
undoSplitCell: bind(undoSplitCellAtom),
|
|
704
|
+
};
|
|
738
705
|
}
|
|
739
706
|
|
|
740
|
-
export type CellActions = ReturnType<typeof
|
|
707
|
+
export type CellActions = ReturnType<typeof bindCellActions>;
|
|
708
|
+
const actionsByStore = new WeakMap<JotaiStore, CellActions>();
|
|
709
|
+
|
|
710
|
+
/** Notebook operations shared by components using the same store. */
|
|
711
|
+
export function useCellActions(): CellActions {
|
|
712
|
+
const store = useStore();
|
|
713
|
+
let actions = actionsByStore.get(store);
|
|
714
|
+
if (!actions) {
|
|
715
|
+
actions = bindCellActions(store);
|
|
716
|
+
actionsByStore.set(store, actions);
|
|
717
|
+
}
|
|
718
|
+
return actions;
|
|
719
|
+
}
|
|
741
720
|
|
|
742
721
|
/** Scroll to the first cell that is currently running. */
|
|
743
722
|
export function notebookScrollToRunning() {
|
package/src/core/cells/cells.ts
CHANGED
|
@@ -167,6 +167,7 @@ export function initialNotebookState(): NotebookState {
|
|
|
167
167
|
const {
|
|
168
168
|
reducer,
|
|
169
169
|
createActions,
|
|
170
|
+
getActionsForStore,
|
|
170
171
|
useActions,
|
|
171
172
|
valueAtom: notebookAtom,
|
|
172
173
|
} = createReducerAndAtoms(initialNotebookState, {
|
|
@@ -404,6 +405,7 @@ function updateCellRuntimeState({
|
|
|
404
405
|
|
|
405
406
|
export {
|
|
406
407
|
createActions as createNotebookActions,
|
|
408
|
+
getActionsForStore as getNotebookActionsForStore,
|
|
407
409
|
reducer as notebookReducer,
|
|
408
410
|
notebookAtom,
|
|
409
411
|
};
|
|
@@ -248,7 +248,7 @@ describe("createReducerAndAtoms", () => {
|
|
|
248
248
|
}),
|
|
249
249
|
};
|
|
250
250
|
const seen: number[] = [];
|
|
251
|
-
const { valueAtom, useActions } = createReducerAndAtoms(
|
|
251
|
+
const { valueAtom, useActions, getActionsForStore } = createReducerAndAtoms(
|
|
252
252
|
initialState,
|
|
253
253
|
reducers,
|
|
254
254
|
[(_prev, next) => seen.push(store.get(valueAtom).count, next.count)],
|
|
@@ -260,9 +260,10 @@ describe("createReducerAndAtoms", () => {
|
|
|
260
260
|
const { result } = renderHook(() => useActions(), { wrapper });
|
|
261
261
|
|
|
262
262
|
result.current.increment(5);
|
|
263
|
+
getActionsForStore(store).increment(2);
|
|
263
264
|
|
|
264
|
-
expect(seen).toEqual([5, 5]);
|
|
265
|
-
expect(store.get(valueAtom)).toEqual({ count:
|
|
265
|
+
expect(seen).toEqual([5, 5, 7, 7]);
|
|
266
|
+
expect(store.get(valueAtom)).toEqual({ count: 7 });
|
|
266
267
|
});
|
|
267
268
|
|
|
268
269
|
it("should log an error for non-existent action types", () => {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
|
-
import { atom,
|
|
3
|
+
import { atom, useStore } from "jotai";
|
|
4
4
|
import type { Reducer } from "react";
|
|
5
|
+
import type { JotaiStore } from "@/core/state/jotai";
|
|
5
6
|
import { Logger } from "@/utils/Logger";
|
|
6
7
|
|
|
7
8
|
type ReducerHandlers<State, Payloads> = {
|
|
@@ -93,6 +94,10 @@ type Middleware<State, Payloads> = (
|
|
|
93
94
|
action: ReducerActionOf<Payloads>,
|
|
94
95
|
) => void;
|
|
95
96
|
|
|
97
|
+
interface ReducerActionOptions {
|
|
98
|
+
skipMiddleware?: boolean;
|
|
99
|
+
}
|
|
100
|
+
|
|
96
101
|
export function createReducerAndAtoms<
|
|
97
102
|
State,
|
|
98
103
|
RH extends ReducerConstraints<NoInfer<State>>,
|
|
@@ -144,21 +149,18 @@ export function createReducerAndAtoms<
|
|
|
144
149
|
};
|
|
145
150
|
|
|
146
151
|
const valueAtom = atom(initialState());
|
|
147
|
-
type ActionsCache = WeakMap<
|
|
148
|
-
ReturnType<typeof useSetAtom<typeof valueAtom>>,
|
|
149
|
-
ReducerActions<Payloads>
|
|
150
|
-
>;
|
|
152
|
+
type ActionsCache = WeakMap<JotaiStore, ReducerActions<Payloads>>;
|
|
151
153
|
const actionsMap: ActionsCache = new WeakMap();
|
|
152
154
|
const plainActionsMap: ActionsCache = new WeakMap();
|
|
153
155
|
|
|
154
|
-
function
|
|
155
|
-
|
|
156
|
+
function getActionsForStore(
|
|
157
|
+
store: JotaiStore,
|
|
158
|
+
options?: ReducerActionOptions,
|
|
156
159
|
): ReducerActions<Payloads> {
|
|
157
|
-
const
|
|
158
|
-
const skipMiddleware = options.skipMiddleware === true;
|
|
160
|
+
const skipMiddleware = options?.skipMiddleware === true;
|
|
159
161
|
const cache = skipMiddleware ? plainActionsMap : actionsMap;
|
|
160
162
|
|
|
161
|
-
const cachedActions = cache.get(
|
|
163
|
+
const cachedActions = cache.get(store);
|
|
162
164
|
if (cachedActions) {
|
|
163
165
|
return cachedActions;
|
|
164
166
|
}
|
|
@@ -166,7 +168,7 @@ export function createReducerAndAtoms<
|
|
|
166
168
|
// Middleware runs after the write commits so it observes the new state.
|
|
167
169
|
let prev: State | undefined;
|
|
168
170
|
let next: State | undefined;
|
|
169
|
-
|
|
171
|
+
store.set(valueAtom, (state: State) => {
|
|
170
172
|
prev = state;
|
|
171
173
|
next = reduceSafely(state, action);
|
|
172
174
|
return next;
|
|
@@ -175,14 +177,19 @@ export function createReducerAndAtoms<
|
|
|
175
177
|
runMiddleware(prev, next, action);
|
|
176
178
|
}
|
|
177
179
|
});
|
|
178
|
-
cache.set(
|
|
180
|
+
cache.set(store, actions);
|
|
179
181
|
return actions;
|
|
180
182
|
}
|
|
181
183
|
|
|
184
|
+
function useActions(options?: ReducerActionOptions) {
|
|
185
|
+
return getActionsForStore(useStore(), options);
|
|
186
|
+
}
|
|
187
|
+
|
|
182
188
|
return {
|
|
183
189
|
reducer: reducerWithMiddleware,
|
|
184
190
|
addMiddleware,
|
|
185
191
|
createActions,
|
|
192
|
+
getActionsForStore,
|
|
186
193
|
valueAtom,
|
|
187
194
|
useActions,
|
|
188
195
|
};
|