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

[microcosm] Unify effect and domain behavior through agent class #500

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

This commit adds a new class to describe the dispatch behavior: an agent. Agents receive action dispatches from history and can manage their own internal state:

import { start, stop } from '../actions/gps'

class GPS extends Agent () {
  setup() {
    this.listen()
  }

  listen() {
    this.watcher = navigator.geolocation.watchPosition(this.next)
  }

  ignore() {
    navigator.geolocation.clearWatch(this.watcher);
  }

  receive(action) {
    switch(action) {
      case start: 
        return this.listen()
      case stop: 
        return this.ignore()
      default: 
        return null
    }
  }
}

let gps = new GPS()

gps.subscribe(next => console.log(next)) // location.... location .... location....

gps.send(start)

I still haven't figured out the public API for Agents yet, but I think they will be a useful escape hatch for creating new types of Domain/Effect-like abstractions. I'm also curious if they could be used as a type of viewmodel class, but I'm not quite there yet.

codecov-io commented 6 years ago

Codecov Report

Merging #500 into master will decrease coverage by 2.78%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #500      +/-   ##
==========================================
- Coverage      98%   95.21%   -2.79%     
==========================================
  Files          29       27       -2     
  Lines         702      669      -33     
  Branches      137      130       -7     
==========================================
- Hits          688      637      -51     
- Misses         12       29      +17     
- Partials        2        3       +1
Impacted Files Coverage Δ
packages/microcosm/src/ledger.js 100% <ø> (+11.11%) :arrow_up:
packages/microcosm/src/microcosm.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/agent.js 100% <100%> (ø)
packages/microcosm/src/effect.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/subject.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/domain.js 100% <100%> (+2.08%) :arrow_up:
packages/microcosm/src/registry.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/history.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/proto.js 100% <100%> (ø)
packages/microcosm-dom/test/engines/react.setup.js
... and 7 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 d6492c5...1e37997. Read the comment docs.

efatsi commented 6 years ago

+1 to the refactoring here. Personally, I'm not too interested in a public API for Agents - I can't think of a use case for cutting off your own class that extends Agent when you have Domains/Effects at your fingertips.

nhunzaker commented 6 years ago

@dce Looks like I was missing coverage in a few areas. In 1e37997a I added a few more tests and added an assertion to warn users when they add a null domain/effect. This achieves complete coverage of the proto module.

nhunzaker commented 6 years ago

@efatsi Agreed. I don't want to advertise it for the time being, but I am quite curious about how we could use it to create domains that do not rely on history.

nhunzaker commented 6 years ago

Thanks everyone!