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

Add Observable::map #485

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

This commit adds the map method to Observables. This is useful for transforming a stream of values over time, necessary to fully replace the old style of building models with Presenters:

class Namer extends Presenter {
  getModel(repo, { prefix }) {
    return {
      name: repo.domains.name.map(name => prefix + ' ' + name)
    }
  }
  render() {
    return <p>{this.model.name}</p>
  }
}

ReactDOM.render(<Namer prefix="Mr." />) // "Mr. Smith"

Long term, I kind of wonder if we should just pass domains as the first argument of Presenter::getModel, then this would just be:

class Namer extends Presenter {
  getModel({ name },  { prefix }) {
    return {
      name: name.map(name => prefix + ' ' + name)
    }
  }
  render() {
    return <p>{this.model.name}</p>
  }
}

You could also call domain methods directly, maybe something like:

// in Name domain
class Name extends Domain {
  withPrefix(prefix) {
    return this.map(name => `${prefix} ${name}`)
  }
}

class Namer extends Presenter {
  getModel({ name }, { prefix }) {
    return {
      name: name.withPrefix(prefix)
    }
  }
  // ...
}
codecov-io commented 6 years ago

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #485   +/-   ##
=========================================
  Coverage          ?   97.99%           
=========================================
  Files             ?       29           
  Lines             ?      698           
  Branches          ?      135           
=========================================
  Hits              ?      684           
  Misses            ?       12           
  Partials          ?        2
Impacted Files Coverage Δ
packages/microcosm/src/ledger.js 88.88% <0%> (ø)
packages/microcosm/src/domain.js 97.95% <100%> (ø)
packages/microcosm/src/microcosm.js 100% <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 ee1bb57...dc068c8. Read the comment docs.