@@ -173,6 +173,16 @@ function Model<M extends Models, MT extends ModelType, E>(
173173 useStore : ( selector ?: Function ) => useStore ( hash , selector )
174174 }
175175 } else {
176+ if ( initialState ) {
177+ // TODO: support multi model group under SSR
178+ Global . gid = 1
179+ } else {
180+ Global . gid += 1
181+ }
182+ let prefix = ''
183+ if ( Global . gid > 1 ) {
184+ prefix = Global . gid + '_'
185+ }
176186 if ( models . actions ) {
177187 console . error ( 'invalid model(s) schema: ' , models )
178188 const errorFn = ( fakeReturnVal ?: unknown ) => ( ..._ : unknown [ ] ) => {
@@ -196,8 +206,10 @@ function Model<M extends Models, MT extends ModelType, E>(
196206 } )
197207 }
198208 extContext && ( Global . Context [ '__global' ] = extContext )
199- Object . keys ( models ) . forEach ( ( name ) => {
200- const model = models [ name ]
209+ let actions : { [ name : string ] : any } = { }
210+ Object . keys ( models ) . forEach ( ( n ) => {
211+ let name = prefix + n
212+ const model = models [ n ]
201213 if ( model . __ERROR__ ) {
202214 // Fallback State and Actions when model schema is invalid
203215 console . error ( name + " model's schema is invalid" )
@@ -239,12 +251,9 @@ function Model<M extends Models, MT extends ModelType, E>(
239251 Global . Middlewares [ name ] = Global . Middlewares [ model . __id ]
240252 Global . Context [ name ] = Global . Context [ model . __id ]
241253 }
242- } )
243254
244- const actions = Object . keys ( models ) . reduce (
245- ( o , modelName ) => ( { ...o , [ modelName ] : getActions ( modelName ) } ) ,
246- { }
247- )
255+ actions [ n ] = getActions ( name )
256+ } )
248257
249258 Global . withDevTools =
250259 typeof window !== 'undefined' &&
@@ -255,12 +264,23 @@ function Model<M extends Models, MT extends ModelType, E>(
255264 }
256265 return {
257266 actions,
258- getActions,
259- getInitialState,
260- getState,
261- subscribe,
262- unsubscribe,
263- useStore
267+ getActions : ( name : string ) => getActions ( prefix + name ) ,
268+ getInitialState : async < T extends { modelName : string | string [ ] } > (
269+ context ?: T ,
270+ config ?: { isServer ?: boolean }
271+ ) => getInitialState ( context , { ...config , prefix } ) ,
272+ getState : ( name : string ) => getState ( prefix + name ) ,
273+ subscribe : (
274+ name : string ,
275+ actions : keyof MT [ 'actions' ] | Array < keyof MT [ 'actions' ] > ,
276+ callback : ( ) => void
277+ ) => subscribe ( prefix + name , actions as string | string [ ] , callback ) ,
278+ unsubscribe : (
279+ name : string ,
280+ actionName : keyof MT [ 'actions' ] | Array < keyof MT [ 'actions' ] >
281+ ) => unsubscribe ( prefix + name , actionName as string | string [ ] ) ,
282+ useStore : ( name : string , selector ?: Function ) =>
283+ useStore ( prefix + name , selector )
264284 } as APIs < M >
265285 }
266286}
0 commit comments