react-router-redux
Version:
Ruthlessly simple bindings to keep react-router and redux in sync
453 lines (368 loc) • 14.6 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["ReactRouterRedux"] = factory();
else
root["ReactRouterRedux"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* This action type will be dispatched by the history actions below.
* If you're writing a middleware to watch for navigation events, be sure to
* look for actions of this type.
*/
var CALL_HISTORY_METHOD = exports.CALL_HISTORY_METHOD = '@@router/CALL_HISTORY_METHOD';
function updateLocation(method) {
return function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return {
type: CALL_HISTORY_METHOD,
payload: { method: method, args: args }
};
};
}
/**
* These actions correspond to the history API.
* The associated routerMiddleware will capture these events before they get to
* your reducer and reissue them as the matching function on your history.
*/
var push = exports.push = updateLocation('push');
var replace = exports.replace = updateLocation('replace');
var go = exports.go = updateLocation('go');
var goBack = exports.goBack = updateLocation('goBack');
var goForward = exports.goForward = updateLocation('goForward');
var routerActions = exports.routerActions = { push: push, replace: replace, go: go, goBack: goBack, goForward: goForward };
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.routerReducer = routerReducer;
/**
* This action type will be dispatched when your history
* receives a location change.
*/
var LOCATION_CHANGE = exports.LOCATION_CHANGE = '@@router/LOCATION_CHANGE';
var initialState = {
locationBeforeTransitions: null
};
/**
* This reducer will update the state with the most recent location history
* has transitioned to. This may not be in sync with the router, particularly
* if you have asynchronously-loaded routes, so reading from and relying on
* this state is discouraged.
*/
function routerReducer() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
type = _ref.type,
payload = _ref.payload;
if (type === LOCATION_CHANGE) {
return _extends({}, state, { locationBeforeTransitions: payload });
}
return state;
}
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports['default'] = routerMiddleware;
var _actions = __webpack_require__(0);
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
/**
* This middleware captures CALL_HISTORY_METHOD actions to redirect to the
* provided history object. This will prevent these actions from reaching your
* reducer or any middleware that comes after this one.
*/
function routerMiddleware(history) {
return function () {
return function (next) {
return function (action) {
if (action.type !== _actions.CALL_HISTORY_METHOD) {
return next(action);
}
var _action$payload = action.payload,
method = _action$payload.method,
args = _action$payload.args;
history[method].apply(history, _toConsumableArray(args));
};
};
};
}
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports['default'] = syncHistoryWithStore;
var _reducer = __webpack_require__(1);
var defaultSelectLocationState = function defaultSelectLocationState(state) {
return state.routing;
};
/**
* This function synchronizes your history state with the Redux store.
* Location changes flow from history to the store. An enhanced history is
* returned with a listen method that responds to store updates for location.
*
* When this history is provided to the router, this means the location data
* will flow like this:
* history.push -> store.dispatch -> enhancedHistory.listen -> router
* This ensures that when the store state changes due to a replay or other
* event, the router will be updated appropriately and can transition to the
* correct router state.
*/
function syncHistoryWithStore(history, store) {
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref$selectLocationSt = _ref.selectLocationState,
selectLocationState = _ref$selectLocationSt === undefined ? defaultSelectLocationState : _ref$selectLocationSt,
_ref$adjustUrlOnRepla = _ref.adjustUrlOnReplay,
adjustUrlOnReplay = _ref$adjustUrlOnRepla === undefined ? true : _ref$adjustUrlOnRepla;
// Ensure that the reducer is mounted on the store and functioning properly.
if (typeof selectLocationState(store.getState()) === 'undefined') {
throw new Error('Expected the routing state to be available either as `state.routing` ' + 'or as the custom expression you can specify as `selectLocationState` ' + 'in the `syncHistoryWithStore()` options. ' + 'Ensure you have added the `routerReducer` to your store\'s ' + 'reducers via `combineReducers` or whatever method you use to isolate ' + 'your reducers.');
}
var initialLocation = void 0;
var isTimeTraveling = void 0;
var unsubscribeFromStore = void 0;
var unsubscribeFromHistory = void 0;
var currentLocation = void 0;
// What does the store say about current location?
var getLocationInStore = function getLocationInStore(useInitialIfEmpty) {
var locationState = selectLocationState(store.getState());
return locationState.locationBeforeTransitions || (useInitialIfEmpty ? initialLocation : undefined);
};
// Init initialLocation with potential location in store
initialLocation = getLocationInStore();
// If the store is replayed, update the URL in the browser to match.
if (adjustUrlOnReplay) {
var handleStoreChange = function handleStoreChange() {
var locationInStore = getLocationInStore(true);
if (currentLocation === locationInStore || initialLocation === locationInStore) {
return;
}
// Update address bar to reflect store state
isTimeTraveling = true;
currentLocation = locationInStore;
history.transitionTo(_extends({}, locationInStore, {
action: 'PUSH'
}));
isTimeTraveling = false;
};
unsubscribeFromStore = store.subscribe(handleStoreChange);
handleStoreChange();
}
// Whenever location changes, dispatch an action to get it in the store
var handleLocationChange = function handleLocationChange(location) {
// ... unless we just caused that location change
if (isTimeTraveling) {
return;
}
// Remember where we are
currentLocation = location;
// Are we being called for the first time?
if (!initialLocation) {
// Remember as a fallback in case state is reset
initialLocation = location;
// Respect persisted location, if any
if (getLocationInStore()) {
return;
}
}
// Tell the store to update by dispatching an action
store.dispatch({
type: _reducer.LOCATION_CHANGE,
payload: location
});
};
unsubscribeFromHistory = history.listen(handleLocationChange);
// History 3.x doesn't call listen synchronously, so fire the initial location change ourselves
if (history.getCurrentLocation) {
handleLocationChange(history.getCurrentLocation());
}
// The enhanced history uses store as source of truth
return _extends({}, history, {
// The listeners are subscribed to the store instead of history
listen: function listen(listener) {
// Copy of last location.
var lastPublishedLocation = getLocationInStore(true);
// Keep track of whether we unsubscribed, as Redux store
// only applies changes in subscriptions on next dispatch
var unsubscribed = false;
var unsubscribeFromStore = store.subscribe(function () {
var currentLocation = getLocationInStore(true);
if (currentLocation === lastPublishedLocation) {
return;
}
lastPublishedLocation = currentLocation;
if (!unsubscribed) {
listener(lastPublishedLocation);
}
});
// History 2.x listeners expect a synchronous call. Make the first call to the
// listener after subscribing to the store, in case the listener causes a
// location change (e.g. when it redirects)
if (!history.getCurrentLocation) {
listener(lastPublishedLocation);
}
// Let user unsubscribe later
return function () {
unsubscribed = true;
unsubscribeFromStore();
};
},
// It also provides a way to destroy internal listeners
unsubscribe: function unsubscribe() {
if (adjustUrlOnReplay) {
unsubscribeFromStore();
}
unsubscribeFromHistory();
}
});
}
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.routerMiddleware = exports.routerActions = exports.goForward = exports.goBack = exports.go = exports.replace = exports.push = exports.CALL_HISTORY_METHOD = exports.routerReducer = exports.LOCATION_CHANGE = exports.syncHistoryWithStore = undefined;
var _reducer = __webpack_require__(1);
Object.defineProperty(exports, 'LOCATION_CHANGE', {
enumerable: true,
get: function get() {
return _reducer.LOCATION_CHANGE;
}
});
Object.defineProperty(exports, 'routerReducer', {
enumerable: true,
get: function get() {
return _reducer.routerReducer;
}
});
var _actions = __webpack_require__(0);
Object.defineProperty(exports, 'CALL_HISTORY_METHOD', {
enumerable: true,
get: function get() {
return _actions.CALL_HISTORY_METHOD;
}
});
Object.defineProperty(exports, 'push', {
enumerable: true,
get: function get() {
return _actions.push;
}
});
Object.defineProperty(exports, 'replace', {
enumerable: true,
get: function get() {
return _actions.replace;
}
});
Object.defineProperty(exports, 'go', {
enumerable: true,
get: function get() {
return _actions.go;
}
});
Object.defineProperty(exports, 'goBack', {
enumerable: true,
get: function get() {
return _actions.goBack;
}
});
Object.defineProperty(exports, 'goForward', {
enumerable: true,
get: function get() {
return _actions.goForward;
}
});
Object.defineProperty(exports, 'routerActions', {
enumerable: true,
get: function get() {
return _actions.routerActions;
}
});
var _sync = __webpack_require__(3);
var _sync2 = _interopRequireDefault(_sync);
var _middleware = __webpack_require__(2);
var _middleware2 = _interopRequireDefault(_middleware);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
exports.syncHistoryWithStore = _sync2['default'];
exports.routerMiddleware = _middleware2['default'];
/***/ })
/******/ ]);
});