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

[microcosm] Always, synchronously, emit the first observer.next() #516

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

This PR updates Observable such that the first "next" callback invokes immediately. This is important for Observable::map, particularly when binding data in React. Otherwise, React renders before the scheduler clears the queue for an observable. So you'll end up with something like:

class UserShow extends Presenter {
  getModel(repo, { id }) {
    let { users } = repo.domains

    return {
      user: users.map(users => find(users, { id })
    }
  }
  render() {
    console.log(this.model.user) 
    // Always undefined initially because the observable callback is still
    // in the scheduler
  }
}

Essentially, this makes it so that you can always get a value immediately, even if the initial payload is empty.

codecov-io commented 6 years ago

Codecov Report

Merging #516 into master will decrease coverage by 0.25%. The diff coverage is 90%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #516      +/-   ##
==========================================
- Coverage   98.78%   98.52%   -0.26%     
==========================================
  Files          30       30              
  Lines         738      748      +10     
  Branches      147      150       +3     
==========================================
+ Hits          729      737       +8     
- Misses          9       11       +2
Impacted Files Coverage Δ
packages/microcosm/src/domain.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/observable.js 94.54% <88.88%> (-1.54%) :arrow_down:

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 7c4c0d7...dcb6501. Read the comment docs.

efatsi commented 6 years ago

👍 thanks for all the inline comments on the more obscure things

nhunzaker commented 6 years ago

Thanks, guys! @zporter I've added a note about that case, as you requested.