@@ -23,15 +23,22 @@ const isAPI = (input: any): input is API => {
23
23
24
24
// useModel rules:
25
25
// DON'T USE useModel OUTSIDE createStore func
26
- function useModel < S > ( state : S ) : [ S , ( state : S ) => void ] {
26
+ function useModel < S > (
27
+ state : S | ( ( ) => S )
28
+ ) : [ S , ( state : S | ( ( state : S ) => S ) ) => void ] {
27
29
const storeId = Global . currentStoreId
28
30
const index = Global . mutableState [ storeId ] . count
29
31
Global . mutableState [ storeId ] . count += 1
30
32
if ( ! Global . mutableState [ storeId ] . hasOwnProperty ( index ) ) {
31
- Global . mutableState [ storeId ] [ index ] = state
33
+ if ( typeof state === 'function' ) {
34
+ // @ts -ignore
35
+ Global . mutableState [ storeId ] [ index ] = state ( )
36
+ } else {
37
+ Global . mutableState [ storeId ] [ index ] = state
38
+ }
32
39
}
33
40
34
- const setter = async ( state : S ) => {
41
+ const setter = async ( state : S | ( ( prevState : S ) => S ) ) => {
35
42
const context : InnerContext = {
36
43
Global,
37
44
action : ( ) => {
@@ -46,7 +53,16 @@ function useModel<S>(state: S): [S, (state: S) => void] {
46
53
params : undefined ,
47
54
type : 'useModel'
48
55
}
49
- Global . mutableState [ storeId ] [ index ] = state
56
+
57
+ if ( typeof state === 'function' ) {
58
+ // @ts -ignore
59
+ Global . mutableState [ storeId ] [ index ] = state (
60
+ Global . mutableState [ storeId ] [ index ]
61
+ )
62
+ } else {
63
+ Global . mutableState [ storeId ] [ index ] = state
64
+ }
65
+ // pass update event to other components subscribe the same store
50
66
return await applyMiddlewares ( actionMiddlewares , context )
51
67
}
52
68
return [ Global . mutableState [ storeId ] [ index ] , setter ]
@@ -124,11 +140,9 @@ function Model<M extends Models, MT extends ModelType, E>(
124
140
} else {
125
141
if ( models . actions ) {
126
142
console . error ( 'invalid model(s) schema: ' , models )
127
- const errorFn =
128
- ( fakeReturnVal ?: unknown ) =>
129
- ( ..._ : unknown [ ] ) => {
130
- return fakeReturnVal
131
- }
143
+ const errorFn = ( fakeReturnVal ?: unknown ) => ( ..._ : unknown [ ] ) => {
144
+ return fakeReturnVal
145
+ }
132
146
// Fallback Functions
133
147
return {
134
148
__ERROR__ : true ,
@@ -325,41 +339,39 @@ class Provider extends PureComponent<{}, Global['State']> {
325
339
}
326
340
}
327
341
328
- const connect =
329
- (
330
- modelName : string ,
331
- mapState ?: Function | undefined ,
332
- mapActions ?: Function | undefined
333
- ) =>
334
- ( Component : typeof React . Component | typeof PureComponent ) =>
335
- class P extends PureComponent < any > {
336
- render ( ) {
337
- const { state : prevState = { } , actions : prevActions = { } } = this . props
338
- return (
339
- < Consumer >
340
- { ( models ) => {
341
- const { [ `${ modelName } ` ] : state } = models as any
342
- const actions = Global . Actions [ modelName ]
343
- return (
344
- < Component
345
- { ...this . props }
346
- state = { {
347
- ...prevState ,
348
- ...( mapState ? mapState ( state ) : state )
349
- } }
350
- actions = { {
351
- ...prevActions ,
352
- ...( mapActions
353
- ? mapActions ( consumerActions ( actions , { modelName } ) )
354
- : consumerActions ( actions , { modelName } ) )
355
- } }
356
- />
357
- )
358
- } }
359
- </ Consumer >
360
- )
361
- }
342
+ const connect = (
343
+ modelName : string ,
344
+ mapState ?: Function | undefined ,
345
+ mapActions ?: Function | undefined
346
+ ) => ( Component : typeof React . Component | typeof PureComponent ) =>
347
+ class P extends PureComponent < any > {
348
+ render ( ) {
349
+ const { state : prevState = { } , actions : prevActions = { } } = this . props
350
+ return (
351
+ < Consumer >
352
+ { ( models ) => {
353
+ const { [ `${ modelName } ` ] : state } = models as any
354
+ const actions = Global . Actions [ modelName ]
355
+ return (
356
+ < Component
357
+ { ...this . props }
358
+ state = { {
359
+ ...prevState ,
360
+ ...( mapState ? mapState ( state ) : state )
361
+ } }
362
+ actions = { {
363
+ ...prevActions ,
364
+ ...( mapActions
365
+ ? mapActions ( consumerActions ( actions , { modelName } ) )
366
+ : consumerActions ( actions , { modelName } ) )
367
+ } }
368
+ />
369
+ )
370
+ } }
371
+ </ Consumer >
372
+ )
362
373
}
374
+ }
363
375
364
376
export {
365
377
actionMiddlewares ,
0 commit comments