1- import type { Atom , WritableAtom } from './atom.ts'
2- import {
3- INTERNAL_buildStoreRev1 as INTERNAL_buildStore ,
4- INTERNAL_initializeStoreHooks ,
5- } from './internals.ts'
6- import type { INTERNAL_AtomState , INTERNAL_Store } from './internals.ts'
1+ import { INTERNAL_buildStoreRev1 as INTERNAL_buildStore } from './internals.ts'
2+ import type { INTERNAL_Store } from './internals.ts'
73
8- // TODO: rename this to `Store` in the near future
9- export type INTERNAL_PrdStore = INTERNAL_Store
10-
11- // For debugging purpose only
12- // This will be removed in the near future
13- /*@deprecated Deprecated: Use devstore from the devtools library */
14- export type INTERNAL_DevStoreRev4 = {
15- dev4_get_internal_weak_map :( ) => {
16- get :( atom :Atom < unknown > ) => INTERNAL_AtomState | undefined
17- }
18- dev4_get_mounted_atoms :( ) => Set < Atom < unknown > >
19- dev4_restore_atoms :(
20- values :Iterable < readonly [ Atom < unknown > , unknown ] > ,
21- ) => void
22- }
23-
24- /*@deprecated Deprecated: Use devstore from the devtools library */
25- const createDevStoreRev4 = ( ) :INTERNAL_PrdStore & INTERNAL_DevStoreRev4 => {
26- let inRestoreAtom = 0
27- const storeHooks = INTERNAL_initializeStoreHooks ( { } )
28- const atomStateMap = new WeakMap ( )
29- const mountedAtoms = new WeakMap ( )
30- const store = INTERNAL_buildStore (
31- atomStateMap ,
32- mountedAtoms ,
33- undefined ,
34- undefined ,
35- undefined ,
36- undefined ,
37- storeHooks ,
38- undefined ,
39- ( atom , get , set , ...args ) => {
40- if ( inRestoreAtom ) {
41- return set ( atom , ...args )
42- }
43- return atom . write ( get , set , ...args )
44- } ,
45- )
46- const debugMountedAtoms = new Set < Atom < unknown > > ( )
47- storeHooks . m . add ( undefined , ( atom ) => {
48- debugMountedAtoms . add ( atom )
49- const atomState = atomStateMap . get ( atom )
50- // For DevStoreRev4 compatibility
51- ; ( atomState as any ) . m = mountedAtoms . get ( atom )
52- } )
53- storeHooks . u . add ( undefined , ( atom ) => {
54- debugMountedAtoms . delete ( atom )
55- const atomState = atomStateMap . get ( atom )
56- // For DevStoreRev4 compatibility
57- delete ( atomState as any ) . m
58- } )
59- const devStore :INTERNAL_DevStoreRev4 = {
60- // store dev methods (these are tentative and subject to change without notice)
61- dev4_get_internal_weak_map :( ) => {
62- console . log ( 'Deprecated: Use devstore from the devtools library' )
63- return atomStateMap
64- } ,
65- dev4_get_mounted_atoms :( ) => debugMountedAtoms ,
66- dev4_restore_atoms :( values ) => {
67- const restoreAtom :WritableAtom < null , [ ] , void > = {
68- read :( ) => null ,
69- write :( _get , set ) => {
70- ++ inRestoreAtom
71- try {
72- for ( const [ atom , value ] of values ) {
73- if ( 'init' in atom ) {
74- set ( atom as never , value )
75- }
76- }
77- } finally {
78- -- inRestoreAtom
79- }
80- } ,
81- }
82- store . set ( restoreAtom )
83- } ,
84- }
85- return Object . assign ( store , devStore )
86- }
87-
88- type PrdOrDevStore =
89- | INTERNAL_PrdStore
90- | ( INTERNAL_PrdStore & INTERNAL_DevStoreRev4 )
4+ export type Store = INTERNAL_Store
915
926let overiddenCreateStore :typeof createStore | undefined
937
@@ -97,19 +11,16 @@ export function INTERNAL_overrideCreateStore(
9711overiddenCreateStore = fn ( overiddenCreateStore )
9812}
9913
100- export function createStore ( ) :PrdOrDevStore {
14+ export function createStore ( ) :Store {
10115if ( overiddenCreateStore ) {
10216return overiddenCreateStore ( )
10317}
104- if ( import . meta. env ?. MODE !== 'production' ) {
105- return createDevStoreRev4 ( )
106- }
10718return INTERNAL_buildStore ( )
10819}
10920
110- let defaultStore :PrdOrDevStore | undefined
21+ let defaultStore :Store | undefined
11122
112- export function getDefaultStore ( ) :PrdOrDevStore {
23+ export function getDefaultStore ( ) :Store {
11324if ( ! defaultStore ) {
11425defaultStore = createStore ( )
11526if ( import . meta. env ?. MODE !== 'production' ) {