-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add time travelling support (#15)
Now listen to state changes in mobx-state-tree and do history.replace with the new history location state. This allows the location to also update when time travelling using mobx-state-tree.
- Loading branch information
Showing
7 changed files
with
108 additions
and
45 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,45 @@ | ||
import { reaction } from 'mobx'; | ||
|
||
/** | ||
* Sync the history object with the given mst router store | ||
* @param {object} history - 'History' instance to subscribe to | ||
* @param {object} history - 'History' instance to subscribe and sync to | ||
* @param {object} store - Router store instance to sync with the history changes | ||
*/ | ||
export const syncHistoryWithStore = (history, store) => { | ||
store._setHistory(history); | ||
|
||
function isLocationEqual(locationA, locationB) { | ||
return ( | ||
locationA && | ||
locationB && | ||
locationA.key && | ||
locationB.key && | ||
locationA.key === locationB.key | ||
); | ||
} | ||
|
||
// Handle update from history object | ||
const handleLocationChange = location => { | ||
store._updateLocation({ ...location }); | ||
if (!isLocationEqual(store.location, location)) { | ||
store._updateLocation({ ...location }); | ||
} | ||
}; | ||
const unsubscribeFromHistory = history.listen(handleLocationChange); | ||
handleLocationChange(history.location); | ||
const unsubscribeFromStoreLocation = reaction( | ||
() => store.location, | ||
location => { | ||
if (!isLocationEqual(history.location, location)) { | ||
history.replace({ ...location }); | ||
} | ||
} | ||
); | ||
|
||
history.unsubscribe = unsubscribeFromHistory; | ||
history.unsubscribe = () => { | ||
unsubscribeFromHistory(); | ||
unsubscribeFromStoreLocation(); | ||
}; | ||
|
||
handleLocationChange(history.location); | ||
|
||
return history; | ||
}; |
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
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