mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: implement Disposable/AsyncDisposable on Page and Browser (#10809)
This commit is contained in:
parent
4d3cb6ccf3
commit
c67141f3ae
17
docs/api/puppeteer.browser._symbol.asyncdispose_.md
Normal file
17
docs/api/puppeteer.browser._symbol.asyncdispose_.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
sidebar_label: Browser.[Symbol.asyncDispose]
|
||||
---
|
||||
|
||||
# Browser.\[Symbol.asyncDispose\]() method
|
||||
|
||||
#### Signature:
|
||||
|
||||
```typescript
|
||||
class Browser {
|
||||
[Symbol.asyncDispose](): Promise<void>;
|
||||
}
|
||||
```
|
||||
|
||||
**Returns:**
|
||||
|
||||
Promise<void>
|
17
docs/api/puppeteer.browser._symbol.dispose_.md
Normal file
17
docs/api/puppeteer.browser._symbol.dispose_.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
sidebar_label: Browser.[Symbol.dispose]
|
||||
---
|
||||
|
||||
# Browser.\[Symbol.dispose\]() method
|
||||
|
||||
#### Signature:
|
||||
|
||||
```typescript
|
||||
class Browser {
|
||||
[Symbol.dispose](): void;
|
||||
}
|
||||
```
|
||||
|
||||
**Returns:**
|
||||
|
||||
void
|
@ -9,11 +9,13 @@ A Browser is created when Puppeteer connects to a browser instance, either throu
|
||||
#### Signature:
|
||||
|
||||
```typescript
|
||||
export declare class Browser extends EventEmitter
|
||||
export declare class Browser extends EventEmitter implements AsyncDisposable, Disposable
|
||||
```
|
||||
|
||||
**Extends:** [EventEmitter](./puppeteer.eventemitter.md)
|
||||
|
||||
**Implements:** AsyncDisposable, Disposable
|
||||
|
||||
## 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.
|
||||
@ -60,6 +62,8 @@ import puppeteer from 'puppeteer';
|
||||
|
||||
| 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). |
|
||||
| [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. |
|
||||
|
17
docs/api/puppeteer.page._symbol.asyncdispose_.md
Normal file
17
docs/api/puppeteer.page._symbol.asyncdispose_.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
sidebar_label: Page.[Symbol.asyncDispose]
|
||||
---
|
||||
|
||||
# Page.\[Symbol.asyncDispose\]() method
|
||||
|
||||
#### Signature:
|
||||
|
||||
```typescript
|
||||
class Page {
|
||||
[Symbol.asyncDispose](): Promise<void>;
|
||||
}
|
||||
```
|
||||
|
||||
**Returns:**
|
||||
|
||||
Promise<void>
|
17
docs/api/puppeteer.page._symbol.dispose_.md
Normal file
17
docs/api/puppeteer.page._symbol.dispose_.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
sidebar_label: Page.[Symbol.dispose]
|
||||
---
|
||||
|
||||
# Page.\[Symbol.dispose\]() method
|
||||
|
||||
#### Signature:
|
||||
|
||||
```typescript
|
||||
class Page {
|
||||
[Symbol.dispose](): void;
|
||||
}
|
||||
```
|
||||
|
||||
**Returns:**
|
||||
|
||||
void
|
@ -15,11 +15,13 @@ One Browser instance might have multiple Page instances.
|
||||
#### Signature:
|
||||
|
||||
```typescript
|
||||
export declare class Page extends EventEmitter
|
||||
export declare class Page extends EventEmitter implements AsyncDisposable, Disposable
|
||||
```
|
||||
|
||||
**Extends:** [EventEmitter](./puppeteer.eventemitter.md)
|
||||
|
||||
**Implements:** AsyncDisposable, Disposable
|
||||
|
||||
## 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.
|
||||
@ -76,6 +78,8 @@ page.off('request', logRequest);
|
||||
|
||||
| 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) | | 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>. |
|
||||
|
@ -20,8 +20,9 @@ import {ChildProcess} from 'child_process';
|
||||
|
||||
import {Protocol} from 'devtools-protocol';
|
||||
|
||||
import {Symbol} from '../../third_party/disposablestack/disposablestack.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 type {BrowserContext} from './BrowserContext.js';
|
||||
@ -217,7 +218,10 @@ export const enum BrowserEmittedEvents {
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export class Browser extends EventEmitter {
|
||||
export class Browser
|
||||
extends EventEmitter
|
||||
implements AsyncDisposable, Disposable
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@ -478,6 +482,14 @@ export class Browser extends EventEmitter {
|
||||
isConnected(): boolean {
|
||||
throw new Error('Not implemented');
|
||||
}
|
||||
|
||||
[Symbol.dispose](): void {
|
||||
return void this.close().catch(debugError);
|
||||
}
|
||||
|
||||
[Symbol.asyncDispose](): Promise<void> {
|
||||
return this.close();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @public
|
||||
|
@ -64,6 +64,7 @@ import type {
|
||||
NodeFor,
|
||||
} from '../common/types.js';
|
||||
import {
|
||||
debugError,
|
||||
importFSPromises,
|
||||
isNumber,
|
||||
isString,
|
||||
@ -477,7 +478,7 @@ export interface NewDocumentScriptEvaluation {
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export class Page extends EventEmitter {
|
||||
export class Page extends EventEmitter implements AsyncDisposable, Disposable {
|
||||
#handlerMap = new WeakMap<Handler<any>, Handler<any>>();
|
||||
|
||||
/**
|
||||
@ -2873,6 +2874,14 @@ export class Page extends EventEmitter {
|
||||
waitForDevicePrompt(): Promise<DeviceRequestPrompt> {
|
||||
throw new Error('Not implemented');
|
||||
}
|
||||
|
||||
[Symbol.dispose](): void {
|
||||
return void this.close().catch(debugError);
|
||||
}
|
||||
|
||||
[Symbol.asyncDispose](): Promise<void> {
|
||||
return this.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user