|
1 | 1 | import { Atom, WritableAtom } from 'jotai'; |
2 | | -import { INTERNAL_overrideCreateStore } from 'jotai/vanilla'; |
| 2 | +import { createStore, INTERNAL_overrideCreateStore } from 'jotai/vanilla'; |
3 | 3 | import { |
4 | 4 | INTERNAL_buildStoreRev1 as INTERNAL_buildStore, |
| 5 | + INTERNAL_getBuildingBlocksRev1, |
5 | 6 | INTERNAL_initializeStoreHooks, |
6 | 7 | } from 'jotai/vanilla/internals'; |
7 | 8 | import { |
8 | 9 | AnyAtom, |
9 | 10 | AnyAtomError, |
10 | 11 | AnyAtomValue, |
| 12 | + BuildingBlocks, |
11 | 13 | DevStore, |
12 | 14 | Store, |
13 | 15 | StoreWithDevMethods, |
@@ -156,27 +158,24 @@ const __composeWithDevTools = ( |
156 | 158 | return store as typeof store & DevToolsStoreMethods; |
157 | 159 | }; |
158 | 160 |
|
159 | | -const createDevStore = (): StoreWithDevMethods => { |
| 161 | +const createDevStore = ( |
| 162 | + prevCreateStore: (() => Store) | undefined, |
| 163 | +): StoreWithDevMethods => { |
160 | 164 | 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; |
180 | 179 | const debugMountedAtoms = new Set<Atom<unknown>>(); |
181 | 180 | storeHooks.m.add(undefined, (atom) => { |
182 | 181 | debugMountedAtoms.add(atom); |
@@ -222,7 +221,7 @@ const isDevStore = (store: Store): store is StoreWithDevMethods => { |
222 | 221 | }; |
223 | 222 |
|
224 | 223 | INTERNAL_overrideCreateStore((prev) => { |
225 | | - return createDevStore; |
| 224 | + return () => createDevStore(prev); |
226 | 225 | }); |
227 | 226 |
|
228 | 227 | export const composeWithDevTools = ( |
|
0 commit comments