Skip to content

Commit

Permalink
feat: optionally allow dispatching the current route by disabling the…
Browse files Browse the repository at this point in the history
… preventDoubleDispatch middleware
  • Loading branch information
hedgepigdaniel committed Oct 1, 2019
1 parent 51ff994 commit 5c7a03a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/rudy/src/core/createRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
pathlessRoute,
anonymousThunk,
transformAction,
preventDoubleDispatch,
call,
enter,
changePageTitle,
Expand All @@ -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 }),
Expand Down
1 change: 1 addition & 0 deletions packages/rudy/src/middleware/index.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
5 changes: 5 additions & 0 deletions packages/rudy/src/middleware/preventDoubleDispatch.js
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 0 additions & 2 deletions packages/rudy/src/middleware/transformAction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 5c7a03a

Please sign in to comment.