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: multiple same request event listener #8404

Merged
merged 1 commit into from May 30, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/common/Page.ts
Expand Up @@ -631,11 +631,13 @@ export class Page extends EventEmitter {
handler: (event: PageEventObject[K]) => void
): EventEmitter {
if (eventName === 'request') {
const wrap = (event: HTTPRequest) => {
event.enqueueInterceptAction(() =>
handler(event as PageEventObject[K])
);
};
const wrap =
this._handlerMap.get(handler) ||
((event: HTTPRequest) => {
event.enqueueInterceptAction(() =>
handler(event as PageEventObject[K])
);
});

this._handlerMap.set(handler, wrap);

Expand Down
10 changes: 7 additions & 3 deletions test/page.spec.ts
Expand Up @@ -153,17 +153,21 @@ describe('Page', function () {
}
};

page.on('request', onResponse);
page.on('request', onResponse);
await page.goto(server.EMPTY_PAGE);
expect(handler.callCount).toBe(1);
expect(handler.callCount).toBe(2);
page.off('request', onResponse);
await page.goto(server.EMPTY_PAGE);
// Still one because we removed the handler.
expect(handler.callCount).toBe(1);
expect(handler.callCount).toBe(3);
page.off('request', onResponse);
await page.goto(server.EMPTY_PAGE);
expect(handler.callCount).toBe(3);
page.on('request', onResponse);
await page.goto(server.EMPTY_PAGE);
// Two now because we added the handler back.
expect(handler.callCount).toBe(2);
expect(handler.callCount).toBe(4);
});
});

Expand Down