From c67141f3aeeb86148791d36394e932a70b94d26e Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Wed, 30 Aug 2023 13:10:06 +0200 Subject: [PATCH] chore: implement Disposable/AsyncDisposable on Page and Browser (#10809) --- .../puppeteer.browser._symbol.asyncdispose_.md | 17 +++++++++++++++++ docs/api/puppeteer.browser._symbol.dispose_.md | 17 +++++++++++++++++ docs/api/puppeteer.browser.md | 6 +++++- .../api/puppeteer.page._symbol.asyncdispose_.md | 17 +++++++++++++++++ docs/api/puppeteer.page._symbol.dispose_.md | 17 +++++++++++++++++ docs/api/puppeteer.page.md | 6 +++++- packages/puppeteer-core/src/api/Browser.ts | 16 ++++++++++++++-- packages/puppeteer-core/src/api/Page.ts | 11 ++++++++++- 8 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 docs/api/puppeteer.browser._symbol.asyncdispose_.md create mode 100644 docs/api/puppeteer.browser._symbol.dispose_.md create mode 100644 docs/api/puppeteer.page._symbol.asyncdispose_.md create mode 100644 docs/api/puppeteer.page._symbol.dispose_.md diff --git a/docs/api/puppeteer.browser._symbol.asyncdispose_.md b/docs/api/puppeteer.browser._symbol.asyncdispose_.md new file mode 100644 index 00000000..50e0ae9b --- /dev/null +++ b/docs/api/puppeteer.browser._symbol.asyncdispose_.md @@ -0,0 +1,17 @@ +--- +sidebar_label: Browser.[Symbol.asyncDispose] +--- + +# Browser.\[Symbol.asyncDispose\]() method + +#### Signature: + +```typescript +class Browser { + [Symbol.asyncDispose](): Promise; +} +``` + +**Returns:** + +Promise<void> diff --git a/docs/api/puppeteer.browser._symbol.dispose_.md b/docs/api/puppeteer.browser._symbol.dispose_.md new file mode 100644 index 00000000..e7a9f657 --- /dev/null +++ b/docs/api/puppeteer.browser._symbol.dispose_.md @@ -0,0 +1,17 @@ +--- +sidebar_label: Browser.[Symbol.dispose] +--- + +# Browser.\[Symbol.dispose\]() method + +#### Signature: + +```typescript +class Browser { + [Symbol.dispose](): void; +} +``` + +**Returns:** + +void diff --git a/docs/api/puppeteer.browser.md b/docs/api/puppeteer.browser.md index cf22ac7e..634c6217 100644 --- a/docs/api/puppeteer.browser.md +++ b/docs/api/puppeteer.browser.md @@ -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. | diff --git a/docs/api/puppeteer.page._symbol.asyncdispose_.md b/docs/api/puppeteer.page._symbol.asyncdispose_.md new file mode 100644 index 00000000..578344b2 --- /dev/null +++ b/docs/api/puppeteer.page._symbol.asyncdispose_.md @@ -0,0 +1,17 @@ +--- +sidebar_label: Page.[Symbol.asyncDispose] +--- + +# Page.\[Symbol.asyncDispose\]() method + +#### Signature: + +```typescript +class Page { + [Symbol.asyncDispose](): Promise; +} +``` + +**Returns:** + +Promise<void> diff --git a/docs/api/puppeteer.page._symbol.dispose_.md b/docs/api/puppeteer.page._symbol.dispose_.md new file mode 100644 index 00000000..f1bfc812 --- /dev/null +++ b/docs/api/puppeteer.page._symbol.dispose_.md @@ -0,0 +1,17 @@ +--- +sidebar_label: Page.[Symbol.dispose] +--- + +# Page.\[Symbol.dispose\]() method + +#### Signature: + +```typescript +class Page { + [Symbol.dispose](): void; +} +``` + +**Returns:** + +void diff --git a/docs/api/puppeteer.page.md b/docs/api/puppeteer.page.md index 9974ae71..bc0f718f 100644 --- a/docs/api/puppeteer.page.md +++ b/docs/api/puppeteer.page.md @@ -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 document.querySelector within the page. If no element matches the selector, the return value resolves to null. | | [$$(selector)](./puppeteer.page.__.md) | | The method runs document.querySelectorAll within the page. If no elements match the selector, the return value resolves to []. | | [$$eval(selector, pageFunction, args)](./puppeteer.page.__eval.md) | | This method runs Array.from(document.querySelectorAll(selector)) within the page and passes the result as the first argument to the pageFunction. | diff --git a/packages/puppeteer-core/src/api/Browser.ts b/packages/puppeteer-core/src/api/Browser.ts index 560c4649..a35ded93 100644 --- a/packages/puppeteer-core/src/api/Browser.ts +++ b/packages/puppeteer-core/src/api/Browser.ts @@ -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 { + return this.close(); + } } /** * @public diff --git a/packages/puppeteer-core/src/api/Page.ts b/packages/puppeteer-core/src/api/Page.ts index 3a5f346e..acd042ea 100644 --- a/packages/puppeteer-core/src/api/Page.ts +++ b/packages/puppeteer-core/src/api/Page.ts @@ -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>(); /** @@ -2873,6 +2874,14 @@ export class Page extends EventEmitter { waitForDevicePrompt(): Promise { throw new Error('Not implemented'); } + + [Symbol.dispose](): void { + return void this.close().catch(debugError); + } + + [Symbol.asyncDispose](): Promise { + return this.close(); + } } /**