Update async API to fix state closure bug - 0.4.0 #21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Addresses the issue where two hook actions are invoked simultaneously and having state closed for both, thus the non-first reducer to be fired with stale state (overriding the n-1th state mutations).
As a side-effect, it means we cannot directly support
async
HookActions that mutate state. Instead, we now supportasync
by enforcing that the promise will resolve with a reducer callback function. In this way,async
actions can be guaranteed that thestate
passed to their reducer callback will not be stale.Example:
Invoking
actions.foo()
will return immediately and not mutate the current state. Internally, the promise will block for 3 seconds, then return a state reducer mutates the current state to have a propertyfoo
with value "foo".The last change in this PR is that
this.state
is no longer available (it was closed anyway) and all HookActions must use the passedstate
variable (always the last arg).