Skip to content

Commit

Permalink
feat: add AbortSignal to waitForFunction (#10078)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com>
  • Loading branch information
OrKoN and Lightning00Blade committed Apr 26, 2023
1 parent 159513c commit 4dd4cb9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api/puppeteer.framewaitforfunctionoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface FrameWaitForFunctionOptions
| Property | Modifiers | Type | Description | Default |
| -------- | --------------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| polling | <code>optional</code> | 'raf' \| 'mutation' \| number | <p>An interval at which the <code>pageFunction</code> is executed, defaults to <code>raf</code>. If <code>polling</code> is a number, then it is treated as an interval in milliseconds at which the function would be executed. If <code>polling</code> is a string, then it can be one of the following values:</p><p>- <code>raf</code> - to constantly execute <code>pageFunction</code> in <code>requestAnimationFrame</code> callback. This is the tightest polling mode which is suitable to observe styling changes.</p><p>- <code>mutation</code> - to execute <code>pageFunction</code> on every DOM mutation.</p> | |
| signal | <code>optional</code> | AbortSignal | A signal object that allows you to cancel a waitForFunction call. | |
| timeout | <code>optional</code> | number | Maximum time to wait in milliseconds. Defaults to <code>30000</code> (30 seconds). Pass <code>0</code> to disable the timeout. Puppeteer's default timeout can be changed using [Page.setDefaultTimeout()](./puppeteer.page.setdefaulttimeout.md). | |
4 changes: 4 additions & 0 deletions packages/puppeteer-core/src/common/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export interface FrameWaitForFunctionOptions {
* using {@link Page.setDefaultTimeout}.
*/
timeout?: number;
/**
* A signal object that allows you to cancel a waitForFunction call.
*/
signal?: AbortSignal;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/src/waittask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ describe('waittask specs', function () {
});
await watchdog;
});
it('should be cancellable', async () => {
const {page, server} = getTestState();

await page.goto(server.EMPTY_PAGE);
const abortController = new AbortController();
const task = page.waitForFunction(
() => {
return (globalThis as any).__done;
},
{
signal: abortController.signal,
}
);
abortController.abort();
await expect(task).rejects.toThrow(/aborted/);
});
});

describe('Page.waitForTimeout', () => {
Expand Down

0 comments on commit 4dd4cb9

Please sign in to comment.