vigetlabs / microcosm

Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.
http://code.viget.com/microcosm/
MIT License
487 stars 29 forks source link

ActionForm and ActionButton actions should unsubscribe on mount #432

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

Before this commit, there was an issue where ActionForm and ActionButton callbacks would still fire if the related components unmounted. This resulted in an error where setState would call on an unmounted component for most of the use cases for these callbacks.

Basically:

const testAction = function() {
  return Promise.resolve(true)
}

class Test extends React.Component {
  state = {
    status: 'inactive'
  }

  onNext = ({ status }) => {
    this.setState({ status })
  }

  render() {
    return <ActionForm action={testAction} onNext={this.onNext} />
  }
}

let form = ReactDOM.render(<Test />, el)

form.submit()

ReactDOM.unmountComponentAtNode(el)

// Action callbacks are still firing. onNext will raise a setState error in React when the action completes.

This commit does a couple of things:

  1. Adds Action::execute: Moves coroutine behavior to the Action prototype
  2. Adds action._origin. Actions know what repo they were created with. This is necessary for execute, but could be used for other future work.
  3. ActionQueue: A helper class for entities that must dispatch and track multiple actions.

The result of this work is that ActionForm and ActionButton now "empty" their queue of actions when they unmount, unsubscribing any callbacks they set on those actions.


https://github.com/vigetlabs/microcosm/issues/425

codecov-io commented 6 years ago

Codecov Report

Merging #432 into master will decrease coverage by 0.6%. The diff coverage is 40%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #432      +/-   ##
==========================================
- Coverage   84.59%   83.98%   -0.61%     
==========================================
  Files          44       45       +1     
  Lines        1318     1349      +31     
  Branches      242      246       +4     
==========================================
+ Hits         1115     1133      +18     
- Misses        172      180       +8     
- Partials       31       36       +5
Impacted Files Coverage Δ
packages/microcosm/src/addons/action-button.js 0% <0%> (-100%) :arrow_down:
packages/microcosm/src/addons/action-queue.js 0% <0%> (ø)
packages/microcosm/src/action.js 100% <100%> (ø) :arrow_up:
packages/microcosm-preact/src/action-form.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/microcosm.js 100% <100%> (ø) :arrow_up:
packages/microcosm-preact/src/action-button.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/addons/action-form.js 97.61% <100%> (+97.61%) :arrow_up:
packages/microcosm/src/addons/jest-matchers.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/history.js 100% <100%> (ø) :arrow_up:
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 7369bb5...7e5c150. Read the comment docs.