@@ -70,19 +70,9 @@ var Setter = {
70
70
classSetter : undefined ,
71
71
functionSetter : { }
72
72
} ;
73
- // If get the Hooks Api from
74
- // import {useState, useEffect, ...} from 'react'
75
- // will throw Error Hooks can only be called inside the body of a function component
76
- var hooksApi = { } ;
77
- var registerModel = function ( models , hooks ) {
78
- if ( hooks === void 0 ) { hooks = {
79
- useCallback : react_1 . useCallback ,
80
- useContext : react_1 . useContext ,
81
- useEffect : react_1 . useEffect ,
82
- useState : react_1 . useState
83
- } ; }
73
+ var uid = Math . random ( ) ; // The unique id of hooks
74
+ var registerModel = function ( models ) {
84
75
GlobalState = __assign ( { } , models ) ;
85
- hooksApi = __assign ( { } , hooks ) ;
86
76
} ;
87
77
exports . registerModel = registerModel ;
88
78
var Provider = /** @class */ ( function ( _super ) {
@@ -106,17 +96,17 @@ var setPartialState = function (name, partialState) {
106
96
actions : GlobalState [ name ] . actions ,
107
97
state : __assign ( { } , GlobalState [ name ] . state , partialState )
108
98
} , _a ) ) ;
109
- console . log ( Object . keys ( Setter . functionSetter [ 'Home' ] ) . length , Object . keys ( Setter . functionSetter [ 'Shared' ] ) . length ) ;
110
99
return GlobalState ;
111
100
} ;
112
101
var useStore = function ( modelName ) {
113
102
// const _state = useContext(GlobalContext)
114
- var _a = hooksApi . useState ( GlobalState [ modelName ] . state ) , state = _a [ 0 ] , setState = _a [ 1 ] ;
115
- var _hash = new Date ( ) . toISOString ( ) + Math . random ( ) ;
103
+ var _a = react_1 . useState ( GlobalState [ modelName ] . state ) , state = _a [ 0 ] , setState = _a [ 1 ] ;
104
+ uid += 1 ;
105
+ var _hash = '' + uid ;
116
106
if ( ! Setter . functionSetter [ modelName ] )
117
107
Setter . functionSetter [ modelName ] = [ ] ;
118
108
Setter . functionSetter [ modelName ] [ _hash ] = { setState : setState } ;
119
- hooksApi . useEffect ( function ( ) {
109
+ react_1 . useEffect ( function ( ) {
120
110
return function cleanup ( ) {
121
111
delete Setter . functionSetter [ modelName ] [ _hash ] ;
122
112
} ;
@@ -154,7 +144,7 @@ var useStore = function (modelName) {
154
144
return ret ;
155
145
} ;
156
146
Object . keys ( GlobalState [ modelName ] . actions ) . map ( function ( key ) {
157
- return ( updaters [ key ] = hooksApi . useCallback ( function ( params ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
147
+ return ( updaters [ key ] = react_1 . useCallback ( function ( params ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
158
148
var newState ;
159
149
return __generator ( this , function ( _a ) {
160
150
switch ( _a . label ) {
@@ -170,10 +160,9 @@ var useStore = function (modelName) {
170
160
return [ 2 /*return*/ ] ;
171
161
}
172
162
} ) ;
173
- } ) ; } , [ GlobalState ] ) ) ;
163
+ } ) ; } , [ GlobalState [ modelName ] ] ) ) ;
174
164
} ) ;
175
165
return [ state , updaters ] ;
176
- // return [state, setState]
177
166
} ;
178
167
exports . useStore = useStore ;
179
168
var connect = function ( modelName , mapProps ) { return function ( Component ) {
0 commit comments