Skip to content

Instantly share code, notes, and snippets.

@ezekielchentnik
Created February 7, 2020 06:20
Show Gist options
  • Save ezekielchentnik/416e3e47babc980e6b6ae65b00e94843 to your computer and use it in GitHub Desktop.
Save ezekielchentnik/416e3e47babc980e6b6ae65b00e94843 to your computer and use it in GitHub Desktop.
// we augment the window.history instance so that it will dispatch an event when using push/replace
// this allows us to create event stream and listen to all history state changes
export const patchHistory = history => {
const wrap = m => (state, title, url) => {
m.call(history, state, title, url);
let e = new Event(m.name.toLowerCase());
e.state = state;
dispatchEvent(e);
};
history.pushState = wrap(history.pushState);
history.replaceState = wrap(history.replaceState);
return history;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment