From 5c7a03a40a02493c3e88b61b40bc6e3426775a15 Mon Sep 17 00:00:00 2001 From: Daniel Playfair Cal Date: Tue, 1 Oct 2019 17:00:08 +1000 Subject: [PATCH] feat: optionally allow dispatching the current route by disabling the preventDoubleDispatch middleware --- packages/rudy/src/core/createRouter.js | 2 ++ packages/rudy/src/middleware/index.js | 1 + packages/rudy/src/middleware/preventDoubleDispatch.js | 5 +++++ packages/rudy/src/middleware/transformAction/index.js | 2 -- 4 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 packages/rudy/src/middleware/preventDoubleDispatch.js diff --git a/packages/rudy/src/core/createRouter.js b/packages/rudy/src/core/createRouter.js index f0e8ab19a..f49f73b61 100644 --- a/packages/rudy/src/core/createRouter.js +++ b/packages/rudy/src/core/createRouter.js @@ -28,6 +28,7 @@ import { pathlessRoute, anonymousThunk, transformAction, + preventDoubleDispatch, call, enter, changePageTitle, @@ -41,6 +42,7 @@ export default ( anonymousThunk, pathlessRoute('thunk'), transformAction, // pipeline starts here + preventDoubleDispatch, // Hydrate: skip callbacks called on server to produce initialState (beforeEnter, thunk, etc) // Server: don't allow client-centric callbacks (onEnter, onLeave, beforeLeave) call('beforeLeave', { prev: true }), diff --git a/packages/rudy/src/middleware/index.js b/packages/rudy/src/middleware/index.js index 97dafd3e2..335378b32 100644 --- a/packages/rudy/src/middleware/index.js +++ b/packages/rudy/src/middleware/index.js @@ -1,4 +1,5 @@ export { default as transformAction } from './transformAction' +export { default as preventDoubleDispatch } from './preventDoubleDispatch' export { default as enter } from './enter' export { default as call } from './call' diff --git a/packages/rudy/src/middleware/preventDoubleDispatch.js b/packages/rudy/src/middleware/preventDoubleDispatch.js new file mode 100644 index 000000000..ad7dcbe80 --- /dev/null +++ b/packages/rudy/src/middleware/preventDoubleDispatch.js @@ -0,0 +1,5 @@ +export default () => (req, next) => { + if (!req.route.path) return next() + if (req.isDoubleDispatch()) return req.handleDoubleDispatch() // don't dispatch the same action twice + return next() +} diff --git a/packages/rudy/src/middleware/transformAction/index.js b/packages/rudy/src/middleware/transformAction/index.js index 31ff98906..407bb1a69 100644 --- a/packages/rudy/src/middleware/transformAction/index.js +++ b/packages/rudy/src/middleware/transformAction/index.js @@ -5,8 +5,6 @@ export default () => (req, next) => { req.action = formatAction(req) - if (req.isDoubleDispatch()) return req.handleDoubleDispatch() // don't dispatch the same action twice - const { type, params, query, state, hash, basename, location } = req.action Object.assign(req, { type, params, query, state, hash, basename, location }) // assign to `req` for conevenience (less destructuring in callbacks)