koax
Build apps by decoupling effects from application logic. Inspired by co, koa, redux and cycle.
The basic idea is that your main
can yield
effects to your interperter stack. Actions are dispatched to main by either dispatching directly or by adding drivers to the effects stack, which dispatch to main.
The basic building block of a koax app is a koax
. A koax
is a generator that processes actions and is composed of koax
middleware.
At the outer most level, koax apps should basically have the same form.
let dispatch =
Effects and main are composed of koax middleware.
let effects = ...
Koax middleware looks like koa middleware, with three params: action
, next
, and ctx
.
{ if actiontype === FETCH return return }
Effects will be added to the interpreter stack, which includes default control flow middlware:
The action creators for these middleware are exposed by koax. They include: fork
, delay
, join
, and cancel
.
Now we can create our main and process it using the interpreter. Remember we want our app to be a koax. We can create it using koax to compose middleware or with a simple generator
{ if evttype === REQUEST && evtmethod ==== 'get' return
Putting it all together (again)...
let dispatch = // results
Installation
$ npm install koax
Router
A router example.
app.js
let effects = let main = moduleexports = { return } { }
For an http server, we could do:
server.js
let server = server
API
koax() (default)
Returns: a koax middleware stack
.use(middleware)
middleware
- add middleware to koax app
Returns: koax app
run(effects, main, ctx)
effects
- effects processing stackmain
- main function or generator, signature:main(action)
ctx
- ctx to pass to effects and main middleware
Returns: a function that interprets data passed to it. data can be an action or a koax app.
Concepts
middleware(action, next, ctx)
action
- an action that middleware can process (preferably treat as immutable)next
- a function that passes execution to next middleware (canyield
orreturn
)ctx
- global shared context for all middleware
Returns: whatever your heart desires or next()
to defer to the next middleware
{ // return a simple value if actiontype === 'FOO' return 'bar' // dispatch FETCH and return its result if actiontype === 'QUX' return type: 'FETCH' payload: 'google' // pass execution to next middleware return }
next()
next
is simply a function that calls the next middleware with action
and next
already bound. Since koax handles generators that are yielded or returned, each middleware can be either a function or generator and they will work as expected.
yield
yield
dispatches actions to the interpreter. The interpreter is composed of the default control flow middleware and the effects stack.
License
MIT