Skip to content

Instantly share code, notes, and snippets.

@ezekielchentnik
Last active December 28, 2015 11:25
Show Gist options
  • Save ezekielchentnik/261fef0f2f892ffce533 to your computer and use it in GitHub Desktop.
Save ezekielchentnik/261fef0f2f892ffce533 to your computer and use it in GitHub Desktop.
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