fix: multiple same request event listener (#8404)

This commit is contained in:
Junyan 2022-05-31 04:34:08 +08:00 committed by GitHub
parent d111d19f78
commit 92110151d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View File

@ -631,11 +631,13 @@ export class Page extends EventEmitter {
handler: (event: PageEventObject[K]) => void handler: (event: PageEventObject[K]) => void
): EventEmitter { ): EventEmitter {
if (eventName === 'request') { if (eventName === 'request') {
const wrap = (event: HTTPRequest) => { const wrap =
event.enqueueInterceptAction(() => this._handlerMap.get(handler) ||
handler(event as PageEventObject[K]) ((event: HTTPRequest) => {
); event.enqueueInterceptAction(() =>
}; handler(event as PageEventObject[K])
);
});
this._handlerMap.set(handler, wrap); this._handlerMap.set(handler, wrap);

View File

@ -153,17 +153,21 @@ describe('Page', function () {
} }
}; };
page.on('request', onResponse);
page.on('request', onResponse); page.on('request', onResponse);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
expect(handler.callCount).toBe(1); expect(handler.callCount).toBe(2);
page.off('request', onResponse); page.off('request', onResponse);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
// Still one because we removed the handler. // 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); page.on('request', onResponse);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
// Two now because we added the handler back. // Two now because we added the handler back.
expect(handler.callCount).toBe(2); expect(handler.callCount).toBe(4);
}); });
}); });