-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmiddlewares.ts
257 lines (241 loc) · 6.66 KB
/
middlewares.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { getCache, setPartialState, timeout, shallowEqual } from './helper'
// -- Middlewares --
const config: MiddlewareConfig = {
logger: {
enable: process.env.NODE_ENV !== 'production'
},
devtools: {
enable: process.env.NODE_ENV !== 'production'
},
tryCatch: {
enable: process.env.NODE_ENV === 'production'
}
}
const tryCatch: Middleware = async (context, restMiddlewares) => {
const { next } = context
if (config.tryCatch.enable) {
return await next(restMiddlewares).catch((e: any) => console.log(e))
} else {
return await next(restMiddlewares)
}
}
const getNewState: Middleware = async (context, restMiddlewares) => {
const {
action,
modelName,
consumerActions,
params,
next,
Global,
type
} = context
if (type === 'u') {
// @ts-ignore
context.newState = action()
} else {
context.newState =
(await action(params, {
actions: consumerActions(Global.Actions[modelName], { modelName }),
state: Global.State[modelName],
...(Global.Context['__global'] || {}),
...(Global.Context[modelName] || {})
})) || null
}
return await next(restMiddlewares)
}
const getNewStateWithCache = (maxTime: number = 5000): Middleware => async (
context,
restMiddlewares
) => {
const {
action,
Global,
modelName,
consumerActions,
params,
next,
actionName
} = context
context.newState =
(await Promise.race([
action(params, {
actions: consumerActions(Global.Actions[modelName], { modelName }),
state: Global.State[modelName]
}),
timeout(maxTime, getCache(modelName, actionName))
])) || null
return await next(restMiddlewares)
}
const setNewState: Middleware = async (context, restMiddlewares) => {
const {
modelName,
newState,
next,
Global,
disableSelectorUpdate,
type
} = context
if (Global.Setter.functionSetter[modelName] && !disableSelectorUpdate) {
Object.keys(Global.Setter.functionSetter[modelName]).map((key) => {
const setter = Global.Setter.functionSetter[modelName][key]
if (setter) {
if (setter.selector && !setter.selectorRef) {
setter.selectorRef = setter.selector(Global.State[modelName])
}
}
})
}
if (newState || type === 'u') {
setPartialState(modelName, newState || {})
return await next(restMiddlewares)
}
}
const stateUpdater: Middleware = async (context, restMiddlewares) => {
const { modelName, next, Global, __hash } = context
const setter = Global.Setter.functionSetter[modelName]
if (
context.type === 'f' &&
__hash &&
setter &&
setter[__hash] &&
setter[__hash].setState
) {
setter[__hash].setState(Global.State[modelName])
}
return await next(restMiddlewares)
}
const subscription: Middleware = async (context, restMiddlewares) => {
const { modelName, actionName, next, Global } = context
const subscriptions =
context.type === 'u'
? Global.subscriptions[modelName]
: Global.subscriptions[`${modelName}_${actionName}`]
if (subscriptions) {
subscriptions.forEach((callback) => {
callback(context)
})
}
return await next(restMiddlewares)
}
const consoleDebugger: Middleware = async (context, restMiddlewares) => {
const { Global } = context
if (
config.logger.enable === true ||
(typeof config.logger.enable === 'function' &&
config.logger.enable(context))
) {
console.group(
`%c ${
context.modelName
} State Change %c ${new Date().toLocaleTimeString()}`,
'color: gray; font-weight: lighter;',
'color: black; font-weight: bold;'
)
console.log(
'%c Previous',
`color: #9E9E9E; font-weight: bold`,
Global.State[context.modelName]
)
console.log(
'%c Action',
`color: #03A9F4; font-weight: bold`,
context.actionName,
`payload: ${context.params}`
)
const ret = await context.next(restMiddlewares)
console.log(
'%c Next',
`color: #4CAF50; font-weight: bold`,
Global.State[context.modelName]
)
console.groupEnd()
return ret
} else {
return await context.next(restMiddlewares)
}
}
const devToolsListener: Middleware = async (context, restMiddlewares) => {
const { Global } = context
const ret = await context.next(restMiddlewares)
Global.withDevTools =
typeof window !== 'undefined' &&
(window as any).__REDUX_DEVTOOLS_EXTENSION__
if (
Global.withDevTools &&
middlewares.config.devtools.enable &&
!Global.devTools
) {
Global.devTools = (window as any).__REDUX_DEVTOOLS_EXTENSION__
Global.devTools.connect()
}
if (Global.withDevTools && config.devtools.enable) {
const actionName =
context.type === 'u' && context.disableSelectorUpdate
? `store[${context.modelName}].update`
: `${context.modelName}_${context.actionName}`
Global.devTools.send(
actionName,
context.type === 'u' ? Global.mutableState[context.modelName] : Global.State[context.modelName],
undefined,
context.modelName
)
}
return ret
}
const communicator: Middleware = async (context, restMiddlewares) => {
const { modelName, next, Global, disableSelectorUpdate } = context
if (Global.Setter.classSetter) {
Global.Setter.classSetter(Global.State)
}
if (Global.Setter.functionSetter[modelName]) {
Object.keys(Global.Setter.functionSetter[modelName]).map((key) => {
const setter = Global.Setter.functionSetter[modelName][key]
if (setter) {
if (!setter.selector || disableSelectorUpdate) {
setter.setState(Global.State[modelName])
} else {
const newSelectorRef = setter.selector(Global.State[modelName])
if (!shallowEqual(newSelectorRef, setter.selectorRef)) {
setter.selectorRef = newSelectorRef
setter.setState(Global.State[modelName])
}
}
}
})
}
return await next(restMiddlewares)
}
const actionMiddlewares = [
tryCatch,
consoleDebugger,
devToolsListener,
getNewState,
setNewState,
stateUpdater,
communicator,
subscription
]
const middlewares = {
communicator,
consoleDebugger,
devToolsListener,
getNewState,
getNewStateWithCache,
setNewState,
stateUpdater,
subscription,
tryCatch,
config
}
const applyMiddlewares = async (
middlewares: Middleware[],
context: BaseContext
) => {
context.next = (restMiddlewares: Middleware[]) => {
if (restMiddlewares.length > 0)
return restMiddlewares[0](context as Context, restMiddlewares.slice(1))
else return context.newState
}
return await middlewares[0](context as Context, middlewares.slice(1))
}
export { actionMiddlewares, applyMiddlewares, middlewares }