Skip to content

Commit a98f4cd

Browse files
authored
Merge pull request #81 from byte-fe/refactor/global
refactor(middleware): add Global to context
2 parents e7e1820 + 53d708f commit a98f4cd

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

src/global.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
let State: Global['State'] = {}
2-
let Actions: Global['Actions'] = {}
3-
let AsyncState: Global['AsyncState'] = {}
1+
let State = {}
2+
let Actions = {}
3+
let AsyncState = {}
44
// Communicate between Provider-Consumer and Hooks
55
let Setter: Setter = {
66
// classSetter stores the setState from Provider, invoke the classSetter.setState can update the state of Global Provider.
@@ -9,10 +9,10 @@ let Setter: Setter = {
99
functionSetter: {}
1010
}
1111

12-
let subscriptions: Subscriptions = {}
12+
let subscriptions = {}
1313

1414
let devTools: any
15-
let withDevTools: boolean = false
15+
let withDevTools = false
1616

1717
let uid = Math.random() // The unique id of hooks
1818

@@ -25,4 +25,4 @@ export default {
2525
withDevTools,
2626
uid,
2727
subscriptions
28-
}
28+
} as Global

src/helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const consumerAction = (
2828
type: 'outer',
2929
modelName: modelContext.modelName,
3030
actionName: action.name,
31+
Global,
3132
newState: null,
3233
params,
3334
middlewareConfig,

src/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ interface Global {
2424
AsyncState: {
2525
[modelName: string]: undefined | ((context?: any) => Promise<Partial<any>>)
2626
}
27+
subscriptions: Subscriptions
28+
Setter: Setter
29+
devTools: any
30+
withDevTools: boolean
31+
uid: number
2732
}
2833

2934
type ClassSetter = React.Dispatch<any> | undefined
@@ -67,6 +72,7 @@ interface BaseContext<S = {}> {
6772
modelName: string
6873
next?: Function
6974
newState: Global['State'] | Function | null
75+
Global: Global
7076
}
7177

7278
interface InnerContext<S = {}> extends BaseContext<S> {

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const getActions = (modelName: string, baseContext: Partial<Context>) => {
7070
middlewareConfig,
7171
consumerActions,
7272
action: action,
73+
Global,
7374
...baseContext
7475
}
7576
await applyMiddlewares(actionMiddlewares, context)

src/middlewares.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import Global from './global'
21
import { setPartialState, timeout, getCache } from './helper'
32
// -- Middlewares --
43

5-
const tryCatch: Middleware<{}> = async (context, restMiddlewares) => {
4+
const tryCatch: Middleware = async (context, restMiddlewares) => {
65
const { next } = context
76
await next(restMiddlewares).catch((e: any) => console.log(e))
87
}
98

10-
const getNewState: Middleware<{}> = async (context, restMiddlewares) => {
11-
const { action, modelName, consumerActions, params, next } = context
9+
const getNewState: Middleware = async (context, restMiddlewares) => {
10+
const { action, modelName, consumerActions, params, next, Global } = context
1211
context.newState =
1312
(await action(
1413
Global.State[modelName],
@@ -24,6 +23,7 @@ const getNewStateWithCache = (maxTime: number = 5000): Middleware => async (
2423
) => {
2524
const {
2625
action,
26+
Global,
2727
modelName,
2828
consumerActions,
2929
params,
@@ -51,22 +51,23 @@ const setNewState: Middleware = async (context, restMiddlewares) => {
5151
}
5252

5353
const stateUpdater: Middleware = async (context, restMiddlewares) => {
54-
const { modelName, next } = context
54+
const { modelName, next, Global } = context
5555
context.type === 'function' &&
5656
context.setState &&
5757
context.setState(Global.State[modelName])
5858
await next(restMiddlewares)
5959
}
6060

6161
const subscription: Middleware = async (context, restMiddlewares) => {
62-
const { modelName, actionName, next } = context
62+
const { modelName, actionName, next, Global } = context
6363
if (Global.subscriptions[`${modelName}_${actionName}`]) {
6464
Global.subscriptions[`${modelName}_${actionName}`]()
6565
}
6666
await next(restMiddlewares)
6767
}
6868

6969
const consoleDebugger: Middleware = async (context, restMiddlewares) => {
70+
const { Global } = context
7071
console.group(
7172
`%c ${
7273
context.modelName
@@ -94,6 +95,7 @@ const consoleDebugger: Middleware = async (context, restMiddlewares) => {
9495
}
9596

9697
const devToolsListener: Middleware = async (context, restMiddlewares) => {
98+
const { Global } = context
9799
await context.next(restMiddlewares)
98100
if (Global.withDevTools) {
99101
Global.devTools.send(
@@ -103,8 +105,8 @@ const devToolsListener: Middleware = async (context, restMiddlewares) => {
103105
}
104106
}
105107

106-
const communicator: Middleware<{}> = async (context, restMiddlewares) => {
107-
const { modelName, next, actionName } = context
108+
const communicator: Middleware = async (context, restMiddlewares) => {
109+
const { modelName, next, actionName, Global } = context
108110
if (Global.Setter.classSetter) {
109111
Global.Setter.classSetter(Global.State)
110112
}

0 commit comments

Comments
 (0)