Skip to content

Commit

Permalink
feat: add terrible API for bypassing routing updates (#880)
Browse files Browse the repository at this point in the history
This allows updating the location so that state/queries are present on POP or refresh but doesn't force a match
  • Loading branch information
jquense committed Jan 10, 2022
1 parent e92972e commit 08b4e99
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/createMatchEnhancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@ import { applyMiddleware } from 'redux';

import ActionTypes from './ActionTypes';

function createMatchMiddleware(matcher) {
return function matchMiddleware() {
function createMatchMiddleware(matcher, getFound) {
return function matchMiddleware(store) {
return (next) => (action) => {
const { type, payload } = action;
if (type !== FarceActionTypes.UPDATE_LOCATION) {
return next(action);
}

return next({
type: ActionTypes.UPDATE_MATCH,
payload: {
let matchPayload;
if (!payload.doNotRerunMatch) {
matchPayload = {
location: payload,
...matcher.match(payload),
},
};
} else {
// HAX: this is terrible, but sometimes you need to update the location without updating the routing world
// so we mutate the current match which will keep it referencially the same and pass checks but update the location
// on it
const { match } = getFound(store.getState());
match.location = payload;
matchPayload = match;
}

return next({
type: ActionTypes.UPDATE_MATCH,
payload: matchPayload,
});
};
};
Expand All @@ -29,7 +41,7 @@ export default function createMatchEnhancer(
return function matchEnhancer(createStore) {
return (...args) => {
const middlewareEnhancer = applyMiddleware(
createMatchMiddleware(matcher),
createMatchMiddleware(matcher, getFound),
);

const store = middlewareEnhancer(createStore)(...args);
Expand Down

0 comments on commit 08b4e99

Please sign in to comment.