4
4
AnyAtomError ,
5
5
AnyAtomValue ,
6
6
Store ,
7
- StoreV1 ,
8
- StoreV2 ,
7
+ StoreWithDevMethods ,
9
8
} from '../../types' ;
10
9
11
10
type DevSubscribeStoreListener = ( action : {
@@ -34,23 +33,19 @@ type DevToolsStoreMethods = {
34
33
35
34
type WithDevToolsStore < S extends Store > = S & DevToolsStoreMethods ;
36
35
37
- const isStoreV2 = ( store : Store | undefined ) : store is StoreV2 => {
36
+ const isDevStore = ( store : Store | undefined ) : store is StoreWithDevMethods => {
38
37
return store ? 'dev4_get_internal_weak_map' in store : false ;
39
38
} ;
40
39
41
- const isStoreV1 = ( store : Store | undefined ) : store is StoreV1 => {
42
- return ! isStoreV2 ( store ) ;
43
- } ;
44
-
45
40
export const isDevToolsStore = (
46
41
store : Store | WithDevToolsStore < Store > ,
47
42
) : store is WithDevToolsStore < Store > => {
48
43
return 'subscribeStore' in store ;
49
44
} ;
50
45
51
- const __composeV2StoreWithDevTools = (
52
- store : StoreV2 ,
53
- ) : WithDevToolsStore < StoreV2 > => {
46
+ const __composeDevTools = (
47
+ store : StoreWithDevMethods ,
48
+ ) : WithDevToolsStore < StoreWithDevMethods > => {
54
49
const { sub, set, get } = store ;
55
50
const storeListeners : Set < DevSubscribeStoreListener > = new Set ( ) ;
56
51
@@ -159,83 +154,6 @@ const __composeV2StoreWithDevTools = (
159
154
return store as typeof store & DevToolsStoreMethods ;
160
155
} ;
161
156
162
- const __composeV1StoreWithDevTools = (
163
- store : StoreV1 ,
164
- ) : StoreV1 | WithDevToolsStore < StoreV1 > => {
165
- if (
166
- 'dev_subscribe_store' in store &&
167
- 'dev_get_mounted_atoms' in store &&
168
- 'dev_get_atom_state' in store &&
169
- 'dev_get_mounted' in store &&
170
- 'dev_restore_atoms' in store
171
- ) {
172
- const {
173
- dev_subscribe_store,
174
- dev_get_mounted_atoms,
175
- dev_get_atom_state,
176
- dev_get_mounted,
177
- dev_restore_atoms,
178
- } = store ;
179
-
180
- ( store as WithDevToolsStore < typeof store > ) . subscribeStore = ( l ) => {
181
- const cb : Parameters < typeof dev_subscribe_store > [ 0 ] = ( action ) => {
182
- if ( action . type === 'write' || action . type === 'async-write' ) {
183
- l ( { type : 'set' } ) ;
184
- }
185
-
186
- if ( action . type === 'sub' ) {
187
- l ( { type : 'sub' } ) ;
188
- }
189
-
190
- if ( action . type === 'unsub' ) {
191
- l ( { type : 'unsub' } ) ;
192
- }
193
-
194
- if ( action . type === 'restore' ) {
195
- l ( { type : 'restore' } ) ;
196
- }
197
- } ;
198
-
199
- return dev_subscribe_store ( cb , 2 ) ;
200
- } ;
201
-
202
- ( store as WithDevToolsStore < typeof store > ) . getMountedAtoms = ( ) => {
203
- return dev_get_mounted_atoms ( ) ;
204
- } ;
205
-
206
- ( store as WithDevToolsStore < typeof store > ) . getAtomState = ( atom ) => {
207
- const aState = dev_get_atom_state ( atom ) ;
208
-
209
- if ( aState ) {
210
- const d = new Set ( aState . d . keys ( ) ) ;
211
- d . delete ( atom ) ;
212
-
213
- if ( 'v' in aState ) {
214
- return { v : aState . v , d } ;
215
- }
216
- if ( 'e' in aState ) {
217
- return { e : aState . e , d } ;
218
- }
219
-
220
- return undefined ;
221
- }
222
- } ;
223
-
224
- ( store as WithDevToolsStore < typeof store > ) . getMountedAtomState = ( atom ) => {
225
- const mounted = dev_get_mounted ( atom ) ;
226
- return mounted ;
227
- } ;
228
-
229
- ( store as WithDevToolsStore < typeof store > ) . restoreAtoms = ( values ) => {
230
- dev_restore_atoms ( values ) ;
231
- } ;
232
-
233
- return store as typeof store & DevToolsStoreMethods ;
234
- }
235
-
236
- return store ;
237
- } ;
238
-
239
157
export const composeWithDevTools = (
240
158
store : Store ,
241
159
) : typeof store | WithDevToolsStore < typeof store > => {
@@ -244,12 +162,8 @@ export const composeWithDevTools = (
244
162
return store ;
245
163
}
246
164
247
- if ( isStoreV2 ( store ) ) {
248
- return __composeV2StoreWithDevTools ( store ) ;
249
- }
250
-
251
- if ( isStoreV1 ( store ) ) {
252
- return __composeV1StoreWithDevTools ( store ) ;
165
+ if ( isDevStore ( store ) ) {
166
+ return __composeDevTools ( store ) ;
253
167
}
254
168
255
169
return store ;
0 commit comments