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

Multiple domain handlers #401

Closed nhunzaker closed 7 years ago

nhunzaker commented 7 years ago

This commit allows Domains and Effects to provide a list of handlers.

Domains Reduce

These handlers reduce over state. This should allow domain handlers to be even simpler operations.

This comes really in handle for sorting behavior:

import { sortBy } from 'lodash'

class Widgets {
  getInitialState() {
    return []
  }
  add(items, widget) {
    return items.concat(widget)
  }
  sort(items) {
    return sortBy(items, 'id')
  }
  register () {
    return {
      [createWidget]: [this.add, this.sort]
    }
  }
}

Effects Iterate

A list of handlers given to an effect are called sequentially, however there is no reducing behavior:

class MyEffect {
  register() {
    return {
      [createWidget]: [
        (repo, action) => log(action.status),
        (repo, action) => log(action.payload),
      ]
    }
  }
}

Presenters do not chain

intercept() only allows one callback. I don't actually know what to do here, should it reduce? If it just iterates, what do we return back from send?

codecov-io commented 7 years ago

Codecov Report

Merging #401 into master will increase coverage by <.01%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #401      +/-   ##
==========================================
+ Coverage   99.77%   99.77%   +<.01%     
==========================================
  Files          25       25              
  Lines         898      906       +8     
==========================================
+ Hits          896      904       +8     
  Misses          2        2
Impacted Files Coverage Δ
src/get-registration.js 100% <100%> (ø) :arrow_up:
src/effect-engine.js 100% <100%> (ø) :arrow_up:
src/domain-engine.js 100% <100%> (ø) :arrow_up:
src/addons/presenter.js 100% <100%> (ø) :arrow_up:
src/utils.js 100% <100%> (ø) :arrow_up:

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 cb69479...d2f5db6. Read the comment docs.

mackermedia commented 7 years ago

Presenter.intercept seems like an okay thing to not reduce/chain. It's saying "hey I want to do something about the event/data here" which can have its own async or nested operations

mackermedia commented 7 years ago

Neat-o! 👍

efatsi commented 7 years ago

👍