mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: reimplement document caching (#10878)
This commit is contained in:
parent
253d469515
commit
eb89720704
@ -322,15 +322,32 @@ 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(() => {
|
||||
#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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user