-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,37 +1,53 @@ | ||
import { compose } from 'redux' | ||
|
||
let allDynamicMiddlewares = [] | ||
const createDynamicMiddlewares = () => { | ||
let allDynamicMiddlewares = [] | ||
|
||
const dynamicMiddlewares = store => next => (action) => { | ||
const chain = allDynamicMiddlewares.map(middleware => middleware(store)) | ||
const enhancer = store => next => (action) => { | ||
const chain = allDynamicMiddlewares.map(middleware => middleware(store)) | ||
|
||
return compose(...chain)(next)(action) | ||
} | ||
return compose(...chain)(next)(action) | ||
} | ||
|
||
const addMiddleware = (...middlewares) => { | ||
allDynamicMiddlewares = [...allDynamicMiddlewares, ...middlewares] | ||
} | ||
const addMiddleware = (...middlewares) => { | ||
allDynamicMiddlewares = [...allDynamicMiddlewares, ...middlewares] | ||
} | ||
|
||
const removeMiddleware = (middleware) => { | ||
const index = allDynamicMiddlewares.findIndex(d => d === middleware) | ||
const removeMiddleware = (middleware) => { | ||
const index = allDynamicMiddlewares.findIndex(d => d === middleware) | ||
|
||
if (index === -1) { | ||
// eslint-disable-next-line no-console | ||
console.error('Middleware does not exist!', middleware) | ||
if (index === -1) { | ||
// eslint-disable-next-line no-console | ||
console.error('Middleware does not exist!', middleware) | ||
|
||
return | ||
return | ||
} | ||
|
||
allDynamicMiddlewares = allDynamicMiddlewares.filter((_, mdwIndex) => mdwIndex !== index) | ||
} | ||
|
||
allDynamicMiddlewares = allDynamicMiddlewares.filter((_, mdwIndex) => mdwIndex !== index) | ||
} | ||
const resetMiddlewares = () => { | ||
allDynamicMiddlewares = [] | ||
} | ||
|
||
const resetMiddlewares = () => { | ||
allDynamicMiddlewares = [] | ||
return { | ||
enhancer, | ||
addMiddleware, | ||
removeMiddleware, | ||
resetMiddlewares, | ||
} | ||
} | ||
|
||
export default dynamicMiddlewares | ||
export { | ||
const dynamicMiddlewaresInstance = createDynamicMiddlewares() | ||
|
||
export default dynamicMiddlewaresInstance.enhancer | ||
|
||
export const { | ||
addMiddleware, | ||
removeMiddleware, | ||
resetMiddlewares, | ||
} = dynamicMiddlewaresInstance | ||
|
||
export { | ||
createDynamicMiddlewares, | ||
} |