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

Domains can be defined as observables #487

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

This commit allows a domain to be a simple observable. Observable domains do not have a ledger and can not respond to actions. Their primary use case is to synchronize state with some other source. Like:

  1. Window history
  2. Websocket state
  3. Geolocation

For these cases, you mostly just want to push an action to synchronize state, but that's a ton of boilerplate. Instead, you could do:

import { history } from 'push-state-lib'

repo.addDomain('location', new Observable(observer => {
  observer.next(history.getLocation())

  let listener = history.listen(location => observer.next(location)

  // The return value of an observable's callback is a clean-up function
  // sort of like Domain::teardown
  return () => listener.remove()
})

repo.domains.location.subscribe(location => {
  console.log("The next location is", location)
})
codecov-io commented 6 years ago

Codecov Report

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

Impacted file tree graph

@@           Coverage Diff           @@
##             master   #487   +/-   ##
=======================================
  Coverage          ?    98%           
=======================================
  Files             ?     29           
  Lines             ?    701           
  Branches          ?    136           
=======================================
  Hits              ?    687           
  Misses            ?     12           
  Partials          ?      2
Impacted Files Coverage Δ
packages/microcosm/src/ledger.js 88.88% <0%> (ø)
packages/microcosm/src/microcosm.js 100% <100%> (ø)
packages/microcosm/src/domain.js 97.91% <100%> (ø)
packages/microcosm/src/subject.js 100% <100%> (ø)
packages/microcosm/src/observable.js 97.72% <75%> (ø)

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 945e928...526cd71. Read the comment docs.