diff --git a/docs/api/puppeteer.elementhandle._symbol.asyncdispose_.md b/docs/api/puppeteer.elementhandle._symbol.asyncdispose_.md new file mode 100644 index 00000000..e1302ac7 --- /dev/null +++ b/docs/api/puppeteer.elementhandle._symbol.asyncdispose_.md @@ -0,0 +1,17 @@ +--- +sidebar_label: ElementHandle.[Symbol.asyncDispose] +--- + +# ElementHandle.\[Symbol.asyncDispose\]() method + +#### Signature: + +```typescript +class ElementHandle { + [Symbol.asyncDispose](): Promise; +} +``` + +**Returns:** + +Promise<void> diff --git a/docs/api/puppeteer.elementhandle._symbol.dispose_.md b/docs/api/puppeteer.elementhandle._symbol.dispose_.md new file mode 100644 index 00000000..88651393 --- /dev/null +++ b/docs/api/puppeteer.elementhandle._symbol.dispose_.md @@ -0,0 +1,17 @@ +--- +sidebar_label: ElementHandle.[Symbol.dispose] +--- + +# ElementHandle.\[Symbol.dispose\]() method + +#### Signature: + +```typescript +class ElementHandle { + [Symbol.dispose](): void; +} +``` + +**Returns:** + +void diff --git a/docs/api/puppeteer.elementhandle.md b/docs/api/puppeteer.elementhandle.md index e30a42bf..52099ddf 100644 --- a/docs/api/puppeteer.elementhandle.md +++ b/docs/api/puppeteer.elementhandle.md @@ -49,6 +49,8 @@ The constructor for this class is marked as internal. Third-party code should no | Method | Modifiers | Description | | -------------------------------------------------------------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [\[Symbol.asyncDispose\]()](./puppeteer.elementhandle._symbol.asyncdispose_.md) | | | +| [\[Symbol.dispose\]()](./puppeteer.elementhandle._symbol.dispose_.md) | | | | [$(selector)](./puppeteer.elementhandle._.md) | | Queries the current element for an element matching the given selector. | | [$$(selector)](./puppeteer.elementhandle.__.md) | | Queries the current element for all elements matching the given selector. | | [$$eval(selector, pageFunction, args)](./puppeteer.elementhandle.__eval.md) | |

Runs the given function on an array of elements matching the given selector in the current element.

If the given function returns a promise, then this method will wait till the promise resolves.

| diff --git a/docs/api/puppeteer.jshandle._symbol.asyncdispose_.md b/docs/api/puppeteer.jshandle._symbol.asyncdispose_.md new file mode 100644 index 00000000..4d64d626 --- /dev/null +++ b/docs/api/puppeteer.jshandle._symbol.asyncdispose_.md @@ -0,0 +1,17 @@ +--- +sidebar_label: JSHandle.[Symbol.asyncDispose] +--- + +# JSHandle.\[Symbol.asyncDispose\]() method + +#### Signature: + +```typescript +class JSHandle { + [Symbol.asyncDispose](): Promise; +} +``` + +**Returns:** + +Promise<void> diff --git a/docs/api/puppeteer.jshandle._symbol.dispose_.md b/docs/api/puppeteer.jshandle._symbol.dispose_.md new file mode 100644 index 00000000..33aac420 --- /dev/null +++ b/docs/api/puppeteer.jshandle._symbol.dispose_.md @@ -0,0 +1,17 @@ +--- +sidebar_label: JSHandle.[Symbol.dispose] +--- + +# JSHandle.\[Symbol.dispose\]() method + +#### Signature: + +```typescript +class JSHandle { + [Symbol.dispose](): void; +} +``` + +**Returns:** + +void diff --git a/docs/api/puppeteer.jshandle.md b/docs/api/puppeteer.jshandle.md index 46d29459..cd1c7ecf 100644 --- a/docs/api/puppeteer.jshandle.md +++ b/docs/api/puppeteer.jshandle.md @@ -13,9 +13,11 @@ Handles can be used as arguments for any evaluation function such as [Page.$eval #### Signature: ```typescript -export declare abstract class JSHandle +export declare abstract class JSHandle implements Disposable, AsyncDisposable ``` +**Implements:** Disposable, AsyncDisposable + ## 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 `JSHandle` class. @@ -36,6 +38,8 @@ const windowHandle = await page.evaluateHandle(() => window); | Method | Modifiers | Description | | ---------------------------------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [\[Symbol.asyncDispose\]()](./puppeteer.jshandle._symbol.asyncdispose_.md) | | | +| [\[Symbol.dispose\]()](./puppeteer.jshandle._symbol.dispose_.md) | | | | [asElement()](./puppeteer.jshandle.aselement.md) | | Either null or the handle itself if the handle is an instance of [ElementHandle](./puppeteer.elementhandle.md). | | [dispose()](./puppeteer.jshandle.dispose.md) | | Releases the object referenced by the handle for garbage collection. | | [evaluate(pageFunction, args)](./puppeteer.jshandle.evaluate.md) | | Evaluates the given function with the current handle as its first argument. | diff --git a/packages/puppeteer-core/src/api/ElementHandle.ts b/packages/puppeteer-core/src/api/ElementHandle.ts index 65ba1e6a..dbac3dda 100644 --- a/packages/puppeteer-core/src/api/ElementHandle.ts +++ b/packages/puppeteer-core/src/api/ElementHandle.ts @@ -250,8 +250,8 @@ export abstract class ElementHandle< /** * @internal */ - override async dispose(): Promise { - return await this.handle.dispose(); + override dispose(): Promise { + return this.handle.dispose(); } override asElement(): ElementHandle { @@ -1323,6 +1323,14 @@ export abstract class ElementHandle< * ``` */ abstract autofill(data: AutofillData): Promise; + + override [Symbol.dispose](): void { + return void this.dispose().catch(debugError); + } + + override [Symbol.asyncDispose](): Promise { + return this.dispose(); + } } /** diff --git a/packages/puppeteer-core/src/api/JSHandle.ts b/packages/puppeteer-core/src/api/JSHandle.ts index 3b4635bc..ef7f09af 100644 --- a/packages/puppeteer-core/src/api/JSHandle.ts +++ b/packages/puppeteer-core/src/api/JSHandle.ts @@ -16,7 +16,9 @@ import Protocol from 'devtools-protocol'; +import {Symbol} from '../../third_party/disposablestack/disposablestack.js'; import {EvaluateFuncWith, HandleFor, HandleOr} from '../common/types.js'; +import {debugError} from '../common/util.js'; import {ElementHandle} from './ElementHandle.js'; @@ -41,7 +43,9 @@ import {ElementHandle} from './ElementHandle.js'; * * @public */ -export abstract class JSHandle { +export abstract class JSHandle + implements Disposable, AsyncDisposable +{ /** * Used for nominally typing {@link JSHandle}. */ @@ -150,4 +154,12 @@ export abstract class JSHandle { * backing this handle. */ abstract remoteObject(): Protocol.Runtime.RemoteObject; + + [Symbol.dispose](): void { + return void this.dispose().catch(debugError); + } + + [Symbol.asyncDispose](): Promise { + return this.dispose(); + } }