UNPKG

react-router

Version:
77 lines (64 loc) 2.28 kB
'use strict'; exports.__esModule = true; var _PatternUtils = require('./PatternUtils'); function routeParamsChanged(route, prevState, nextState) { if (!route.path) return false; var paramNames = (0, _PatternUtils.getParamNames)(route.path); return paramNames.some(function (paramName) { return prevState.params[paramName] !== nextState.params[paramName]; }); } /** * Returns an object of { leaveRoutes, changeRoutes, enterRoutes } determined by * the change from prevState to nextState. We leave routes if either * 1) they are not in the next state or 2) they are in the next state * but their params have changed (i.e. /users/123 => /users/456). * * leaveRoutes are ordered starting at the leaf route of the tree * we're leaving up to the common parent route. enterRoutes are ordered * from the top of the tree we're entering down to the leaf route. * * changeRoutes are any routes that didn't leave or enter during * the transition. */ function computeChangedRoutes(prevState, nextState) { var prevRoutes = prevState && prevState.routes; var nextRoutes = nextState.routes; var leaveRoutes = void 0, changeRoutes = void 0, enterRoutes = void 0; if (prevRoutes) { (function () { var parentIsLeaving = false; leaveRoutes = prevRoutes.filter(function (route) { if (parentIsLeaving) { return true; } else { var isLeaving = nextRoutes.indexOf(route) === -1 || routeParamsChanged(route, prevState, nextState); if (isLeaving) parentIsLeaving = true; return isLeaving; } }); // onLeave hooks start at the leaf route. leaveRoutes.reverse(); enterRoutes = []; changeRoutes = []; nextRoutes.forEach(function (route) { var isNew = prevRoutes.indexOf(route) === -1; var paramsChanged = leaveRoutes.indexOf(route) !== -1; if (isNew || paramsChanged) enterRoutes.push(route);else changeRoutes.push(route); }); })(); } else { leaveRoutes = []; changeRoutes = []; enterRoutes = nextRoutes; } return { leaveRoutes: leaveRoutes, changeRoutes: changeRoutes, enterRoutes: enterRoutes }; } exports.default = computeChangedRoutes; module.exports = exports['default'];