diff --git a/plugin/eventbus/lib/EggContextEventBus.ts b/plugin/eventbus/lib/EggContextEventBus.ts index 34151285..ecb0df2f 100644 --- a/plugin/eventbus/lib/EggContextEventBus.ts +++ b/plugin/eventbus/lib/EggContextEventBus.ts @@ -28,6 +28,8 @@ export class EggContextEventBus implements ContextEventBus { uncork() { assert(this.corkId, 'eventbus uncork without cork'); this.eventBus.uncork(this.corkId); + this.context.set(CORK_ID, null); + this.corkId = undefined; } emit(event: E, ...args: any): boolean { diff --git a/plugin/eventbus/test/eventbus.test.ts b/plugin/eventbus/test/eventbus.test.ts index c32a2e61..51b85d0f 100644 --- a/plugin/eventbus/test/eventbus.test.ts +++ b/plugin/eventbus/test/eventbus.test.ts @@ -65,4 +65,28 @@ describe('test/eventbus.test.ts', () => { await helloEvent; assert(helloTime >= triggerTime + 100); }); + + it('can call cork/uncork multi times', async () => { + ctx = await app.mockModuleContext(); + + const helloService = await ctx.getEggObject(HelloService); + const eventWaiter = await app.getEventWaiter(); + + let helloCalled = 0; + // helloLogger is in child context + mm(HelloLogger.prototype, 'handle', () => { + helloCalled++; + }); + helloService.cork(); + helloService.hello(); + helloService.uncork(); + await eventWaiter.await('helloEgg'); + + helloService.cork(); + helloService.hello(); + helloService.uncork(); + await eventWaiter.await('helloEgg'); + + assert(helloCalled === 2); + }); });