Skip to content

Commit

Permalink
chore: use AbortSignal instead of AbortController (#10048)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Apr 20, 2023
1 parent 5547e43 commit bbf2c0a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
12 changes: 6 additions & 6 deletions docs/api/puppeteer.waitforselectoroptions.md
Expand Up @@ -12,9 +12,9 @@ export interface WaitForSelectorOptions

## Properties

| Property | Modifiers | Type | Description | Default |
| --------------- | --------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------- |
| abortController | <code>optional</code> | AbortController | Provide an abort controller to cancel a waitForSelector call. | |
| hidden | <code>optional</code> | boolean | Wait for the selected element to not be found in the DOM or to be hidden, i.e. have <code>display: none</code> or <code>visibility: hidden</code> CSS properties. | <code>false</code> |
| timeout | <code>optional</code> | number | <p>Maximum time to wait in milliseconds. Pass <code>0</code> to disable timeout.</p><p>The default value can be changed by using [Page.setDefaultTimeout()](./puppeteer.page.setdefaulttimeout.md)</p> | <code>30_000</code> (30 seconds) |
| visible | <code>optional</code> | boolean | Wait for the selected element to be present in DOM and to be visible, i.e. to not have <code>display: none</code> or <code>visibility: hidden</code> CSS properties. | <code>false</code> |
| Property | Modifiers | Type | Description | Default |
| -------- | --------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------- |
| hidden | <code>optional</code> | boolean | Wait for the selected element to not be found in the DOM or to be hidden, i.e. have <code>display: none</code> or <code>visibility: hidden</code> CSS properties. | <code>false</code> |
| signal | <code>optional</code> | AbortSignal | A signal object that allows you to cancel a waitForSelector call. | |
| timeout | <code>optional</code> | number | <p>Maximum time to wait in milliseconds. Pass <code>0</code> to disable timeout.</p><p>The default value can be changed by using [Page.setDefaultTimeout()](./puppeteer.page.setdefaulttimeout.md)</p> | <code>30_000</code> (30 seconds) |
| visible | <code>optional</code> | boolean | Wait for the selected element to be present in DOM and to be visible, i.e. to not have <code>display: none</code> or <code>visibility: hidden</code> CSS properties. | <code>false</code> |
10 changes: 5 additions & 5 deletions packages/puppeteer-core/src/common/IsolatedWorld.ts
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -435,15 +435,15 @@ export class IsolatedWorld {
polling?: 'raf' | 'mutation' | number;
timeout?: number;
root?: ElementHandle<Node>;
abortController?: AbortController;
signal?: AbortSignal;
} = {},
...args: Params
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
const {
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');
Expand All @@ -454,7 +454,7 @@ export class IsolatedWorld {
polling,
root,
timeout,
abortController,
signal,
},
pageFunction as unknown as
| ((...args: unknown[]) => Promise<Awaited<ReturnType<Func>>>)
Expand Down
8 changes: 4 additions & 4 deletions packages/puppeteer-core/src/common/QueryHandler.ts
Expand Up @@ -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.');
}

Expand All @@ -190,7 +190,7 @@ export class QueryHandler {
polling: visible || hidden ? 'raf' : 'mutation',
root: element,
timeout,
abortController,
signal,
},
LazyArg.create(context => {
return context.puppeteerUtil;
Expand All @@ -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.');
}
Expand Down
8 changes: 4 additions & 4 deletions packages/puppeteer-core/src/common/WaitTask.ts
Expand Up @@ -32,7 +32,7 @@ export interface WaitTaskOptions {
polling: 'raf' | 'mutation' | number;
root?: ElementHandle<Node>;
timeout: number;
abortController?: AbortController;
signal?: AbortSignal;
}

/**
Expand All @@ -51,7 +51,7 @@ export class WaitTask<T = unknown> {
#result = createDeferredPromise<HandleFor<T>>();

#poller?: JSHandle<Poller<T>>;
#abortController?: AbortController;
#signal?: AbortSignal;

constructor(
world: IsolatedWorld,
Expand All @@ -62,8 +62,8 @@ export class WaitTask<T = unknown> {
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.'));
Expand Down
2 changes: 1 addition & 1 deletion test/src/waittask.spec.ts
Expand Up @@ -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/);
Expand Down

0 comments on commit bbf2c0a

Please sign in to comment.