-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve createReducer function
enhance #164 createReducer
- Loading branch information
Showing
1 changed file
with
21 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,36 @@ | ||
import { useReducer } from 'react'; | ||
import { useMemo, useRef, useState } from 'react'; | ||
|
||
function compose(...funcs) { | ||
if (funcs.length === 0) { | ||
return arg => arg; | ||
} | ||
|
||
if (funcs.length === 1) { | ||
return funcs[0]; | ||
} | ||
|
||
return funcs.reduce((a, b) => (...args) => a(b(...args))); | ||
function composeMiddleware(chain) { | ||
return (context, dispatch) => { | ||
return chain.reduceRight((res, middleware) => { | ||
return middleware(context)(res); | ||
}, dispatch); | ||
}; | ||
} | ||
|
||
const createReducer = (...middlewares) => (...args) => { | ||
const [state, dispatch] = useReducer(...args); | ||
|
||
const createReducer = (...middlewares) => (reducer, initialState, initializer = value => value) => { | ||
const ref = useRef(initializer(initialState)) | ||
const [, setState] = useState(ref.current) | ||
let middlewareDispatch = () => { | ||
throw new Error( | ||
'Dispatching while constructing your middleware is not allowed. ' + | ||
'Other middleware would not be applied to this dispatch.' | ||
); | ||
}; | ||
|
||
const dispatch = action => { | ||
ref.current = reducer(ref.current, action) | ||
setState(ref.current) | ||
return action; | ||
} | ||
const composedMiddleware = useMemo(() => { | ||
return composeMiddleware(middlewares); | ||
}, middlewares); | ||
const middlewareAPI = { | ||
getState: () => state, | ||
getState: () => ref.current, | ||
dispatch: (...args) => middlewareDispatch(...args), | ||
}; | ||
const chain = middlewares.map(middleware => middleware(middlewareAPI)); | ||
middlewareDispatch = compose(...chain)(dispatch); | ||
|
||
return [state, middlewareDispatch]; | ||
middlewareDispatch = composedMiddleware(middlewareAPI, dispatch); | ||
return [ref.current, middlewareDispatch]; | ||
}; | ||
|
||
export default createReducer; |