Skip to content

Commit

Permalink
fix(context): close invocation context only after async is done
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 10, 2019
1 parent 8a8857d commit e71e990
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ describe('InvocationContext', () => {
expect(invocationCtxForCheckName.invokeTargetMethod()).to.eql(true);
});

it('does not close when an interceptor is in processing', () => {
const result = invocationCtxForGreet.invokeTargetMethod();
expect(invocationCtxForGreet.isBound('abc'));
return result;
});

class MyController {
static checkName(name: string) {
const firstLetter = name.substring(0, 1);
Expand All @@ -73,6 +79,7 @@ describe('InvocationContext', () => {

function givenContext() {
ctx = new Context();
ctx.bind('abc').to('xyz');
}

function givenInvocationContext() {
Expand Down
19 changes: 12 additions & 7 deletions packages/context/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {filterByTag} from './binding-filter';
import {BindingAddress} from './binding-key';
import {Context} from './context';
import {ContextBindings, ContextTags} from './keys';
import {transformValueOrPromise, ValueOrPromise} from './value-promise';
import {
transformValueOrPromise,
tryWithFinally,
ValueOrPromise,
} from './value-promise';
const debug = debugFactory('loopback:context:interceptor');
const getTargetName = DecoratorFactory.getTargetName;

Expand Down Expand Up @@ -390,12 +394,13 @@ export function invokeMethodWithInterceptors(
);

invocationCtx.assertMethodExists();
try {
const interceptors = invocationCtx.loadInterceptors();
return invokeInterceptors(invocationCtx, interceptors);
} finally {
invocationCtx.close();
}
return tryWithFinally(
() => {
const interceptors = invocationCtx.loadInterceptors();
return invokeInterceptors(invocationCtx, interceptors);
},
() => invocationCtx.close(),
);
}

/**
Expand Down

0 comments on commit e71e990

Please sign in to comment.