chore: implement Disposable/AsyncDisposable on Page and Browser (#10809)

This commit is contained in:
jrandolf 2023-08-30 13:10:06 +02:00 committed by GitHub
parent 4d3cb6ccf3
commit c67141f3ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 102 additions and 5 deletions

View File

@ -0,0 +1,17 @@
---
sidebar_label: Browser.[Symbol.asyncDispose]
---
# Browser.\[Symbol.asyncDispose\]() method
#### Signature:
```typescript
class Browser {
[Symbol.asyncDispose](): Promise<void>;
}
```
**Returns:**
Promise&lt;void&gt;

View File

@ -0,0 +1,17 @@
---
sidebar_label: Browser.[Symbol.dispose]
---
# Browser.\[Symbol.dispose\]() method
#### Signature:
```typescript
class Browser {
[Symbol.dispose](): void;
}
```
**Returns:**
void

View File

@ -9,11 +9,13 @@ A Browser is created when Puppeteer connects to a browser instance, either throu
#### Signature: #### Signature:
```typescript ```typescript
export declare class Browser extends EventEmitter export declare class Browser extends EventEmitter implements AsyncDisposable, Disposable
``` ```
**Extends:** [EventEmitter](./puppeteer.eventemitter.md) **Extends:** [EventEmitter](./puppeteer.eventemitter.md)
**Implements:** AsyncDisposable, Disposable
## Remarks ## Remarks
The Browser class extends from Puppeteer's [EventEmitter](./puppeteer.eventemitter.md) class and will emit various events which are documented in the [BrowserEmittedEvents](./puppeteer.browseremittedevents.md) enum. The Browser class extends from Puppeteer's [EventEmitter](./puppeteer.eventemitter.md) class and will emit various events which are documented in the [BrowserEmittedEvents](./puppeteer.browseremittedevents.md) enum.
@ -60,6 +62,8 @@ import puppeteer from 'puppeteer';
| Method | Modifiers | Description | | Method | Modifiers | Description |
| ---------------------------------------------------------------------------------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ---------------------------------------------------------------------------------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [\[Symbol.asyncDispose\]()](./puppeteer.browser._symbol.asyncdispose_.md) | | |
| [\[Symbol.dispose\]()](./puppeteer.browser._symbol.dispose_.md) | | |
| [browserContexts()](./puppeteer.browser.browsercontexts.md) | | Returns an array of all open browser contexts. In a newly created browser, this will return a single instance of [BrowserContext](./puppeteer.browsercontext.md). | | [browserContexts()](./puppeteer.browser.browsercontexts.md) | | Returns an array of all open browser contexts. In a newly created browser, this will return a single instance of [BrowserContext](./puppeteer.browsercontext.md). |
| [close()](./puppeteer.browser.close.md) | | Closes the browser and all of its pages (if any were opened). The [Browser](./puppeteer.browser.md) object itself is considered to be disposed and cannot be used anymore. | | [close()](./puppeteer.browser.close.md) | | Closes the browser and all of its pages (if any were opened). The [Browser](./puppeteer.browser.md) object itself is considered to be disposed and cannot be used anymore. |
| [createIncognitoBrowserContext(options)](./puppeteer.browser.createincognitobrowsercontext.md) | | Creates a new incognito browser context. This won't share cookies/cache with other browser contexts. | | [createIncognitoBrowserContext(options)](./puppeteer.browser.createincognitobrowsercontext.md) | | Creates a new incognito browser context. This won't share cookies/cache with other browser contexts. |

View File

@ -0,0 +1,17 @@
---
sidebar_label: Page.[Symbol.asyncDispose]
---
# Page.\[Symbol.asyncDispose\]() method
#### Signature:
```typescript
class Page {
[Symbol.asyncDispose](): Promise<void>;
}
```
**Returns:**
Promise&lt;void&gt;

View File

@ -0,0 +1,17 @@
---
sidebar_label: Page.[Symbol.dispose]
---
# Page.\[Symbol.dispose\]() method
#### Signature:
```typescript
class Page {
[Symbol.dispose](): void;
}
```
**Returns:**
void

View File

@ -15,11 +15,13 @@ One Browser instance might have multiple Page instances.
#### Signature: #### Signature:
```typescript ```typescript
export declare class Page extends EventEmitter export declare class Page extends EventEmitter implements AsyncDisposable, Disposable
``` ```
**Extends:** [EventEmitter](./puppeteer.eventemitter.md) **Extends:** [EventEmitter](./puppeteer.eventemitter.md)
**Implements:** AsyncDisposable, Disposable
## Remarks ## Remarks
The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `Page` class. The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `Page` class.
@ -76,6 +78,8 @@ page.off('request', logRequest);
| Method | Modifiers | Description | | Method | Modifiers | Description |
| ---------------------------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ---------------------------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [\[Symbol.asyncDispose\]()](./puppeteer.page._symbol.asyncdispose_.md) | | |
| [\[Symbol.dispose\]()](./puppeteer.page._symbol.dispose_.md) | | |
| [$(selector)](./puppeteer.page._.md) | | Runs <code>document.querySelector</code> within the page. If no element matches the selector, the return value resolves to <code>null</code>. | | [$(selector)](./puppeteer.page._.md) | | Runs <code>document.querySelector</code> within the page. If no element matches the selector, the return value resolves to <code>null</code>. |
| [$$(selector)](./puppeteer.page.__.md) | | The method runs <code>document.querySelectorAll</code> within the page. If no elements match the selector, the return value resolves to <code>[]</code>. | | [$$(selector)](./puppeteer.page.__.md) | | The method runs <code>document.querySelectorAll</code> within the page. If no elements match the selector, the return value resolves to <code>[]</code>. |
| [$$eval(selector, pageFunction, args)](./puppeteer.page.__eval.md) | | This method runs <code>Array.from(document.querySelectorAll(selector))</code> within the page and passes the result as the first argument to the <code>pageFunction</code>. | | [$$eval(selector, pageFunction, args)](./puppeteer.page.__eval.md) | | This method runs <code>Array.from(document.querySelectorAll(selector))</code> within the page and passes the result as the first argument to the <code>pageFunction</code>. |

View File

@ -20,8 +20,9 @@ import {ChildProcess} from 'child_process';
import {Protocol} from 'devtools-protocol'; import {Protocol} from 'devtools-protocol';
import {Symbol} from '../../third_party/disposablestack/disposablestack.js';
import {EventEmitter} from '../common/EventEmitter.js'; import {EventEmitter} from '../common/EventEmitter.js';
import {waitWithTimeout} from '../common/util.js'; import {debugError, waitWithTimeout} from '../common/util.js';
import {Deferred} from '../util/Deferred.js'; import {Deferred} from '../util/Deferred.js';
import type {BrowserContext} from './BrowserContext.js'; import type {BrowserContext} from './BrowserContext.js';
@ -217,7 +218,10 @@ export const enum BrowserEmittedEvents {
* *
* @public * @public
*/ */
export class Browser extends EventEmitter { export class Browser
extends EventEmitter
implements AsyncDisposable, Disposable
{
/** /**
* @internal * @internal
*/ */
@ -478,6 +482,14 @@ export class Browser extends EventEmitter {
isConnected(): boolean { isConnected(): boolean {
throw new Error('Not implemented'); throw new Error('Not implemented');
} }
[Symbol.dispose](): void {
return void this.close().catch(debugError);
}
[Symbol.asyncDispose](): Promise<void> {
return this.close();
}
} }
/** /**
* @public * @public

View File

@ -64,6 +64,7 @@ import type {
NodeFor, NodeFor,
} from '../common/types.js'; } from '../common/types.js';
import { import {
debugError,
importFSPromises, importFSPromises,
isNumber, isNumber,
isString, isString,
@ -477,7 +478,7 @@ export interface NewDocumentScriptEvaluation {
* *
* @public * @public
*/ */
export class Page extends EventEmitter { export class Page extends EventEmitter implements AsyncDisposable, Disposable {
#handlerMap = new WeakMap<Handler<any>, Handler<any>>(); #handlerMap = new WeakMap<Handler<any>, Handler<any>>();
/** /**
@ -2873,6 +2874,14 @@ export class Page extends EventEmitter {
waitForDevicePrompt(): Promise<DeviceRequestPrompt> { waitForDevicePrompt(): Promise<DeviceRequestPrompt> {
throw new Error('Not implemented'); throw new Error('Not implemented');
} }
[Symbol.dispose](): void {
return void this.close().catch(debugError);
}
[Symbol.asyncDispose](): Promise<void> {
return this.close();
}
} }
/** /**