1- import { Atom } from 'jotai' ;
1+ import { Atom , WritableAtom } from 'jotai' ;
2+ import { INTERNAL_overrideCreateStore } from 'jotai/vanilla' ;
3+ import {
4+ INTERNAL_buildStoreRev1 as INTERNAL_buildStore ,
5+ INTERNAL_initializeStoreHooks ,
6+ } from 'jotai/vanilla/internals' ;
27import {
38 AnyAtom ,
49 AnyAtomError ,
510 AnyAtomValue ,
11+ DevStore ,
612 Store ,
713 StoreWithDevMethods ,
814} from '../../types' ;
@@ -33,17 +39,13 @@ type DevToolsStoreMethods = {
3339
3440type WithDevToolsStore < S extends Store > = S & DevToolsStoreMethods ;
3541
36- const isDevStore = ( store : Store | undefined ) : store is StoreWithDevMethods => {
37- return store ? 'dev4_get_internal_weak_map' in store : false ;
38- } ;
39-
4042export const isDevToolsStore = (
4143 store : Store | WithDevToolsStore < Store > ,
4244) : store is WithDevToolsStore < Store > => {
4345 return 'subscribeStore' in store ;
4446} ;
4547
46- const __composeDevTools = (
48+ const __composeWithDevTools = (
4749 store : StoreWithDevMethods ,
4850) : WithDevToolsStore < StoreWithDevMethods > => {
4951 const { sub, set, get } = store ;
@@ -120,11 +122,11 @@ const __composeDevTools = (
120122 } ;
121123
122124 ( store as WithDevToolsStore < typeof store > ) . getMountedAtoms = ( ) => {
123- return store . dev4_get_mounted_atoms ( ) ;
125+ return store . get_mounted_atoms ( ) ;
124126 } ;
125127
126128 ( store as WithDevToolsStore < typeof store > ) . getAtomState = ( atom ) => {
127- const aState = store . dev4_get_internal_weak_map ( ) . get ( atom ) ;
129+ const aState = store . get_internal_weak_map ( ) . get ( atom ) ;
128130
129131 if ( aState ) {
130132 return { v : aState . v , e : aState . e , d : new Set ( aState . d . keys ( ) ) } ;
@@ -134,26 +136,98 @@ const __composeDevTools = (
134136 } ;
135137
136138 ( store as WithDevToolsStore < typeof store > ) . getMountedAtomState = ( atom ) => {
137- const aState = store . dev4_get_internal_weak_map ( ) . get ( atom ) ;
139+ const aState = store . get_internal_weak_map ( ) . get ( atom ) ;
138140
139- if ( aState && aState . m ) {
141+ if ( aState && 'm' in aState ) {
140142 return {
141- l : aState . m . l ,
142- t : aState . m . t ,
143+ l : ( aState . m as any ) . l ,
144+ t : ( aState . m as any ) . t ,
143145 } ;
144146 }
145147
146148 return undefined ;
147149 } ;
148150
149151 ( store as WithDevToolsStore < typeof store > ) . restoreAtoms = ( values ) => {
150- store . dev4_restore_atoms ( values ) ;
152+ store . restore_atoms ( values ) ;
151153 storeListeners . forEach ( ( l ) => l ( { type : 'restore' } ) ) ;
152154 } ;
153155
154156 return store as typeof store & DevToolsStoreMethods ;
155157} ;
156158
159+ const createDevStore = ( ) : StoreWithDevMethods => {
160+ 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+ ) ;
180+ const debugMountedAtoms = new Set < Atom < unknown > > ( ) ;
181+ storeHooks . m . add ( undefined , ( atom ) => {
182+ debugMountedAtoms . add ( atom ) ;
183+ const atomState = atomStateMap . get ( atom ) ;
184+ // For DevStoreRev4 compatibility
185+ ( atomState as any ) . m = mountedAtoms . get ( atom ) ;
186+ } ) ;
187+ storeHooks . u . add ( undefined , ( atom ) => {
188+ debugMountedAtoms . delete ( atom ) ;
189+ const atomState = atomStateMap . get ( atom ) ;
190+ // For DevStoreRev4 compatibility
191+ delete ( atomState as any ) . m ;
192+ } ) ;
193+ const devStore : DevStore = {
194+ // store dev methods (these are tentative and subject to change without notice)
195+ get_internal_weak_map : ( ) => atomStateMap ,
196+ get_mounted_atoms : ( ) => debugMountedAtoms ,
197+ restore_atoms : ( values ) => {
198+ const restoreAtom : WritableAtom < null , [ ] , void > = {
199+ read : ( ) => null ,
200+ write : ( _get , set ) => {
201+ ++ inRestoreAtom ;
202+ try {
203+ for ( const [ atom , value ] of values ) {
204+ if ( 'init' in atom ) {
205+ set ( atom as never , value ) ;
206+ }
207+ }
208+ } finally {
209+ -- inRestoreAtom ;
210+ }
211+ } ,
212+ } ;
213+ store . set ( restoreAtom ) ;
214+ } ,
215+ } ;
216+
217+ return {
218+ ...store ,
219+ ...devStore ,
220+ } ;
221+ } ;
222+
223+ const isDevStore = ( store : Store ) : store is StoreWithDevMethods => {
224+ return 'get_internal_weak_map' in store ;
225+ } ;
226+
227+ INTERNAL_overrideCreateStore ( ( prev ) => {
228+ return createDevStore ;
229+ } ) ;
230+
157231export const composeWithDevTools = (
158232 store : Store ,
159233) : typeof store | WithDevToolsStore < typeof store > => {
@@ -163,7 +237,7 @@ export const composeWithDevTools = (
163237 }
164238
165239 if ( isDevStore ( store ) ) {
166- return __composeDevTools ( store ) ;
240+ return __composeWithDevTools ( store ) ;
167241 }
168242
169243 return store ;
0 commit comments