Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update async API to fix state closure bug - 0.4.0 #21

Merged

Conversation

msmith-techempower
Copy link
Member

@msmith-techempower msmith-techempower commented May 30, 2019

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 support async by enforcing that the promise will resolve with a reducer callback function. In this way, async actions can be guaranteed that the state passed to their reducer callback will not be stale.

Example:

const contract = {
  async foo() {
    const foo = "foo";
    await new Promise(resolve => setTimeout(() => resolve(), 3000));

    return state => ({
      ...state,
      foo
    });
  }
};

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 property foo 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 passed state variable (always the last arg).

@NateBrady23 NateBrady23 changed the title WIP Update async API to fix state closure bug - 0.4.0 May 30, 2019
@NateBrady23 NateBrady23 merged commit 3c40374 into TechEmpower:master May 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants