1
1
/* eslint-disable no-unused-vars */
2
2
3
- import {
4
- EffectBuilder ,
5
- State ,
6
- StoreApi ,
7
- StoreBuilderApi ,
8
- StoreDef ,
9
- } from './types' ;
3
+ import { EffectBuilder , State , StoreApi , StoreDef } from './types' ;
10
4
import { StoreOptions } from './types/CreateStoreOptions' ;
11
5
12
6
import React from 'react' ;
@@ -17,8 +11,12 @@ import {
17
11
} from './utils/create-computed-methods' ;
18
12
import { createStore } from './utils/create-inner-store' ;
19
13
import { createSplitProps } from './utils/split-props' ;
14
+ import { createStoreApiProxy } from './utils/create-store-proxy' ;
20
15
21
- export const storeBuilder = < TState extends State > ( ) => {
16
+ export const store = < TState extends State > (
17
+ initialState ?: TState ,
18
+ options ?: StoreOptions < TState >
19
+ ) : StoreApi < TState > => {
22
20
const _def = {
23
21
initialState : undefined ,
24
22
input : { } ,
@@ -39,25 +37,23 @@ export const storeBuilder = <TState extends State>() => {
39
37
) {
40
38
_def . extensions . push ( builder ) ;
41
39
42
- if ( _def . options . onExtend ) {
43
- return _def . options . onExtend ( builder ) ;
44
- }
45
- return builderMethods ;
40
+ return storeApi ;
46
41
}
47
42
48
- const builderMethods = {
43
+ const storeApi = createStoreApiProxy ( {
44
+ _def,
49
45
options : ( newOpts : any ) => {
50
46
Object . assign ( _def . options , newOpts ) ;
51
- return builderMethods ;
47
+ return storeApi ;
52
48
} ,
53
49
state : ( initialValue : TState ) => {
54
50
Object . assign ( _def , { initialState : initialValue } ) ;
55
- return builderMethods ;
51
+ return storeApi ;
56
52
} ,
57
53
58
54
name : ( newName : string ) => {
59
55
Object . assign ( _def . options , { name : newName } ) ;
60
- return builderMethods ;
56
+ return storeApi ;
61
57
} ,
62
58
63
59
/**
@@ -71,7 +67,6 @@ export const storeBuilder = <TState extends State>() => {
71
67
effects : < TBuilder extends EffectBuilder < StoreApi < TState , { } > > > (
72
68
builder : TBuilder
73
69
) : StoreApi < TState , { } > => {
74
- // @ts -expect-error
75
70
return extend ( ( store ) => {
76
71
const effectNameToFn = builder ( store ) ;
77
72
const unsubMethods : Record < string , ( ) => void > = { } ;
@@ -93,15 +88,11 @@ export const storeBuilder = <TState extends State>() => {
93
88
unsubscribeFromEffects,
94
89
} ;
95
90
96
- // subscribe to the effects when the store is created
97
- subscribeToEffects ( ) ;
98
-
99
91
return extraProps ;
100
92
} ) ;
101
93
} ,
102
94
computed : < TComputedProps extends ComputedProps > (
103
95
computedCallback : ComputedBuilder < TState , TComputedProps >
104
- // ): StoreApi<TState, TComputedProps> => {
105
96
) => {
106
97
return extend ( ( store ) =>
107
98
// @ts -expect-error
@@ -110,7 +101,10 @@ export const storeBuilder = <TState extends State>() => {
110
101
} ,
111
102
create : ( initialValue : Partial < TState > & Record < string , any > ) => {
112
103
if ( ! initialValue ) {
113
- return createStore ( _def ) ;
104
+ const instance = createStore ( _def ) ;
105
+
106
+ Object . assign ( instance , storeApi ) ;
107
+ return instance ;
114
108
}
115
109
116
110
if ( ! isObject ( _def . initialState ) ) {
@@ -130,90 +124,24 @@ export const storeBuilder = <TState extends State>() => {
130
124
// @ts -expect-error
131
125
return createStore ( _def , stateInitialValue , inputInitialValue ) ;
132
126
} ,
133
- } ;
134
-
135
- return Object . assign ( builderMethods , {
136
- _def,
137
- } ) as unknown as StoreBuilderApi < TState > ;
138
- } ;
139
-
140
- export const store = < TState extends State > (
141
- initialState ?: TState ,
142
- options ?: StoreOptions < TState >
143
- ) : StoreBuilderApi < TState > => {
144
- let globalStore = { } as unknown as StoreBuilderApi < TState > ;
145
-
146
- const _options = {
147
- ...options ,
148
- onExtend : ( builder : any ) => {
149
- if ( options ?. onExtend ) {
150
- options . onExtend ( store ) ;
151
- }
152
-
153
- Object . assign ( globalStore , builder ( globalStore ) ) ;
154
- return globalStore as unknown as StoreApi < TState , { } > ;
155
- } ,
156
- } ;
127
+ } ) ;
157
128
129
+ // must check for undefined to allow for 0 as a valid initial state
158
130
if ( initialState !== undefined ) {
159
- const builder = storeBuilder ( )
160
- . options ( _options as any )
161
- . state ( initialState ) ;
162
-
163
- const instance = builder . create ( ) as StoreApi < TState > ;
164
-
165
- Object . assign ( instance , builder ) ;
166
-
167
- // @ts -expect-error
168
- globalStore = instance ;
169
- } else {
170
- const stateFn = ( _initialState : any ) => {
171
- const builder = storeBuilder ( )
172
- . options ( _options as any )
173
- . state ( _initialState ) ;
174
-
175
- const instance = builder . create ( ) as StoreApi < TState > ;
176
-
177
- Object . assign ( instance , builder ) ;
178
-
179
- // @ts -expect-error
180
- globalStore = instance ;
181
- return globalStore as StoreBuilderApi < TState > ;
182
- } ;
183
-
184
- // @ts -expect-error
185
- globalStore = {
186
- state : stateFn ,
187
- } ;
131
+ Object . assign ( _def , { initialState } ) ;
188
132
}
189
133
190
- return globalStore as unknown as StoreBuilderApi < TState > ;
191
-
192
- // return storeBuilder()
193
- // .state(initialState)
194
- // .options(options ?? {})
195
- // .create() as StoreApi<TState>;
196
-
197
- // if (initialState !== undefined && options !== undefined)
198
- // return storeBuilder()
199
- // .options(options as any)
200
- // .state(initialState) as StoreApi<TState>;
201
-
202
- // if (initialState !== undefined) {
203
- // return storeBuilder().state(initialState).create() as StoreApi<TState>;
204
- // }
205
-
206
- // if (options !== undefined) {
207
- // return storeBuilder().options(options as any) as StoreApi<TState>;
208
- // }
134
+ if ( options ) {
135
+ Object . assign ( _def , { options } ) ;
136
+ }
209
137
210
- // return storeBuilder() as StoreApi<TState>;
138
+ return storeApi as unknown as StoreApi < TState > ;
211
139
} ;
212
140
213
141
export function createStoreContext <
214
142
TState extends State ,
215
143
TExtensions extends object ,
216
- > ( store : StoreBuilderApi < TState , TExtensions > ) {
144
+ > ( store : StoreApi < TState , TExtensions > ) {
217
145
const Context = React . createContext < StoreApi < TState , TExtensions > | null > (
218
146
null
219
147
) ;
@@ -230,13 +158,17 @@ export function createStoreContext<
230
158
) ;
231
159
232
160
React . useEffect ( ( ) => {
161
+ const instance = storeInstance . current ;
162
+ if ( instance && 'subscribeToEffects' in instance ) {
163
+ const fn = instance . subscribeToEffects ;
164
+ if ( typeof fn === 'function' ) fn ( ) ;
165
+ }
166
+
233
167
return ( ) => {
234
- if (
235
- storeInstance . current &&
236
- 'unsubscribeFromEffects' in storeInstance . current
237
- ) {
238
- // @ts -expect-error
239
- storeInstance . current ?. unsubscribeFromEffects ?.( ) ;
168
+ const instance = storeInstance . current ;
169
+ if ( instance && 'unsubscribeFromEffects' in instance ) {
170
+ const fn = instance . unsubscribeFromEffects ;
171
+ if ( typeof fn === 'function' ) fn ( ) ;
240
172
}
241
173
} ;
242
174
} , [ ] ) ;
0 commit comments