Same question, but regarding the XState example.
What makes the cancelation logic work? Does the onDone function not get invoked if the src promise has resolved but we have since transitioned to a different state?
Ok, so after debugging the sandbox a bit I think I get it...
The cleanup function (that turns canceled into true) only runs when state changes from loading to something else (because it is only returned in the loading state).
So... if we've changed from loading to idle before the promise has returned, when it returns the canceled flag will be true and it will return without doing anything.
I do however feel that this logic kind of goes against what this entire post is trying to advocate: declarative, easy to understand logic.
I'm wondering if maybe there's a more "state machiney" way to implement this functionality (without going full on state machine like in the last example)
I also stumbled over this example and agree that explicit cancelation would make the app logic easier to understand. Implicit cancelation feels too close to the isLoadingΒ from the very first example.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi David, Thanks a lot for this write up.
One thing that wasn't very clear to me is the cancelling logic in the reducer example.
The cleanup function is inside the
if (state.status === "loading")block. So how is it still being invoked when status changes to "idle"? (due to a cancel event)
How does the cleanup variable persist across renders?
In general i'd love a few words on cancelation logic since it doesn't look very trivial.
Thanks again!
Same question, but regarding the XState example.
What makes the cancelation logic work? Does the
onDonefunction not get invoked if thesrcpromise has resolved but we have since transitioned to a different state?I had this question too, and eventually found the answer in the docs
The
invokeproperty seems like a little bit of "magic" in XState and it took me a while to understand what is actually happening there.David, thanks for writing this, having a concrete example really helped understand XState and I look forward to seeing more.
Ok, so after debugging the sandbox a bit I think I get it...
The cleanup function (that turns
canceledintotrue) only runs when state changes fromloadingto something else (because it is only returned in the loading state).So... if we've changed from
loadingtoidlebefore the promise has returned, when it returns the canceled flag will be true and it will return without doing anything.I do however feel that this logic kind of goes against what this entire post is trying to advocate: declarative, easy to understand logic.
I'm wondering if maybe there's a more "state machiney" way to implement this functionality (without going full on state machine like in the last example)
Yes there is, and Reason has done it - reasonml.github.io/reason-react/do...
I also stumbled over this example and agree that explicit cancelation would make the app logic easier to understand. Implicit cancelation feels too close to the
isLoadingΒ from the very first example.