@marimo-team/islands 0.26.1-dev103 → 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.
Files changed (29) hide show
  1. package/dist/{ConnectedDataExplorerComponent-BJBdnd0O.js → ConnectedDataExplorerComponent-C6dY5fiI.js} +1 -1
  2. package/dist/{ErrorBoundary-B5MVsi73.js → ErrorBoundary-Bn664FZa.js} +1 -1
  3. package/dist/{chat-ui-BbJTFtyC.js → chat-ui-zGgiq5zN.js} +3 -3
  4. package/dist/{code-visibility-DLPZ_eYq.js → code-visibility-CMgSFUs7.js} +2 -2
  5. package/dist/{common-B2K9WgcL.js → common-DWnR77Qk.js} +5 -5
  6. package/dist/{createReducer-BUQhgLOI.js → createReducer-m7yAXmDu.js} +16 -19
  7. package/dist/{data-grid-overlay-editor-1bQ1lRk0.js → data-grid-overlay-editor-zS1h3iMI.js} +1 -1
  8. package/dist/{error-entries-DhZotdzQ.js → error-entries-DVclFjaE.js} +381 -379
  9. package/dist/{feedback-button-DdJjyfKE.js → feedback-button-BMGqp8Ff.js} +3 -3
  10. package/dist/{glide-data-editor-wPYhuqFs.js → glide-data-editor-Bra6h4KB.js} +3 -3
  11. package/dist/{globals-DyJvKfkF.js → globals-_bXqC0R8.js} +1 -1
  12. package/dist/{html-to-image-B8erjaaD.js → html-to-image-Cst4BlIm.js} +5685 -5678
  13. package/dist/main.js +36 -36
  14. package/dist/{number-overlay-editor-BPSNY0UK.js → number-overlay-editor-XLUhOVa7.js} +1 -1
  15. package/dist/{process-output-C-2_O5Ry.js → process-output-DizFa5IE.js} +2 -2
  16. package/dist/{reveal-component-CX1LQkT7.js → reveal-component-llpC324_.js} +2 -2
  17. package/package.json +1 -1
  18. package/src/components/editor/cell/__tests__/cell-actions.test.tsx +74 -0
  19. package/src/components/editor/cell/cell-action-body.tsx +28 -0
  20. package/src/components/editor/cell/cell-actions.tsx +60 -72
  21. package/src/components/editor/cell/cell-context-menu.tsx +36 -62
  22. package/src/components/editor/navigation/navigation.ts +1 -1
  23. package/src/core/cells/__tests__/use-cell-actions.test.tsx +48 -0
  24. package/src/core/cells/actions.ts +68 -89
  25. package/src/core/cells/cells.ts +2 -0
  26. package/src/core/executions/__tests__/select.test.ts +32 -0
  27. package/src/core/executions/select.ts +18 -1
  28. package/src/utils/__tests__/createReducer.test.tsx +4 -3
  29. 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
