1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | function waitForNetworkIdle(page, timeout = 50, maxInflightRequests = 0) {
|
4 | page.on('request', onRequestStarted);
|
5 | page.on('requestfinished', onRequestFinished);
|
6 | page.on('requestfailed', onRequestFinished);
|
7 | let inflight = 0;
|
8 | let fulfill;
|
9 | const promise = new Promise((x) => fulfill = x);
|
10 | let timeoutId = setTimeout(onTimeoutDone, timeout);
|
11 | return promise;
|
12 | function onTimeoutDone() {
|
13 | page.removeListener('request', onRequestStarted);
|
14 | page.removeListener('requestfinished', onRequestFinished);
|
15 | page.removeListener('requestfailed', onRequestFinished);
|
16 | fulfill();
|
17 | }
|
18 | function onRequestStarted() {
|
19 | ++inflight;
|
20 | if (inflight > maxInflightRequests) {
|
21 | clearTimeout(timeoutId);
|
22 | }
|
23 | }
|
24 | function onRequestFinished() {
|
25 | if (inflight === 0) {
|
26 | return;
|
27 | }
|
28 | --inflight;
|
29 | if (inflight === maxInflightRequests) {
|
30 | timeoutId = setTimeout(onTimeoutDone, timeout);
|
31 | }
|
32 | }
|
33 | }
|
34 | exports.default = waitForNetworkIdle;
|