chore: simplify waitWithTimeout (#10278)

This commit is contained in:
Nikolay Vitkov 2023-05-31 14:48:51 +02:00 committed by GitHub
parent 6f8ea5764d
commit 6923c6b3de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,14 +22,13 @@ import type {ElementHandle} from '../api/ElementHandle.js';
import type {JSHandle} from '../api/JSHandle.js'; import type {JSHandle} from '../api/JSHandle.js';
import {Page} from '../api/Page.js'; import {Page} from '../api/Page.js';
import {isNode} from '../environment.js'; import {isNode} from '../environment.js';
import {createDeferred} from '../puppeteer-core.js';
import {assert} from '../util/assert.js'; import {assert} from '../util/assert.js';
import {createDeferred} from '../util/Deferred.js';
import {isErrorLike} from '../util/ErrorLike.js'; import {isErrorLike} from '../util/ErrorLike.js';
import type {CDPSession} from './Connection.js'; import type {CDPSession} from './Connection.js';
import {debug} from './Debug.js'; import {debug} from './Debug.js';
import {CDPElementHandle} from './ElementHandle.js'; import {CDPElementHandle} from './ElementHandle.js';
import {TimeoutError} from './Errors.js';
import type {CommonEventEmitter} from './EventEmitter.js'; import type {CommonEventEmitter} from './EventEmitter.js';
import type {ExecutionContext} from './ExecutionContext.js'; import type {ExecutionContext} from './ExecutionContext.js';
import {CDPJSHandle} from './JSHandle.js'; import {CDPJSHandle} from './JSHandle.js';
@ -392,7 +391,8 @@ export async function waitForEvent<T>(
deferred.resolve(event); deferred.resolve(event);
} }
}); });
return Promise.race([deferred.valueOrThrow(), abortPromise]).then( return Promise.race([deferred.valueOrThrow(), abortPromise])
.then(
r => { r => {
removeEventListeners([listener]); removeEventListeners([listener]);
if (isErrorLike(r)) { if (isErrorLike(r)) {
@ -404,7 +404,10 @@ export async function waitForEvent<T>(
removeEventListeners([listener]); removeEventListeners([listener]);
throw error; throw error;
} }
); )
.finally(() => {
deferred.reject(new Error('Cleared'));
});
} }
/** /**
@ -506,26 +509,14 @@ export async function waitWithTimeout<T>(
taskName: string, taskName: string,
timeout: number timeout: number
): Promise<T> { ): Promise<T> {
let reject: (reason?: Error) => void; const deferred = createDeferred<never>({
const timeoutError = new TimeoutError( message: `waiting for ${taskName} failed: timeout ${timeout}ms exceeded`,
`waiting for ${taskName} failed: timeout ${timeout}ms exceeded` timeout,
); });
const timeoutPromise = new Promise<never>((_, rej) => {
return (reject = rej); return await Promise.race([promise, deferred.valueOrThrow()]).finally(() => {
deferred.reject(new Error('Cleared'));
}); });
let timeoutTimer = null;
if (timeout) {
timeoutTimer = setTimeout(() => {
return reject(timeoutError);
}, timeout);
}
try {
return await Promise.race([promise, timeoutPromise]);
} finally {
if (timeoutTimer) {
clearTimeout(timeoutTimer);
}
}
} }
/** /**