Skip to content

Context State and Hookless hooking #139

@ritch

Description

@ritch

@simonho @Raymond @miroslav

I have a proposal for hooks that re-uses our existing declare context bindings approach.

@bind('res.time')
function getTime(@inject('state.accepted') accepted, @inject('state.finished') finished)  {
  // pause execution until the context is in the `accepted`
  await accepted();
  const start = hrtime();
  // pause until the context is in the `finished` state
  await finished();
  return hrtime() - start;
}

/* states:
   - accepted - the request has been parsed
   - finished - the response has been written
*/

@bind('state.finished')
function finished(@inject('watch') watch) {
  while((await watch('state.history').contains('finished') === false) {
    // wait for finished state to be in the history
  }
}

// in core
function writeResponse(@inject('state') state) {
  state('finished'); // state.history.push('finished');
  // ...
}

The way this works is that you can ensure some state has already occurred, but not define a specific order of execution. Which gives you the benefits of koa without the middleware execution order dependency.

Metadata

Metadata

Assignees

Labels

IoC/Context@loopback/context: Dependency Injection, Inversion of Controlstale

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions