11import { getCache , setPartialState , timeout } from './helper'
22// -- Middlewares --
33
4+ const config : MiddlewareConfig = {
5+ logger : {
6+ enable : process . env . NODE_ENV !== 'production'
7+ } ,
8+ devtools : {
9+ enable : process . env . NODE_ENV !== 'production'
10+ } ,
11+ tryCatch : {
12+ enable : process . env . NODE_ENV === 'production'
13+ }
14+ }
15+
416const tryCatch : Middleware = async ( context , restMiddlewares ) => {
517 const { next } = context
6- await next ( restMiddlewares ) . catch ( ( e : any ) => console . log ( e ) )
18+ if ( config . tryCatch . enable ) {
19+ await next ( restMiddlewares ) . catch ( ( e : any ) => console . log ( e ) )
20+ } else {
21+ await next ( restMiddlewares )
22+ }
723}
824
925const getNewState : Middleware = async ( context , restMiddlewares ) => {
@@ -78,37 +94,46 @@ const subscription: Middleware = async (context, restMiddlewares) => {
7894
7995const consoleDebugger : Middleware = async ( context , restMiddlewares ) => {
8096 const { Global } = context
81- console . group (
82- `%c ${
83- context . modelName
84- } State Change %c ${ new Date ( ) . toLocaleTimeString ( ) } `,
85- 'color: gray; font-weight: lighter;' ,
86- 'color: black; font-weight: bold;'
87- )
88- console . log (
89- '%c Previous' ,
90- `color: #9E9E9E; font-weight: bold` ,
91- Global . State [ context . modelName ]
92- )
93- console . log (
94- '%c Action' ,
95- `color: #03A9F4; font-weight: bold` ,
96- context . actionName ,
97- `payload: ${ context . params } `
98- )
99- await context . next ( restMiddlewares )
100- console . log (
101- '%c Next' ,
102- `color: #4CAF50; font-weight: bold` ,
103- Global . State [ context . modelName ]
104- )
105- console . groupEnd ( )
97+
98+ if (
99+ config . logger . enable === true ||
100+ ( typeof config . logger . enable === 'function' &&
101+ config . logger . enable ( context ) )
102+ ) {
103+ console . group (
104+ `%c ${
105+ context . modelName
106+ } State Change %c ${ new Date ( ) . toLocaleTimeString ( ) } `,
107+ 'color: gray; font-weight: lighter;' ,
108+ 'color: black; font-weight: bold;'
109+ )
110+ console . log (
111+ '%c Previous' ,
112+ `color: #9E9E9E; font-weight: bold` ,
113+ Global . State [ context . modelName ]
114+ )
115+ console . log (
116+ '%c Action' ,
117+ `color: #03A9F4; font-weight: bold` ,
118+ context . actionName ,
119+ `payload: ${ context . params } `
120+ )
121+ await context . next ( restMiddlewares )
122+ console . log (
123+ '%c Next' ,
124+ `color: #4CAF50; font-weight: bold` ,
125+ Global . State [ context . modelName ]
126+ )
127+ console . groupEnd ( )
128+ } else {
129+ await context . next ( restMiddlewares )
130+ }
106131}
107132
108133const devToolsListener : Middleware = async ( context , restMiddlewares ) => {
109134 const { Global } = context
110135 await context . next ( restMiddlewares )
111- if ( Global . withDevTools ) {
136+ if ( Global . withDevTools && config . devtools . enable ) {
112137 Global . devTools . send (
113138 `${ context . modelName } _${ context . actionName } ` ,
114139 Global . State
@@ -137,20 +162,17 @@ const communicator: Middleware = async (context, restMiddlewares) => {
137162 await next ( restMiddlewares )
138163}
139164
140- let actionMiddlewares = [
165+ const actionMiddlewares = [
166+ tryCatch ,
167+ consoleDebugger ,
168+ devToolsListener ,
141169 getNewState ,
142170 setNewState ,
143171 stateUpdater ,
144172 communicator ,
145173 subscription
146174]
147175
148- if ( process . env . NODE_ENV === 'production' ) {
149- actionMiddlewares = [ tryCatch , ...actionMiddlewares ]
150- } else {
151- actionMiddlewares = [ consoleDebugger , devToolsListener , ...actionMiddlewares ]
152- }
153-
154176const middlewares = {
155177 communicator,
156178 consoleDebugger,
@@ -160,7 +182,8 @@ const middlewares = {
160182 setNewState,
161183 stateUpdater,
162184 subscription,
163- tryCatch
185+ tryCatch,
186+ config
164187}
165188
166189const applyMiddlewares = async (
0 commit comments