chore: reimplement document caching (#10878)

This commit is contained in:
jrandolf 2023-09-11 14:07:52 +02:00 committed by GitHub
parent 253d469515
commit eb89720704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 10 deletions

View File

@ -322,14 +322,31 @@ export abstract class Frame extends EventEmitter {
*/
abstract isolatedRealm(): Realm;
#_document: Promise<ElementHandle<Document>> | undefined;
/**
* @internal
*/
async document(): Promise<ElementHandle<Document>> {
// TODO(#10813): Implement document caching.
return await this.evaluateHandle(() => {
return document;
});
#document(): Promise<ElementHandle<Document>> {
if (!this.#_document) {
this.#_document = this.isolatedRealm()
.evaluateHandle(() => {
return document;
})
.then(handle => {
return this.mainRealm().transferHandle(handle);
});
}
return this.#_document;
}
/**
* Used to clear the document handle that has been destroyed.
*
* @internal
*/
clearDocumentHandle(): void {
this.#_document = undefined;
}
/**
@ -441,7 +458,8 @@ export abstract class Frame extends EventEmitter {
async $<Selector extends string>(
selector: Selector
): Promise<ElementHandle<NodeFor<Selector>> | null> {
using document = await this.document();
// eslint-disable-next-line rulesdir/use-using -- This is cached.
const document = await this.#document();
return await document.$(selector);
}
@ -456,7 +474,8 @@ export abstract class Frame extends EventEmitter {
async $$<Selector extends string>(
selector: Selector
): Promise<Array<ElementHandle<NodeFor<Selector>>>> {
using document = await this.document();
// eslint-disable-next-line rulesdir/use-using -- This is cached.
const document = await this.#document();
return await document.$$(selector);
}
@ -494,7 +513,8 @@ export abstract class Frame extends EventEmitter {
...args: Params
): Promise<Awaited<ReturnType<Func>>> {
pageFunction = withSourcePuppeteerURLIfNone(this.$eval.name, pageFunction);
using document = await this.document();
// eslint-disable-next-line rulesdir/use-using -- This is cached.
const document = await this.#document();
return await document.$eval(selector, pageFunction, ...args);
}
@ -532,7 +552,8 @@ export abstract class Frame extends EventEmitter {
...args: Params
): Promise<Awaited<ReturnType<Func>>> {
pageFunction = withSourcePuppeteerURLIfNone(this.$$eval.name, pageFunction);
using document = await this.document();
// eslint-disable-next-line rulesdir/use-using -- This is cached.
const document = await this.#document();
return await document.$$eval(selector, pageFunction, ...args);
}
@ -548,7 +569,8 @@ export abstract class Frame extends EventEmitter {
*/
@throwIfDetached
async $x(expression: string): Promise<Array<ElementHandle<Node>>> {
using document = await this.document();
// eslint-disable-next-line rulesdir/use-using -- This is cached.
const document = await this.#document();
return await document.$x(expression);
}

View File

@ -126,6 +126,9 @@ export class IsolatedWorld extends Realm {
clearContext(): void {
this.#context = Deferred.create();
if (this.#frameOrWorker instanceof CDPFrame) {
this.#frameOrWorker.clearDocumentHandle();
}
}
setContext(context: ExecutionContext): void {

View File

@ -49,6 +49,7 @@ export class Realm extends EventEmitter {
// Note: The Realm is destroyed, so in theory the handle should be as
// well.
this.internalPuppeteerUtil = undefined;
this.#sandbox.environment.clearDocumentHandle();
}
};