Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand All @@ -25,4 +25,4 @@ export default {
withDevTools,
uid,
subscriptions
}
} as Global
1 change: 1 addition & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const consumerAction = (
type: 'outer',
modelName: modelContext.modelName,
actionName: action.name,
Global,
newState: null,
params,
middlewareConfig,
Expand Down
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ interface Global {
AsyncState: {
[modelName: string]: undefined | ((context?: any) => Promise<Partial<any>>)
}
subscriptions: Subscriptions
Setter: Setter
devTools: any
withDevTools: boolean
uid: number
}

type ClassSetter = React.Dispatch<any> | undefined
Expand Down Expand Up @@ -67,6 +72,7 @@ interface BaseContext<S = {}> {
modelName: string
next?: Function
newState: Global['State'] | Function | null
Global: Global
}

interface InnerContext<S = {}> extends BaseContext<S> {
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const getActions = (modelName: string, baseContext: Partial<Context>) => {
middlewareConfig,
consumerActions,
action: action,
Global,
...baseContext
}
await applyMiddlewares(actionMiddlewares, context)
Expand Down
18 changes: 10 additions & 8 deletions src/middlewares.ts
Original file line number Diff line number Diff line change
@@ -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],
Expand All @@ -24,6 +23,7 @@ const getNewStateWithCache = (maxTime: number = 5000): Middleware => async (
) => {
const {
action,
Global,
modelName,
consumerActions,
params,
Expand Down Expand Up @@ -51,22 +51,23 @@ 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])
await next(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}`]()
}
await next(restMiddlewares)
}

const consoleDebugger: Middleware = async (context, restMiddlewares) => {
const { Global } = context
console.group(
`%c ${
context.modelName
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
Expand Down