diff --git a/src/global.ts b/src/global.ts index 943242a..f249c82 100644 --- a/src/global.ts +++ b/src/global.ts @@ -1,6 +1,6 @@ -let State: Global['State'] = {} -let Actions: Global['Actions'] = {} -let AsyncState: Global['AsyncState'] = {} +let State = {} +let Actions = {} +let AsyncState = {} // Communicate between Provider-Consumer and Hooks let Setter: Setter = { // classSetter stores the setState from Provider, invoke the classSetter.setState can update the state of Global Provider. @@ -9,10 +9,10 @@ let Setter: Setter = { functionSetter: {} } -let subscriptions: Subscriptions = {} +let subscriptions = {} let devTools: any -let withDevTools: boolean = false +let withDevTools = false let uid = Math.random() // The unique id of hooks @@ -25,4 +25,4 @@ export default { withDevTools, uid, subscriptions -} +} as Global diff --git a/src/helper.ts b/src/helper.ts index 411a7ed..3bb44a7 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -28,6 +28,7 @@ const consumerAction = ( type: 'outer', modelName: modelContext.modelName, actionName: action.name, + Global, newState: null, params, middlewareConfig, diff --git a/src/index.d.ts b/src/index.d.ts index 7bebd5f..71ead64 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -24,6 +24,11 @@ interface Global { AsyncState: { [modelName: string]: undefined | ((context?: any) => Promise>) } + subscriptions: Subscriptions + Setter: Setter + devTools: any + withDevTools: boolean + uid: number } type ClassSetter = React.Dispatch | undefined @@ -67,6 +72,7 @@ interface BaseContext { modelName: string next?: Function newState: Global['State'] | Function | null + Global: Global } interface InnerContext extends BaseContext { diff --git a/src/index.tsx b/src/index.tsx index d738f76..00e9ced 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -70,6 +70,7 @@ const getActions = (modelName: string, baseContext: Partial) => { middlewareConfig, consumerActions, action: action, + Global, ...baseContext } await applyMiddlewares(actionMiddlewares, context) diff --git a/src/middlewares.ts b/src/middlewares.ts index 711150f..0c9cd4b 100644 --- a/src/middlewares.ts +++ b/src/middlewares.ts @@ -1,14 +1,13 @@ -import Global from './global' import { setPartialState, timeout, getCache } from './helper' // -- Middlewares -- -const tryCatch: Middleware<{}> = async (context, restMiddlewares) => { +const tryCatch: Middleware = async (context, restMiddlewares) => { const { next } = context await next(restMiddlewares).catch((e: any) => console.log(e)) } -const getNewState: Middleware<{}> = async (context, restMiddlewares) => { - const { action, modelName, consumerActions, params, next } = context +const getNewState: Middleware = async (context, restMiddlewares) => { + const { action, modelName, consumerActions, params, next, Global } = context context.newState = (await action( Global.State[modelName], @@ -24,6 +23,7 @@ const getNewStateWithCache = (maxTime: number = 5000): Middleware => async ( ) => { const { action, + Global, modelName, consumerActions, params, @@ -51,7 +51,7 @@ const setNewState: Middleware = async (context, restMiddlewares) => { } const stateUpdater: Middleware = async (context, restMiddlewares) => { - const { modelName, next } = context + const { modelName, next, Global } = context context.type === 'function' && context.setState && context.setState(Global.State[modelName]) @@ -59,7 +59,7 @@ const stateUpdater: Middleware = async (context, restMiddlewares) => { } const subscription: Middleware = async (context, restMiddlewares) => { - const { modelName, actionName, next } = context + const { modelName, actionName, next, Global } = context if (Global.subscriptions[`${modelName}_${actionName}`]) { Global.subscriptions[`${modelName}_${actionName}`]() } @@ -67,6 +67,7 @@ const subscription: Middleware = async (context, restMiddlewares) => { } const consoleDebugger: Middleware = async (context, restMiddlewares) => { + const { Global } = context console.group( `%c ${ context.modelName @@ -94,6 +95,7 @@ const consoleDebugger: Middleware = async (context, restMiddlewares) => { } const devToolsListener: Middleware = async (context, restMiddlewares) => { + const { Global } = context await context.next(restMiddlewares) if (Global.withDevTools) { Global.devTools.send( @@ -103,8 +105,8 @@ const devToolsListener: Middleware = async (context, restMiddlewares) => { } } -const communicator: Middleware<{}> = async (context, restMiddlewares) => { - const { modelName, next, actionName } = context +const communicator: Middleware = async (context, restMiddlewares) => { + const { modelName, next, actionName, Global } = context if (Global.Setter.classSetter) { Global.Setter.classSetter(Global.State) }