Created
February 7, 2020 06:20
-
-
Save ezekielchentnik/416e3e47babc980e6b6ae65b00e94843 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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