-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
IoC/Context@loopback/context: Dependency Injection, Inversion of Control@loopback/context: Dependency Injection, Inversion of Controlstale
Description
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 Control@loopback/context: Dependency Injection, Inversion of Controlstale