refactor: remove timeoutSettings from browsingContext (#10624)

This commit is contained in:
Alex Rudenko 2023-07-24 16:03:12 +02:00 committed by GitHub
parent 996d53fc65
commit 4568f1bba5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 32 deletions

View File

@ -8,7 +8,6 @@ import type {CDPSession, Connection as CDPConnection} from '../Connection.js';
import {ProtocolError, TargetCloseError, TimeoutError} from '../Errors.js'; import {ProtocolError, TargetCloseError, TimeoutError} from '../Errors.js';
import {EventEmitter} from '../EventEmitter.js'; import {EventEmitter} from '../EventEmitter.js';
import {PuppeteerLifeCycleEvent} from '../LifecycleWatcher.js'; import {PuppeteerLifeCycleEvent} from '../LifecycleWatcher.js';
import {TimeoutSettings} from '../TimeoutSettings.js';
import {getPageContent, setPageContent, waitWithTimeout} from '../util.js'; import {getPageContent, setPageContent, waitWithTimeout} from '../util.js';
import {Connection} from './Connection.js'; import {Connection} from './Connection.js';
@ -111,19 +110,13 @@ export class CDPSessionWrapper extends EventEmitter implements CDPSession {
* @internal * @internal
*/ */
export class BrowsingContext extends Realm { export class BrowsingContext extends Realm {
#timeoutSettings: TimeoutSettings;
#id: string; #id: string;
#url: string; #url: string;
#cdpSession: CDPSession; #cdpSession: CDPSession;
constructor( constructor(connection: Connection, info: Bidi.BrowsingContext.Info) {
connection: Connection,
timeoutSettings: TimeoutSettings,
info: Bidi.BrowsingContext.Info
) {
super(connection, info.context); super(connection, info.context);
this.connection = connection; this.connection = connection;
this.#timeoutSettings = timeoutSettings;
this.#id = info.context; this.#id = info.context;
this.#url = info.url; this.#url = info.url;
this.#cdpSession = new CDPSessionWrapper(this); this.#cdpSession = new CDPSessionWrapper(this);
@ -161,14 +154,11 @@ export class BrowsingContext extends Realm {
options: { options: {
referer?: string; referer?: string;
referrerPolicy?: string; referrerPolicy?: string;
timeout?: number; timeout: number;
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[]; waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
} = {} }
): Promise<string | null> { ): Promise<string | null> {
const { const {waitUntil = 'load', timeout} = options;
waitUntil = 'load',
timeout = this.#timeoutSettings.navigationTimeout(),
} = options;
const readinessState = lifeCycleToReadinessState.get( const readinessState = lifeCycleToReadinessState.get(
getWaitUntilSingle(waitUntil) getWaitUntilSingle(waitUntil)
@ -197,11 +187,8 @@ export class BrowsingContext extends Realm {
} }
} }
async reload(options: WaitForOptions = {}): Promise<void> { async reload(options: WaitForOptions & {timeout: number}): Promise<void> {
const { const {waitUntil = 'load', timeout} = options;
waitUntil = 'load',
timeout = this.#timeoutSettings.navigationTimeout(),
} = options;
const readinessState = lifeCycleToReadinessState.get( const readinessState = lifeCycleToReadinessState.get(
getWaitUntilSingle(waitUntil) getWaitUntilSingle(waitUntil)
@ -220,14 +207,11 @@ export class BrowsingContext extends Realm {
async setContent( async setContent(
html: string, html: string,
options: { options: {
timeout?: number; timeout: number;
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[]; waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
} }
): Promise<void> { ): Promise<void> {
const { const {waitUntil = 'load', timeout} = options;
waitUntil = 'load',
timeout = this.#timeoutSettings.navigationTimeout(),
} = options;
const waitUntilEvent = lifeCycleToSubscribedEvent.get( const waitUntilEvent = lifeCycleToSubscribedEvent.get(
getWaitUntilSingle(waitUntil) getWaitUntilSingle(waitUntil)

View File

@ -131,7 +131,10 @@ export class Frame extends BaseFrame {
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[]; waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
} }
): Promise<HTTPResponse | null> { ): Promise<HTTPResponse | null> {
const navigationId = await this.#context.goto(url, options); const navigationId = await this.#context.goto(url, {
...options,
timeout: options?.timeout ?? this.#timeoutSettings.navigationTimeout(),
});
return this.#page.getNavigationResponse(navigationId); return this.#page.getNavigationResponse(navigationId);
} }
@ -142,7 +145,10 @@ export class Frame extends BaseFrame {
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[]; waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
} }
): Promise<void> { ): Promise<void> {
return this.#context.setContent(html, options); return this.#context.setContent(html, {
...options,
timeout: options?.timeout ?? this.#timeoutSettings.navigationTimeout(),
});
} }
override content(): Promise<string> { override content(): Promise<string> {

View File

@ -223,11 +223,7 @@ export class Page extends PageBase {
!this.frame(info.context) && !this.frame(info.context) &&
(this.frame(info.parent ?? '') || !this.#frameTree.getMainFrame()) (this.frame(info.parent ?? '') || !this.#frameTree.getMainFrame())
) { ) {
const context = new BrowsingContext( const context = new BrowsingContext(this.#connection, info);
this.#connection,
this.#timeoutSettings,
info
);
this.#connection.registerBrowsingContexts(context); this.#connection.registerBrowsingContexts(context);
const frame = new Frame( const frame = new Frame(
@ -396,7 +392,13 @@ export class Page extends PageBase {
response.url() === this.url() response.url() === this.url()
); );
}), }),
this.mainFrame().context().reload(options), this.mainFrame()
.context()
.reload({
...options,
timeout:
options?.timeout ?? this.#timeoutSettings.navigationTimeout(),
}),
]); ]);
return response; return response;