Skip to content

Instantly share code, notes, and snippets.

@ezekielchentnik
Last active December 28, 2015 11:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
const UPDATE_LOCATION = 'UPDATE_LOCATION';
import { createLocation } from 'history';
var initialState = createLocation();
export function locationReducer(state = initialState, action) {
//needs support for Immutable.js
if (action.type === UPDATE_LOCATION) {//issues w/deep merge, query ??
return Object.assign({}, state, action.payload)
}
return state;
}
export function updateLocation(location) {
return {
type: UPDATE_LOCATION,
payload: createLocation(location)
}
}
export function connectHistory(store, history) {
let currentKey;
function createUniqueKey(location){
//needs to account for location.state
return history.createPath(location);
}
const unlisten = history.listen(nextLocation => {
const { location } = store.getState();//needs support for state selection
let key = createUniqueKey(location);
currentKey = createUniqueKey(nextLocation);
if (key != currentKey) {
store.dispatch(updateLocation(nextLocation));
}
});
const unsubscribe = store.subscribe(() => {
const { location } = store.getState();
let key = createUniqueKey(location);
if (key !== currentKey) {
const method = location.action === 'REPLACE' ? 'replace' : 'push';
history[method](location);
}
});
return function unconnectHistory() {
unlisten();
unsubscribe();
};
}
const history = useQueries(createHistory)();
const location = history.createLocation(window.location);
const store = configureStore(Object.assign({}, window.__INITIAL_STATE__, {location}));
const unconnectHistory = connectHistory(store, history);
//unconnectHistory();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment