Skip to content

koaxjs/run

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

run

Build status Git tag NPM version Code style

Run a koax middleware stack.

This is the raw middleware stack runner. The basic principal of the runner is that values that are yielded in the middleware stack are dispatched back through the middleware stack to get resolved. yielded functors (arrays, generators, and iterators) aren't dispatched directly - instead the functors values are mapped, using predefined mapping functions, over the middleware stack.

Installation

$ npm install @koax/run

Usage

import run from '@koax/run'

let dispatch = run([
  function * (action, next) {
    if (action === 'foo') return 'bar'
    else if (action === 'qux') return yield 'foo'
    return next()
  },
  function * (action, next) {
    if (action === 'multi') return yield ['foo', 'qux']
    return next()
  }
])

dispatch('foo').then((res) => res) // => 'bar'
disaptch('qux').then((res) => res) // => 'bar'
disaptch('multi').then((res) => res) // => ['bar', 'bar']

API

run(middleware, ctx)

  • middleware - koax middleware array
  • ctx - ctx for middleware stack

Returns: dispatch function: dispatch(action, next)

License

MIT