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 the ability to alias actions #406

Closed nhunzaker closed 7 years ago

nhunzaker commented 7 years ago

This change allows actions to be aliased to a given string. When the string name is not recognized, Microcosm raises an assertion error in strict mode.

In order to prevent this from being a breaking change, string actions that are pushed into a repo that are unrecognized through aliasing still work. However I would like to eventually send out a minor release where this is deprecated, and throw an exception in the next major release.

The benefit of this change is that we can use it to know about available actions and prevent the need to import actions in places like ActionButtons, something I know @mackermedia will love :). It also makes it so that you do not need to use computed keys for actions. Domain registration can actually happen without a function, like so:

let repo = new Microcosm()

repo.addDomain('planets', {
  getInitialState() {
    return []
  },

  actions: { addPlanet: name => name },

  register: {
    addPlanet: (planets, params) => planets.concat(params)
  }
})

I need this for my GraphQL Microcosm work in order to know what Mutation types are valid, and to support some new debugger features.

Fixes #405

codecov-io commented 7 years ago

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #406      +/-   ##
==========================================
+ Coverage   99.78%   99.78%   +<.01%     
==========================================
  Files          26       26              
  Lines         933      942       +9     
==========================================
+ Hits          931      940       +9     
  Misses          2        2
Impacted Files Coverage Δ
src/coroutine.js 100% <ø> (ø) :arrow_up:
src/get-registration.js 100% <ø> (ø) :arrow_up:
src/effect-engine.js 100% <100%> (ø) :arrow_up:
src/domain-engine.js 100% <100%> (ø) :arrow_up:
src/microcosm.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 24dac29...056aa83. Read the comment docs.

mackermedia commented 7 years ago

I probably don't have enough context around the codebase to know if this is A+ and I'm still not up to speed on flow so this is a bit hard to review.

That said, I think the concept and tests are :+1:

efatsi commented 7 years ago

NIce 👍