Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cork/uncork should can be called multi times in same ctx #78

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugin/eventbus/lib/EggContextEventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看来是一直都有问题?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的。之前没测试过多次的场景

this.corkId = undefined;
}

emit<E extends keyof Events>(event: E, ...args: any): boolean {
Expand Down
24 changes: 24 additions & 0 deletions plugin/eventbus/test/eventbus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});