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 22 forks source link

Pass next when completing with a payload. Improve efficiency #519

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

What

As a part of the Microcosm 13 update, actions are moving away from a custom data type and embrace the Observable standard interface. Unfortunately, that causes a few API incompatibilities that I'm trying to smooth over before a full release of 13.x.

In Microcosm 12.x, you can complete an action with a payload like:

action.resolve('stuff')

As far as I understand, with Observables, the correct way to accomplish this is something like:

observer.next('stuff')
observer.complete()

While I've respected that for Observables themselves, this PR updates the Subject API to allow passing a payload to complete. This improves ergonomics and compatibility with usage in Microcosm 12.x.

Other related work

The drawback to this approach is that an extra action status is dispatched. I have alleviated that by adding a filter method to the Observable prototype that Domains use to avoid dispatch if an action update has no impact on their state. Essentially:

// In a Domain
// History is a tree of actions that reprocess in a consistent order as they change
history 
  .filter(action => {
    // Can I process this action at all?
    return this.shouldListenTo(action)
  })
  .subscribe(action => {
    // Update the domain's state value
    this.dispatch(action)
  })

This actually ended up being a greater efficiency update than I anticipated, allowing me to unskip a few tests. Pretty neat. I've been wanting to add filter to the core API, but was wary of doing so until I could dogfood it internally. Now I can!

codecov-io commented 6 years ago

Codecov Report

:exclamation: No coverage uploaded for pull request base (master@fbd3d38). Click here to learn what that means. The diff coverage is 97.56%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #519   +/-   ##
=========================================
  Coverage          ?   93.73%           
=========================================
  Files             ?       32           
  Lines             ?      798           
  Branches          ?      162           
=========================================
  Hits              ?      748           
  Misses            ?       43           
  Partials          ?        7
Impacted Files Coverage Δ
packages/microcosm/src/ledger.js 100% <100%> (ø)
packages/microcosm/src/subject.js 100% <100%> (ø)
packages/microcosm/src/domain.js 100% <100%> (ø)
packages/microcosm/src/coroutine.js 100% <100%> (ø)
packages/microcosm/src/agent.js 100% <100%> (ø)
packages/microcosm/src/lifecycle.js 100% <100%> (ø)
packages/microcosm/src/observable.js 88.13% <94.73%> (ø)

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 fbd3d38...a5ee630. Read the comment docs.

nhunzaker commented 6 years ago

@zporter Thanks! Honestly, as stop and go as this has been, they've been super helpful for me too.

nhunzaker commented 6 years ago

Thanks again, @zporter!