-
Notifications
You must be signed in to change notification settings - Fork 639
Description
Bug report
- I've checked documentation and searched for existing issues and discussions
- I've made sure my project is based on the latest MST version
- Fork this code sandbox or another minimal reproduction.
Sandbox link or minimal reproduction code
- Clone this repo: https://github.com/ChristopherGabba/BugHuntingForFrankAndTyler.git
- Create expo development build via simulator or via actual device:
npx eas build --profile development --platform ios - Run
npx expo install -cin terminal - The ignite boilerplate app will open up. Tap
Sign in - Go to the
PodcastsTab at the bottom - Tap on any
Toggle Favoritebutton for any podcast. I hard coded this:
export const EpisodeStoreModel = types
.model("EpisodeStore")
.props({
episodes: types.array(EpisodeModel),
favorites: types.array(
types.safeReference(EpisodeModel, {
onInvalidated(event) {
console.log("INVALIDATED", JSON.stringify(event, null, 4))
},
}),
),
favoritesOnly: false,
})with this:
addFavorite(episode: Episode) {
const bsEpisode: ReferenceIdentifier[] = ["BS_EPISODE_REFERENCE"]
store.setProp("favorites", bsEpisode)
},Describe the expected behavior
I would expect the onInvalidated hook to call and remove the bad reference immediately instead of crashing the app.
I've SEVERELY struggled with this behavior with MST with regard to references, and that's why I migrated to safeReference so that it wouldn't crash. The app should not crash when a bad reference arrives, it should handle gracefully (aka hit the invalidated function call).
Real Life Scenario:
- Fetch data from backend
- Data was modified by another user (lets say a Todo action and assigned it to another user that isn't in the store)
- Fetch data again
- Bad reference arrives and app crashes
Describe the observed behavior
The app crashes immediately.
You'll notice after you add the bad reference, the app is stuck in an eternal cycle of instant crashes because the bad reference is just stuck in the store, and it forces the dev to have to clear the store. When you have about 10 reference situations in an app like mine, that is so annoying to have to do.
In my app, what I've had to do is:
addFavorite(episode: Episode) {
const bsEpisodes: ReferenceIdentifier[] = ["BS_EPISODE_REFERENCE"]
// Install a function that parses episodes first and checks for valid references
const validEpisodes = checkEpisodesForValidReferences(bsEpisodes)
store.setProp("favorites", validEpisodes)
},And I do not enjoy having to do this, and frankly feel as though I shouldn't have to if the onInvalidated function was working correctly. If a reference is bad from the backend, you have to send something to modify the backend to make sure it doesn't just keep hitting the client with a bad reference over and over again, and the onInvalidated hook was where I handle this..
There has to be a better way of dealing with this.
As always, thanks so much for looking into this, I love MST (minus the reference stuff) :)