BaseRunner - kristianmandrup/middleware GitHub Wiki

The BaseRunner is a base class that provides the base functionality for any middleware runner. A typical runner such as ModelRunner (see model-mw project) extends BaseRunner to be specialized for operating with models.

When constructed, it can be passed a on-success function which is returned if all middlewares are run successfully. It can also take an on-error function which is called when one or more middlewares cause an error.

The result of run determines if on-success or on-error is called, depending on whether runner.errors is empty or not.

my-done-fun = ->
  "Success :)"

error-fun = ->
  error: true
  errors: @errors

mw = {}
mw.base     = new BaseMw name: 'my mw'
base-runner = new BaseRunner on-success: my-done-fun, on-error: error-fun
base-runner.use mw.base
base-runner.run!

# can also use with hash of Mw-components, registering each by key
base-runner.use basic: mw.base, super-cool: mw.cool

# or simply by list - default: name from class name, dasherized!
base-runner.use mw.auth, mw.very-cool