Can I rely on all the Cmd
s that I return from update
being dispatched (not neccessarily completed) before update
is called again?
Specifically I have a situation like this…
- Application state is persisted both in localStorage and in the cloud, so as to allow syncing between devices.
- Therefore my application needs to compare version data between what is stored in the localStorage and what is stored in the cloud.
- Cloud storage is accessed via a simple API using
Http
commands, but requires an access key that occasionally changes. - To prevent always asking for the access key, it is stored in localStorage together with the state data, so that it only needs to be requested from the user when it has changed.
Thus when the access key has changed, my application flow looks like this:
Pass 1
update
receives the state stored in localStorage and discovers the access key is out of date.
view
provides an input box for the user to enter the new access key.
Pass 2
update
receives the new access key from the user and dispatches two batched Cmd
s. One to fetch stored state from the cloud API using the new access key, the other to update the currently stored state in localStorage with the new access key.
Pass 3
update
receives a reply from the cloud API with the application state stored there. It compares the version number of this state with that retrieved from the localStorage and (a) if it is more recent dispatches another Cmd
to update localStorage with the state retrieved from the cloud or (b) if it is less recent dispatches a Cmd
to update the cloud storage with the more recent state.
The Concern
Is it possible that Pass 3 commences before all the Cmd
s from Pass 2 have been dispatched? That is: could it happen that the Cmd
to the cloud API is dispatched and triggers a new Msg
before the Cmd
to localStorage from Pass 2 is dispatched, and that this latter Cmd
then just gets batched together with the next set of Cmd
s from Pass 3? (In which case there would be a race condition as to which update is the last to get written to localStorage) Or is it always guaranteed (and will continue to be in future versions) that Cmd
s from one update
call will all be dispatched before calling update
again?
Obvious workaround
I realise that in the case presented, a race condition (were it possible) could easily be avoided by storing the access key and application state in separate fields of the localStorage. However, I envision there must be situations where such a workaround would be much more difficult. So, having thought of the possibility, wanted to find out whether there are any guarantees about Cmd
dispatch.