From bbf2c0a8ecb161765613fe9842c4f8330ec8388d Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Thu, 20 Apr 2023 08:18:48 +0200 Subject: [PATCH] chore: use AbortSignal instead of AbortController (#10048) --- docs/api/puppeteer.waitforselectoroptions.md | 12 ++++++------ packages/puppeteer-core/src/common/IsolatedWorld.ts | 10 +++++----- packages/puppeteer-core/src/common/QueryHandler.ts | 8 ++++---- packages/puppeteer-core/src/common/WaitTask.ts | 8 ++++---- test/src/waittask.spec.ts | 2 +- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/api/puppeteer.waitforselectoroptions.md b/docs/api/puppeteer.waitforselectoroptions.md index b8f3f3b3045..41dbd25c9f7 100644 --- a/docs/api/puppeteer.waitforselectoroptions.md +++ b/docs/api/puppeteer.waitforselectoroptions.md @@ -12,9 +12,9 @@ export interface WaitForSelectorOptions ## Properties -| Property | Modifiers | Type | Description | Default | -| --------------- | --------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------- | -| abortController | optional | AbortController | Provide an abort controller to cancel a waitForSelector call. | | -| hidden | optional | boolean | Wait for the selected element to not be found in the DOM or to be hidden, i.e. have display: none or visibility: hidden CSS properties. | false | -| timeout | optional | number |

Maximum time to wait in milliseconds. Pass 0 to disable timeout.

The default value can be changed by using [Page.setDefaultTimeout()](./puppeteer.page.setdefaulttimeout.md)

| 30_000 (30 seconds) | -| visible | optional | boolean | Wait for the selected element to be present in DOM and to be visible, i.e. to not have display: none or visibility: hidden CSS properties. | false | +| Property | Modifiers | Type | Description | Default | +| -------- | --------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------- | +| hidden | optional | boolean | Wait for the selected element to not be found in the DOM or to be hidden, i.e. have display: none or visibility: hidden CSS properties. | false | +| signal | optional | AbortSignal | A signal object that allows you to cancel a waitForSelector call. | | +| timeout | optional | number |

Maximum time to wait in milliseconds. Pass 0 to disable timeout.

The default value can be changed by using [Page.setDefaultTimeout()](./puppeteer.page.setdefaulttimeout.md)

| 30_000 (30 seconds) | +| visible | optional | boolean | Wait for the selected element to be present in DOM and to be visible, i.e. to not have display: none or visibility: hidden CSS properties. | false | diff --git a/packages/puppeteer-core/src/common/IsolatedWorld.ts b/packages/puppeteer-core/src/common/IsolatedWorld.ts index 1e1e6450c6e..7be1a8560e3 100644 --- a/packages/puppeteer-core/src/common/IsolatedWorld.ts +++ b/packages/puppeteer-core/src/common/IsolatedWorld.ts @@ -73,9 +73,9 @@ export interface WaitForSelectorOptions { */ timeout?: number; /** - * Provide an abort controller to cancel a waitForSelector call. + * A signal object that allows you to cancel a waitForSelector call. */ - abortController?: AbortController; + signal?: AbortSignal; } /** @@ -435,7 +435,7 @@ export class IsolatedWorld { polling?: 'raf' | 'mutation' | number; timeout?: number; root?: ElementHandle; - abortController?: AbortController; + signal?: AbortSignal; } = {}, ...args: Params ): Promise>>> { @@ -443,7 +443,7 @@ export class IsolatedWorld { polling = 'raf', timeout = this.#timeoutSettings.timeout(), root, - abortController, + signal, } = options; if (typeof polling === 'number' && polling < 0) { throw new Error('Cannot poll with non-positive interval'); @@ -454,7 +454,7 @@ export class IsolatedWorld { polling, root, timeout, - abortController, + signal, }, pageFunction as unknown as | ((...args: unknown[]) => Promise>>) diff --git a/packages/puppeteer-core/src/common/QueryHandler.ts b/packages/puppeteer-core/src/common/QueryHandler.ts index 1c174a2b313..31290613b39 100644 --- a/packages/puppeteer-core/src/common/QueryHandler.ts +++ b/packages/puppeteer-core/src/common/QueryHandler.ts @@ -167,10 +167,10 @@ export class QueryHandler { element = await frame.worlds[PUPPETEER_WORLD].adoptHandle(elementOrFrame); } - const {visible = false, hidden = false, timeout, abortController} = options; + const {visible = false, hidden = false, timeout, signal} = options; try { - if (options.abortController?.signal.aborted) { + if (signal?.aborted) { throw new AbortError('QueryHander.waitFor has been aborted.'); } @@ -190,7 +190,7 @@ export class QueryHandler { polling: visible || hidden ? 'raf' : 'mutation', root: element, timeout, - abortController, + signal, }, LazyArg.create(context => { return context.puppeteerUtil; @@ -201,7 +201,7 @@ export class QueryHandler { visible ? true : hidden ? false : undefined ); - if (options.abortController?.signal.aborted) { + if (signal?.aborted) { await handle.dispose(); throw new AbortError('QueryHander.waitFor has been aborted.'); } diff --git a/packages/puppeteer-core/src/common/WaitTask.ts b/packages/puppeteer-core/src/common/WaitTask.ts index d88ab59f410..ddb87b782d0 100644 --- a/packages/puppeteer-core/src/common/WaitTask.ts +++ b/packages/puppeteer-core/src/common/WaitTask.ts @@ -32,7 +32,7 @@ export interface WaitTaskOptions { polling: 'raf' | 'mutation' | number; root?: ElementHandle; timeout: number; - abortController?: AbortController; + signal?: AbortSignal; } /** @@ -51,7 +51,7 @@ export class WaitTask { #result = createDeferredPromise>(); #poller?: JSHandle>; - #abortController?: AbortController; + #signal?: AbortSignal; constructor( world: IsolatedWorld, @@ -62,8 +62,8 @@ export class WaitTask { this.#world = world; this.#polling = options.polling; this.#root = options.root; - this.#abortController = options.abortController; - this.#abortController?.signal?.addEventListener( + this.#signal = options.signal; + this.#signal?.addEventListener( 'abort', () => { this.terminate(new AbortError('WaitTask has been aborted.')); diff --git a/test/src/waittask.spec.ts b/test/src/waittask.spec.ts index f5abb7c2a55..7187b1dc481 100644 --- a/test/src/waittask.spec.ts +++ b/test/src/waittask.spec.ts @@ -386,7 +386,7 @@ describe('waittask specs', function () { await page.goto(server.EMPTY_PAGE); const abortController = new AbortController(); const task = page.waitForSelector('wrong', { - abortController, + signal: abortController.signal, }); abortController.abort(); expect(task).rejects.toThrow(/aborted/);