redux-simple-router
Advanced tools
Comparing version 0.0.7 to 0.0.8
"use strict"; | ||
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; }; | ||
// constants | ||
var UPDATE_PATH = "@@router/UPDATE_PATH"; | ||
var SELECT_STATE = function SELECT_STATE(state) { | ||
return state.routing; | ||
}; | ||
@@ -19,3 +24,6 @@ // Action creator | ||
var initialState = {}; | ||
var initialState = typeof window === 'undefined' ? {} : { | ||
path: locationToString(window.location) | ||
}; | ||
function update() { | ||
@@ -26,3 +34,3 @@ var state = arguments.length <= 0 || arguments[0] === undefined ? initialState : arguments[0]; | ||
if (action.type === UPDATE_PATH) { | ||
return Object.assign({}, state, { | ||
return _extends({}, state, { | ||
path: action.path, | ||
@@ -42,10 +50,16 @@ noRouterUpdate: action.noRouterUpdate | ||
function syncReduxAndRouter(history, store) { | ||
if (!store.getState().routing) { | ||
throw new Error("Cannot sync router: route state does not exist. Did you " + "install the reducer under the name `routing`?"); | ||
var selectRouterState = arguments.length <= 2 || arguments[2] === undefined ? SELECT_STATE : arguments[2]; | ||
var getRouterState = function getRouterState() { | ||
return selectRouterState(store.getState()); | ||
}; | ||
if (!getRouterState()) { | ||
throw new Error("Cannot sync router: route state does not exist. Did you " + "install the routing reducer?"); | ||
} | ||
history.listen(function (location) { | ||
var unsubscribeHistory = history.listen(function (location) { | ||
// Avoid dispatching an action if the store is already up-to-date, | ||
// even if `history` wouldn't do anthing if the location is the same | ||
if (store.getState().routing.path !== locationToString(location)) { | ||
// even if `history` wouldn't do anything if the location is the same | ||
if (getRouterState().path !== locationToString(location)) { | ||
store.dispatch(updatePath(locationToString(location))); | ||
@@ -55,6 +69,7 @@ } | ||
store.subscribe(function () { | ||
var routing = store.getState().routing; | ||
var unsubscribeStore = store.subscribe(function () { | ||
var routing = getRouterState(); | ||
// Don't update the router if nothing has changed. The | ||
// `avoidRouterUpdate` flag can be set to avoid updating altogether, | ||
// `noRouterUpdate` flag can be set to avoid updating altogether, | ||
// which is useful for things like loading snapshots or very special | ||
@@ -66,2 +81,7 @@ // edge cases. | ||
}); | ||
return function unsubscribe() { | ||
unsubscribeHistory(); | ||
unsubscribeStore(); | ||
}; | ||
} | ||
@@ -68,0 +88,0 @@ |
{ | ||
"name": "redux-simple-router", | ||
"version": "0.0.7", | ||
"description": "Ruthlessly simple bindings to keep react-router and redux in syncy", | ||
"version": "0.0.8", | ||
"description": "Ruthlessly simple bindings to keep react-router and redux in sync", | ||
"main": "lib/index", | ||
@@ -15,3 +15,3 @@ "repository": { | ||
"scripts": { | ||
"build": "mkdir -p lib && babel ./src/index.js --presets babel-preset-es2015 --out-file ./lib/index.js", | ||
"build": "mkdir -p lib && babel ./src/index.js --plugins transform-object-assign --presets babel-preset-es2015 --out-file ./lib/index.js", | ||
"prepublish": "npm run build" | ||
@@ -30,4 +30,5 @@ }, | ||
"babel-cli": "^6.1.2", | ||
"babel-plugin-transform-object-assign": "^6.0.14", | ||
"babel-preset-es2015": "^6.1.2" | ||
} | ||
} |
@@ -6,3 +6,3 @@ | ||
[react-router](https://github.com/rackt/react-router) is neat. The | ||
problem is that react-router manages an important piece of your piece | ||
problem is that react-router manages an important piece | ||
of your application state: the URL. If you are using redux, you want | ||
@@ -123,3 +123,3 @@ your app state to fully represent your UI; if you snapshotted the app | ||
### `syncReduxAndRouter(history, store)` | ||
### `syncReduxAndRouter(history, store, selectRouterState?)` | ||
@@ -130,2 +130,7 @@ Call this with a react-router and a redux store instance to install | ||
Supply an optional function `selectRouterState` to customize where to | ||
find the router state on your app state. It defaults to `state => | ||
state.routing`, so you would install the reducer under the name | ||
"routing". Feel free to change this to whatever you like. | ||
### `routeReducer` | ||
@@ -147,5 +152,5 @@ | ||
The `noRouterUpdate`, if `true`, will stop react-router from reacting | ||
to this and all future URL changes. Pass `true` to make it start | ||
to this and all future URL changes. Pass `false` to make it start | ||
reacting again. This is useful if replaying snapshots while using the | ||
`forceRefresh` option of the browser history which forces full | ||
reloads. It's a rare edge case. |
@@ -5,2 +5,3 @@ | ||
const UPDATE_PATH = "@@router/UPDATE_PATH"; | ||
const SELECT_STATE = state => state.routing; | ||
@@ -19,3 +20,6 @@ // Action creator | ||
const initialState = {}; | ||
const initialState = typeof window === 'undefined' ? {} : { | ||
path: locationToString(window.location) | ||
}; | ||
function update(state=initialState, action) { | ||
@@ -37,14 +41,16 @@ if(action.type === UPDATE_PATH) { | ||
function syncReduxAndRouter(history, store) { | ||
if(!store.getState().routing) { | ||
function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) { | ||
const getRouterState = () => selectRouterState(store.getState()); | ||
if(!getRouterState()) { | ||
throw new Error( | ||
"Cannot sync router: route state does not exist. Did you " + | ||
"install the reducer under the name `routing`?" | ||
"install the routing reducer?" | ||
); | ||
} | ||
history.listen(location => { | ||
const unsubscribeHistory = history.listen(location => { | ||
// Avoid dispatching an action if the store is already up-to-date, | ||
// even if `history` wouldn't do anthing if the location is the same | ||
if(store.getState().routing.path !== locationToString(location)) { | ||
// even if `history` wouldn't do anything if the location is the same | ||
if(getRouterState().path !== locationToString(location)) { | ||
store.dispatch(updatePath(locationToString(location))); | ||
@@ -54,6 +60,7 @@ } | ||
store.subscribe(() => { | ||
const routing = store.getState().routing; | ||
const unsubscribeStore = store.subscribe(() => { | ||
const routing = getRouterState(); | ||
// Don't update the router if nothing has changed. The | ||
// `avoidRouterUpdate` flag can be set to avoid updating altogether, | ||
// `noRouterUpdate` flag can be set to avoid updating altogether, | ||
// which is useful for things like loading snapshots or very special | ||
@@ -66,2 +73,7 @@ // edge cases. | ||
}); | ||
return function unsubscribe() { | ||
unsubscribeHistory(); | ||
unsubscribeStore(); | ||
}; | ||
} | ||
@@ -68,0 +80,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11715
135
153
3