BakeryJS icon indicating copy to clipboard operation
BakeryJS copied to clipboard

Middleware API

Open jnv opened this issue 6 years ago • 2 comments

This could be used to implement: #39, #25, #44.

  • Setup globally, for program
  • Wraps each box in a flow
  • Receives an instance of box and its metadata, can (re-)call it

jnv avatar Jan 21 '19 11:01 jnv

Since box can emit unlimited amount of messages, a plain Koa-like middleware won't cut it. We can use generators though and yield to the middleware. The user-level API may look like this:

function myFancyMiddleware (ctx, next) {
  // do something before the box is called
  for (let message of next()) {
    // do something with message
    // pass the message up the middleware chain
    yield message
  }
  // do something once the box is done
}

jnv avatar Jan 21 '19 13:01 jnv

Prototype here https://github.com/jnv/bakeryjs-middlewares-prototype

jnv avatar Jan 21 '19 18:01 jnv