Skip to content

Commit

Permalink
fix: multiple same request event listener (#8404)
Browse files Browse the repository at this point in the history
  • Loading branch information
Junyan committed May 30, 2022
1 parent d111d19 commit 9211015
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
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

0 comments on commit 9211015

Please sign in to comment.