Skip to content

Commit

Permalink
fix: pass not found state to middleware functions on start
Browse files Browse the repository at this point in the history
BREAKING CHAGE: when using  option, custom middleware functions will now be passed not found states.
  • Loading branch information
troch committed Jun 23, 2017
1 parent 0eeb7bb commit 0d0bf75
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 55 deletions.
12 changes: 8 additions & 4 deletions packages/router5/modules/core/router-lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ export default function withRouterLifecycle(router) {
{ replace: true, reload: true },
done
);
// If matched start path
if (startState) {
const transitionToState = state => {
router.transitionToState(
startState,
state,
router.getState(),
{},
(err, state) => {
Expand All @@ -98,11 +97,16 @@ export default function withRouterLifecycle(router) {
else cb(err, null, false);
}
);
};

// If matched start path
if (startState) {
transitionToState(startState);
} else if (options.defaultRoute) {
// If default, navigate to default
navigateToDefault();
} else if (options.allowNotFound) {
cb(null, router.makeNotFoundState(startPath));
transitionToState(router.makeNotFoundState(startPath));
} else {
// No start match, no default => do nothing
cb({ code: errorCodes.ROUTE_NOT_FOUND, path: startPath }, null);
Expand Down
114 changes: 63 additions & 51 deletions packages/router5/modules/transition/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import transitionPath, { nameToIDs } from 'router5-transition-path';
import resolve from './resolve';
import { errorCodes } from '../constants';
import constants, { errorCodes } from '../constants';

export default transition;

Expand Down Expand Up @@ -42,54 +42,8 @@ function transition(router, toState, fromState, opts, callback) {
...(err instanceof Object ? err : { error: err })
});

const { toDeactivate, toActivate } = transitionPath(toState, fromState);
const asyncBase = { isCancelled, toState, fromState };

const canDeactivate = (toState, fromState, cb) => {
let canDeactivateFunctionMap = toDeactivate
.filter(name => canDeactivateFunctions[name])
.reduce(
(fnMap, name) => ({
...fnMap,
[name]: canDeactivateFunctions[name]
}),
{}
);

resolve(
canDeactivateFunctionMap,
{ ...asyncBase, errorKey: 'segment' },
err =>
cb(
err
? makeError({ code: errorCodes.CANNOT_DEACTIVATE }, err)
: null
)
);
};

const canActivate = (toState, fromState, cb) => {
const canActivateFunctionMap = toActivate
.filter(name => canActivateFunctions[name])
.reduce(
(fnMap, name) => ({
...fnMap,
[name]: canActivateFunctions[name]
}),
{}
);

resolve(
canActivateFunctionMap,
{ ...asyncBase, errorKey: 'segment' },
err =>
cb(
err
? makeError({ code: errorCodes.CANNOT_ACTIVATE }, err)
: null
)
);
};
let pipeline;

const middleware = !middlewareFunctions.length
? []
Expand All @@ -103,9 +57,67 @@ function transition(router, toState, fromState, opts, callback) {
)
);

let pipeline = (fromState && !opts.forceDeactivate ? [canDeactivate] : [])
.concat(canActivate)
.concat(middleware);
if (toState.name === constants.UNKNOWN_ROUTE) {
pipeline = middleware;
} else {
const { toDeactivate, toActivate } = transitionPath(toState, fromState);

const canDeactivate = (toState, fromState, cb) => {
let canDeactivateFunctionMap = toDeactivate
.filter(name => canDeactivateFunctions[name])
.reduce(
(fnMap, name) => ({
...fnMap,
[name]: canDeactivateFunctions[name]
}),
{}
);

resolve(
canDeactivateFunctionMap,
{ ...asyncBase, errorKey: 'segment' },
err =>
cb(
err
? makeError(
{ code: errorCodes.CANNOT_DEACTIVATE },
err
)
: null
)
);
};

const canActivate = (toState, fromState, cb) => {
const canActivateFunctionMap = toActivate
.filter(name => canActivateFunctions[name])
.reduce(
(fnMap, name) => ({
...fnMap,
[name]: canActivateFunctions[name]
}),
{}
);

resolve(
canActivateFunctionMap,
{ ...asyncBase, errorKey: 'segment' },
err =>
cb(
err
? makeError(
{ code: errorCodes.CANNOT_ACTIVATE },
err
)
: null
)
);
};

pipeline = (fromState && !opts.forceDeactivate ? [canDeactivate] : [])
.concat(canActivate)
.concat(middleware);
}

resolve(pipeline, asyncBase, done);

Expand Down

0 comments on commit 0d0bf75

Please sign in to comment.