11/// <reference path="./index.d.ts" />
22import * as React from 'react'
3- import Global from './global'
43import { PureComponent , useEffect , useState } from 'react'
4+ import Global from './global'
55import {
6- GlobalContext ,
76 Consumer ,
7+ consumerActions ,
88 getInitialState ,
9- consumerActions
9+ GlobalContext
1010} from './helper'
1111import { actionMiddlewares , applyMiddlewares , middlewares } from './middlewares'
1212
@@ -34,16 +34,16 @@ const Model = <M extends Models>(models: M, initialState?: Global['State']) => {
3434 }
3535 return {
3636 actions,
37- useStore,
38- getState,
39- getInitialState,
4037 getActions,
38+ getInitialState,
39+ getState,
4140 subscribe,
42- unsubscribe
41+ unsubscribe,
42+ useStore
4343 } as {
4444 useStore : < K extends keyof M > (
4545 name : K ,
46- depActions ?: ( keyof Get < M [ K ] , 'actions' > ) [ ]
46+ depActions ?: Array < keyof Get < M [ K ] , 'actions' > >
4747 ) => [ Get < M [ K ] , 'state' > , getConsumerActionsType < Get < M [ K ] , 'actions' > > ]
4848 getState : < K extends keyof M > ( modelName : K ) => Readonly < Get < M [ K ] , 'state' > >
4949 getActions : < K extends keyof M > (
@@ -52,12 +52,14 @@ const Model = <M extends Models>(models: M, initialState?: Global['State']) => {
5252 getInitialState : typeof getInitialState
5353 subscribe : < K extends keyof M > (
5454 modelName : K ,
55- actionName : keyof Get < M [ K ] , 'actions' > | ( keyof Get < M [ K ] , 'actions' > ) [ ] ,
56- callback : Function
55+ actionName :
56+ | keyof Get < M [ K ] , 'actions' >
57+ | Array < keyof Get < M [ K ] , 'actions' > > ,
58+ callback : ( ) => void
5759 ) => void
5860 unsubscribe : < K extends keyof M > (
5961 modelName : K ,
60- actionName : keyof Get < M [ K ] , 'actions' > | ( keyof Get < M [ K ] , 'actions' > ) [ ]
62+ actionName : keyof Get < M [ K ] , 'actions' > | Array < keyof Get < M [ K ] , 'actions' > >
6163 ) => void
6264 actions : {
6365 [ K in keyof M ] : Readonly < getConsumerActionsType < Get < M [ K ] , 'actions' > > >
@@ -72,7 +74,7 @@ const unsubscribe = (modelName: string, actions: string | string[]) => {
7274const subscribe = (
7375 modelName : string ,
7476 actions : string | string [ ] ,
75- callback ?: Function
77+ callback ?: ( ) => void
7678) => {
7779 if ( Array . isArray ( actions ) ) {
7880 actions . forEach ( actionName => {
@@ -110,15 +112,15 @@ const getActions = (
110112 ( [ key , action ] ) =>
111113 ( updaters [ key ] = async ( params : any , middlewareConfig ?: any ) => {
112114 const context : InnerContext = {
113- modelName ,
115+ action ,
114116 actionName : key ,
117+ consumerActions,
118+ middlewareConfig,
119+ modelName,
115120 newState : null ,
116121 params,
117- middlewareConfig,
118- consumerActions,
119- action : action ,
120- Global,
121- ...baseContext
122+ ...baseContext ,
123+ Global
122124 }
123125 await applyMiddlewares ( actionMiddlewares , context )
124126 } )
@@ -129,13 +131,14 @@ const getActions = (
129131const useStore = ( modelName : string , depActions ?: string [ ] ) => {
130132 const setState = useState ( Global . State [ modelName ] ) [ 1 ]
131133 Global . uid += 1
132- const _hash = '' + Global . uid
133- if ( ! Global . Setter . functionSetter [ modelName ] )
134+ const hash = '' + Global . uid
135+ if ( ! Global . Setter . functionSetter [ modelName ] ) {
134136 Global . Setter . functionSetter [ modelName ] = { }
135- Global . Setter . functionSetter [ modelName ] [ _hash ] = { setState, depActions }
137+ }
138+ Global . Setter . functionSetter [ modelName ] [ hash ] = { setState, depActions }
136139 useEffect ( ( ) => {
137140 return function cleanup ( ) {
138- delete Global . Setter . functionSetter [ modelName ] [ _hash ]
141+ delete Global . Setter . functionSetter [ modelName ] [ hash ]
139142 }
140143 } )
141144 const updaters = getActions ( modelName , { setState, type : 'function' } )
0 commit comments