microcosm
microcosm copied to clipboard
Cleaning up the generator action interface
Generator actions are quickly becoming my favorite form of writing actions:
function createUserFlow (params) {
return function * (repo) {
yield repo.push(validate, params)
let user = yield repo.push(createUser, '/users')
yield repo.push(navigate, '/users/' + user.id)
}
}
This feels heavy. I wonder if we could clean this up by adding a layer of abstraction:
// 1. A general "send" helper, where you don't need to know about the repo at all
import { send } from 'microcosm'
// 2. Actions themselves can be generators, not just returning a function
function * createUserFlow (params) {
yield send(validate, params)
let user = yield send(createUser, '/users')
yield send(navigate, '/users/' + user.id)
}
Under the hood, send might do something like:
function send (action, ...params) {
return repo => repo.push(action, ...params)
}
We'd pass repo to the action inside of Microcosm.
seems like a good idea to me