- let body = (
333
- <div className="flex items-center flex-1">
334
- {action.icon && (
335
- <div className="mr-2 w-5 text-muted-foreground">
336
- {action.icon}
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
- <div className="flex-1">{action.label}</div>
340
- <div className="shrink-0 text-sm">
341
- {action.hotkey && renderMinimalShortcut(action.hotkey)}
342
- {action.rightElement}
343
- </div>
344
- </div>
345
- );
346
-
347
- if (action.tooltip) {
348
- body = (
349
- <Tooltip delayDuration={100} content={action.tooltip}>
350
- {body}
351
- </Tooltip>
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 } = useCellActions();
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 { atom, type Getter, type Setter, useSetAtom } from "jotai";
7
- import { useMemo } from "react";
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
- * Every notebook operation, for components. Stable for the store's lifetime.
651
- *
652
- * The reducer's actions are included while the reducer still has cases.
653
- * Outside React, set the atoms on the store directly.
654
- */
655
- export function useCellActions() {
656
- // The session feeds the kernel's messages and the document into the
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
- } = useReducerActions();
664
- const createCell = useSetAtom(createCellAtom);
665
- const moveToNextCell = useSetAtom(moveToNextCellAtom);
666
- const addSetupCell = useSetAtom(addSetupCellAtom);
667
- const addOrAppendSetupCell = useSetAtom(addOrAppendSetupCellAtom);
668
- const deleteCells = useSetAtom(deleteCellsAtom);
669
- const undoDeleteCell = useSetAtom(undoDeleteCellAtom);
670
- const focusCell = useSetAtom(focusCellAtom);
671
- const focusTopCell = useSetAtom(focusTopCellAtom);
672
- const focusBottomCell = useSetAtom(focusBottomCellAtom);
673
- const collapseCell = useSetAtom(collapseCellAtom);
674
- const expandCell = useSetAtom(expandCellAtom);
675
- const collapseAllCells = useSetAtom(collapseAllCellsAtom);
676
- const expandAllCells = useSetAtom(expandAllCellsAtom);
677
- const clearCellOutput = useSetAtom(clearCellOutputAtom);
678
- const clearCellConsoleOutput = useSetAtom(clearCellConsoleOutputAtom);
679
- const clearAllCellOutputs = useSetAtom(clearAllCellOutputsAtom);
680
- const showCellIfHidden = useSetAtom(showCellIfHiddenAtom);
681
- const moveCell = useSetAtom(moveCellAtom);
682
- const moveCellToIndex = useSetAtom(moveCellToIndexAtom);
683
- const moveCellsRelativeTo = useSetAtom(moveCellsRelativeToAtom);
684
- const dropCellOverCell = useSetAtom(dropCellOverCellAtom);
685
- const dropCellOverColumn = useSetAtom(dropCellOverColumnAtom);
686
- const sendToTop = useSetAtom(sendToTopAtom);
687
- const sendToBottom = useSetAtom(sendToBottomAtom);
688
- const addColumn = useSetAtom(addColumnAtom);
689
- const addColumnBreakpoint = useSetAtom(addColumnBreakpointAtom);
690
- const deleteColumn = useSetAtom(deleteColumnAtom);
691
- const mergeAllColumns = useSetAtom(mergeAllColumnsAtom);
692
- const compactColumns = useSetAtom(compactColumnsAtom);
693
- const moveColumn = useSetAtom(moveColumnAtom);
694
- const setCellCode = useSetAtom(setCellCodeAtom);
695
- const splitCell = useSetAtom(splitCellAtom);
696
- const undoSplitCell = useSetAtom(undoSplitCellAtom);
697
- return useMemo(
698
- () => ({
699
- ...operations,
700
- setCellCode,
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 useCellActions>;
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() {
@@ -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
  };
@@ -342,6 +342,38 @@ describe("cellExecutionView", () => {
342
342
  expect(cellExecutionView(over, C1).status).toBe("idle");
343
343
  });
344
344
 
345
+ it("indexes a requested cell list once across cell reads and run snapshots", () => {
346
+ const cellIds = [C1, C2];
347
+ const iterate = vi.spyOn(cellIds, Symbol.iterator);
348
+ onTestFinished(() => iterate.mockRestore());
349
+ const queued: Protocol.RunOutput = {
350
+ ...run([], "queued"),
351
+ origin: { kind: "cells", cell_ids: cellIds },
352
+ };
353
+ const running: Protocol.RunOutput = {
354
+ ...queued,
355
+ origin: { ...queued.origin },
356
+ state: { status: "running", started_at: T0 },
357
+ };
358
+
359
+ const before = inputs({ runs: [queued] });
360
+ const after = inputs({ runs: [running] });
361
+ expect(cellExecutionView(before, C1).status).toBe("queued");
362
+ expect(cellExecutionView(before, C2).status).toBe("queued");
363
+ expect(cellExecutionView(after, C2).status).toBe("queued");
364
+ expect(cellExecutionView(after, cellId("c3")).status).toBe("idle");
365
+ expect(iterate).toHaveBeenCalledTimes(1);
366
+ });
367
+
368
+ it("uses each snapshot's requested list when run IDs match", () => {
369
+ const first = inputs({ runs: [run(["c1"], "queued")] });
370
+ const second = inputs({ runs: [run(["c2"], "queued")] });
371
+ expect(cellExecutionView(first, C1).status).toBe("queued");
372
+ expect(cellExecutionView(second, C1).status).toBe("idle");
373
+ expect(cellExecutionView(second, C2).status).toBe("queued");
374
+ expect(cellExecutionView(first, C2).status).toBe("idle");
375
+ });
376
+
345
377
  it("does not read as queued a named cell a run cannot reach", () => {
346
378
  const runs = [run(["c1", "c9"], "queued")];
347
379
  expect(
@@ -184,6 +184,23 @@ function awaited(inputs: CellExecutionInputs, id: Protocol.CellId): boolean {
184
184
  );
185
185
  }
186
186
 
187
+ // The list is fixed when the run is accepted and can outlive a run snapshot.
188
+ const requestedCellSets = new WeakMap<
189
+ readonly Protocol.CellId[],
190
+ ReadonlySet<Protocol.CellId>
191
+ >();
192
+
193
+ function requestedCellSet(
194
+ cellIds: readonly Protocol.CellId[],
195
+ ): ReadonlySet<Protocol.CellId> {
196
+ let index = requestedCellSets.get(cellIds);
197
+ if (index === undefined) {
198
+ index = new Set(cellIds);
199
+ requestedCellSets.set(cellIds, index);
200
+ }
201
+ return index;
202
+ }
203
+
187
204
  export function awaitingRun(
188
205
  runs: Executions["runs"],
189
206
  id: Protocol.CellId,
@@ -194,7 +211,7 @@ export function awaitingRun(
194
211
  run.state.status !== "completed" &&
195
212
  run.id !== planned &&
196
213
  run.origin.kind === "cells" &&
197
- run.origin.cell_ids.includes(id)
214
+ requestedCellSet(run.origin.cell_ids).has(id)
198
215
  ) {
199
216
  return true;
200
217
  }
@@ -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: 5 });
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, useSetAtom } from "jotai";
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 useActions(
155
- options: { skipMiddleware?: boolean } = {},
156
+ function getActionsForStore(
157
+ store: JotaiStore,
158
+ options?: ReducerActionOptions,
156
159
  ): ReducerActions<Payloads> {
157
- const setState = useSetAtom(valueAtom);
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(setState);
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
- setState((state: State) => {
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(setState, actions);
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
  };