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,14 +322,31 @@ export abstract class Frame extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
abstract isolatedRealm(): Realm;
|
abstract isolatedRealm(): Realm;
|
||||||
|
|
||||||
|
#_document: Promise<ElementHandle<Document>> | undefined;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
async document(): Promise<ElementHandle<Document>> {
|
#document(): Promise<ElementHandle<Document>> {
|
||||||
// TODO(#10813): Implement document caching.
|
if (!this.#_document) {
|
||||||
return await this.evaluateHandle(() => {
|
this.#_document = this.isolatedRealm()
|
||||||
return document;
|
.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>(
|
async $<Selector extends string>(
|
||||||
selector: Selector
|
selector: Selector
|
||||||
): Promise<ElementHandle<NodeFor<Selector>> | null> {
|
): 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);
|
return await document.$(selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -456,7 +474,8 @@ export abstract class Frame extends EventEmitter {
|
|||||||
async $$<Selector extends string>(
|
async $$<Selector extends string>(
|
||||||
selector: Selector
|
selector: Selector
|
||||||
): Promise<Array<ElementHandle<NodeFor<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);
|
return await document.$$(selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -494,7 +513,8 @@ export abstract class Frame extends EventEmitter {
|
|||||||
...args: Params
|
...args: Params
|
||||||
): Promise<Awaited<ReturnType<Func>>> {
|
): Promise<Awaited<ReturnType<Func>>> {
|
||||||
pageFunction = withSourcePuppeteerURLIfNone(this.$eval.name, pageFunction);
|
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);
|
return await document.$eval(selector, pageFunction, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -532,7 +552,8 @@ export abstract class Frame extends EventEmitter {
|
|||||||
...args: Params
|
...args: Params
|
||||||
): Promise<Awaited<ReturnType<Func>>> {
|
): Promise<Awaited<ReturnType<Func>>> {
|
||||||
pageFunction = withSourcePuppeteerURLIfNone(this.$$eval.name, pageFunction);
|
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);
|
return await document.$$eval(selector, pageFunction, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -548,7 +569,8 @@ export abstract class Frame extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
@throwIfDetached
|
@throwIfDetached
|
||||||
async $x(expression: string): Promise<Array<ElementHandle<Node>>> {
|
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);
|
return await document.$x(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,6 +126,9 @@ export class IsolatedWorld extends Realm {
|
|||||||
|
|
||||||
clearContext(): void {
|
clearContext(): void {
|
||||||
this.#context = Deferred.create();
|
this.#context = Deferred.create();
|
||||||
|
if (this.#frameOrWorker instanceof CDPFrame) {
|
||||||
|
this.#frameOrWorker.clearDocumentHandle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setContext(context: ExecutionContext): void {
|
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
|
// Note: The Realm is destroyed, so in theory the handle should be as
|
||||||
// well.
|
// well.
|
||||||
this.internalPuppeteerUtil = undefined;
|
this.internalPuppeteerUtil = undefined;
|
||||||
|
this.#sandbox.environment.clearDocumentHandle();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user