Skip to content

Commit 9d0c712

Browse files
Merge pull request #724 from GuillaumeGomez/less-wait
Remove usage of `puppeteer.wait` function in `index.js`
2 parents aa6bc03 + fcf87dc commit 9d0c712

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const {
1313
getFileInfoFromPath,
1414
extractFileNameWithoutExtension,
1515
plural,
16+
sleep,
17+
waitForConditionTrue,
1618
} = require('./utils.js');
1719
const consts = require('./consts.js');
1820
const { Options } = require('./options.js');
@@ -203,7 +205,7 @@ async function runAllCommands(loaded, logs, options, browser) {
203205
page = await browser.newPage(options, currentFile, logs);
204206
} catch (e) {
205207
// try again after waiting a bit first to avoid "Session with given id not found" error...
206-
await new Promise(r => setTimeout(r, 100));
208+
await sleep(100);
207209
page = await browser.newPage(options, currentFile, logs);
208210
}
209211
await browser.emulate(options, currentFile, page, logs);
@@ -419,14 +421,19 @@ async function runAllCommands(loaded, logs, options, browser) {
419421
}
420422
if (shouldWait) {
421423
// We wait a bit between each command to be sure the browser can follow.
422-
await new Promise(r => setTimeout(r, 50));
424+
await sleep(50);
423425
}
424426
// If the URL changed, we wait for the document to be fully loaded before running other
425427
// commands.
426428
const url = page.url();
427429
if (url !== current_url) {
428430
current_url = url;
429-
await page.waitForFunction('document.readyState === "complete"');
431+
if (!await waitForConditionTrue(pages, async() => {
432+
return await page.evaluate(() => 'return document.readyState === "complete"');
433+
})) {
434+
logs.error(currentFile, `\`${url}\` never finished loading`);
435+
break command_loop;
436+
}
430437
}
431438
if (checkJsErrors() || checkRequestErrors()) {
432439
break command_loop;

src/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ function getFileInfo(context_parser, line = null, isExact = true) {
195195
return getFileInfoFromPath(context_parser.getCurrentFile(), line, isExact);
196196
}
197197

198+
async function sleep(ms) {
199+
await new Promise(r => setTimeout(r, ms));
200+
}
201+
202+
async function waitForConditionTrue(pages, callback) {
203+
const timeLimit = pages[0].getDefaultTimeout();
204+
const timeAdd = 50;
205+
let allTime = 0;
206+
207+
// eslint-disable-next-line no-constant-condition
208+
while (true) {
209+
if (await callback()) {
210+
return true;
211+
}
212+
if (timeLimit === 0) {
213+
continue;
214+
}
215+
allTime += timeAdd;
216+
if (allTime >= timeLimit) {
217+
return false;
218+
}
219+
}
220+
}
221+
198222
module.exports = {
199223
'addSlash': addSlash,
200224
'getCurrentDir': getCurrentDir,
@@ -218,5 +242,7 @@ module.exports = {
218242
'hasError': hasError,
219243
'getFileInfo': getFileInfo,
220244
'getFileInfoFromPath': getFileInfoFromPath,
245+
'sleep': sleep,
246+
'waitForConditionTrue': waitForConditionTrue,
221247
'ALLOWED_EMULATE_MEDIA_FEATURES_KEYS': ALLOWED_EMULATE_MEDIA_FEATURES_KEYS,
222248
};

0 commit comments

Comments
 (0)