Skip to content

Commit 26b1d10

Browse files
committed
fix: compose with prev createStore
1 parent 15fa585 commit 26b1d10

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Atom, WritableAtom, createStore } from 'jotai/vanilla';
33
import type {
44
INTERNAL_AtomState,
55
INTERNAL_Store,
6+
INTERNAL_getBuildingBlocksRev1,
67
} from 'jotai/vanilla/internals';
78

89
export type DevStore = {
@@ -17,6 +18,10 @@ export type StoreWithoutDevMethods = ReturnType<typeof createStore>;
1718
export type StoreWithDevMethods = INTERNAL_Store & DevStore;
1819

1920
export type Store = StoreWithoutDevMethods | StoreWithDevMethods;
21+
type Mutable<T> = { -readonly [P in keyof T]: T[P] };
22+
export type BuildingBlocks = Mutable<
23+
ReturnType<typeof INTERNAL_getBuildingBlocksRev1>
24+
>;
2025

2126
export type Options = Parameters<typeof useStore>[0];
2227

src/utils/internals/compose-with-devtools.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Atom, WritableAtom } from 'jotai';
2-
import { INTERNAL_overrideCreateStore } from 'jotai/vanilla';
2+
import { createStore, INTERNAL_overrideCreateStore } from 'jotai/vanilla';
33
import {
44
INTERNAL_buildStoreRev1 as INTERNAL_buildStore,
5+
INTERNAL_getBuildingBlocksRev1,
56
INTERNAL_initializeStoreHooks,
67
} from 'jotai/vanilla/internals';
78
import {
89
AnyAtom,
910
AnyAtomError,
1011
AnyAtomValue,
12+
BuildingBlocks,
1113
DevStore,
1214
Store,
1315
StoreWithDevMethods,
@@ -156,27 +158,24 @@ const __composeWithDevTools = (
156158
return store as typeof store & DevToolsStoreMethods;
157159
};
158160

159-
const createDevStore = (): StoreWithDevMethods => {
161+
const createDevStore = (
162+
prevCreateStore: (() => Store) | undefined,
163+
): StoreWithDevMethods => {
160164
let inRestoreAtom = 0;
161-
const storeHooks = INTERNAL_initializeStoreHooks({});
162-
const atomStateMap = new WeakMap();
163-
const mountedAtoms = new WeakMap();
164-
const store = INTERNAL_buildStore(
165-
atomStateMap,
166-
mountedAtoms,
167-
undefined,
168-
undefined,
169-
undefined,
170-
undefined,
171-
storeHooks,
172-
undefined,
173-
(atom, get, set, ...args) => {
174-
if (inRestoreAtom) {
175-
return set(atom, ...(args as any));
176-
}
177-
return atom.write(get, set, ...(args as any));
178-
},
179-
);
165+
const prevStore = prevCreateStore?.() ?? createStore();
166+
const buildingBlocks = [...INTERNAL_getBuildingBlocksRev1(prevStore)] as BuildingBlocks;
167+
const storeHooks = INTERNAL_initializeStoreHooks(buildingBlocks[6]);
168+
const atomWrite = buildingBlocks[8];
169+
buildingBlocks[8] = (atom, get, set, ...args) => {
170+
if (inRestoreAtom) {
171+
return set(atom, ...args);
172+
}
173+
return atomWrite
174+
? atomWrite(atom, get, set, ...args)
175+
: atom.write(get, set, ...args);
176+
};
177+
const store = INTERNAL_buildStore(...buildingBlocks);
178+
const [atomStateMap, mountedAtoms] = buildingBlocks;
180179
const debugMountedAtoms = new Set<Atom<unknown>>();
181180
storeHooks.m.add(undefined, (atom) => {
182181
debugMountedAtoms.add(atom);
@@ -222,7 +221,7 @@ const isDevStore = (store: Store): store is StoreWithDevMethods => {
222221
};
223222

224223
INTERNAL_overrideCreateStore((prev) => {
225-
return createDevStore;
224+
return () => createDevStore(prev);
226225
});
227226

228227
export const composeWithDevTools = (

0 commit comments

Comments
 (0)