|
1 | 1 | import { Atom, WritableAtom } from 'jotai';
|
2 |
| -import { INTERNAL_overrideCreateStore } from 'jotai/vanilla'; |
| 2 | +import { INTERNAL_overrideCreateStore, createStore } 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,26 @@ 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?.() ?? 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; |
180 | 181 | const debugMountedAtoms = new Set<Atom<unknown>>();
|
181 | 182 | storeHooks.m.add(undefined, (atom) => {
|
182 | 183 | debugMountedAtoms.add(atom);
|
@@ -222,7 +223,7 @@ const isDevStore = (store: Store): store is StoreWithDevMethods => {
|
222 | 223 | };
|
223 | 224 |
|
224 | 225 | INTERNAL_overrideCreateStore((prev) => {
|
225 |
| - return createDevStore; |
| 226 | + return () => createDevStore(prev); |
226 | 227 | });
|
227 | 228 |
|
228 | 229 | export const composeWithDevTools = (
|
|
0 commit comments