microcosm icon indicating copy to clipboard operation
microcosm copied to clipboard

Cleaning up the generator action interface

Open nhunzaker opened this issue 8 years ago • 1 comments

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.

nhunzaker avatar Jun 14 '17 12:06 nhunzaker

seems like a good idea to me

efatsi avatar Jun 14 '17 15:06 efatsi