Skip to content

Commit 2c586d0

Browse files
committed
fix: compose with prev createStore
1 parent 15fa585 commit 2c586d0

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-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: 23 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 { INTERNAL_overrideCreateStore, createStore } 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,26 @@ 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?.() ?? INTERNAL_buildStore();
166+
const buildingBlocks = [
167+
...INTERNAL_getBuildingBlocksRev1(prevStore),
168+
] as BuildingBlocks;
169+
const storeHooks = INTERNAL_initializeStoreHooks(buildingBlocks[6]);
170+
const atomWrite = buildingBlocks[8];
171+
buildingBlocks[8] = (atom, get, set, ...args) => {
172+
if (inRestoreAtom) {
173+
return set(atom, ...args);
174+
}
175+
return atomWrite
176+
? atomWrite(atom, get, set, ...args)
177+
: atom.write(get, set, ...args);
178+
};
179+
const store = INTERNAL_buildStore(...buildingBlocks);
180+
const [atomStateMap, mountedAtoms] = buildingBlocks;
180181
const debugMountedAtoms = new Set<Atom<unknown>>();
181182
storeHooks.m.add(undefined, (atom) => {
182183
debugMountedAtoms.add(atom);
@@ -222,7 +223,7 @@ const isDevStore = (store: Store): store is StoreWithDevMethods => {
222223
};
223224

224225
INTERNAL_overrideCreateStore((prev) => {
225-
return createDevStore;
226+
return () => createDevStore(prev);
226227
});
227228

228229
export const composeWithDevTools = (

0 commit comments

Comments
 (0)