refactor: add WorkerTarget and OtherTarget (#10181)
This commit is contained in:
parent
d0c68ff002
commit
609584a8b8
@ -39,7 +39,7 @@ import {ChromeTargetManager} from './ChromeTargetManager.js';
|
|||||||
import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js';
|
import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js';
|
||||||
import {FirefoxTargetManager} from './FirefoxTargetManager.js';
|
import {FirefoxTargetManager} from './FirefoxTargetManager.js';
|
||||||
import {Viewport} from './PuppeteerViewport.js';
|
import {Viewport} from './PuppeteerViewport.js';
|
||||||
import {PageTarget, Target} from './Target.js';
|
import {OtherTarget, PageTarget, Target, WorkerTarget} from './Target.js';
|
||||||
import {TargetManager, TargetManagerEmittedEvents} from './TargetManager.js';
|
import {TargetManager, TargetManagerEmittedEvents} from './TargetManager.js';
|
||||||
import {TaskQueue} from './TaskQueue.js';
|
import {TaskQueue} from './TaskQueue.js';
|
||||||
import {waitWithTimeout} from './util.js';
|
import {waitWithTimeout} from './util.js';
|
||||||
@ -318,34 +318,39 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
throw new Error('Missing browser context');
|
throw new Error('Missing browser context');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const createSession = (isAutoAttachEmulated: boolean) => {
|
||||||
|
return this.#connection._createSession(targetInfo, isAutoAttachEmulated);
|
||||||
|
};
|
||||||
if (this.#isPageTargetCallback(targetInfo)) {
|
if (this.#isPageTargetCallback(targetInfo)) {
|
||||||
return new PageTarget(
|
return new PageTarget(
|
||||||
targetInfo,
|
targetInfo,
|
||||||
session,
|
session,
|
||||||
context,
|
context,
|
||||||
this.#targetManager,
|
this.#targetManager,
|
||||||
(isAutoAttachEmulated: boolean) => {
|
createSession,
|
||||||
return this.#connection._createSession(
|
|
||||||
targetInfo,
|
|
||||||
isAutoAttachEmulated
|
|
||||||
);
|
|
||||||
},
|
|
||||||
this.#ignoreHTTPSErrors,
|
this.#ignoreHTTPSErrors,
|
||||||
this.#defaultViewport ?? null,
|
this.#defaultViewport ?? null,
|
||||||
this.#screenshotTaskQueue
|
this.#screenshotTaskQueue
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return new Target(
|
if (
|
||||||
|
targetInfo.type === 'service_worker' ||
|
||||||
|
targetInfo.type === 'shared_worker'
|
||||||
|
) {
|
||||||
|
return new WorkerTarget(
|
||||||
|
targetInfo,
|
||||||
|
session,
|
||||||
|
context,
|
||||||
|
this.#targetManager,
|
||||||
|
createSession
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return new OtherTarget(
|
||||||
targetInfo,
|
targetInfo,
|
||||||
session,
|
session,
|
||||||
context,
|
context,
|
||||||
this.#targetManager,
|
this.#targetManager,
|
||||||
(isAutoAttachEmulated: boolean) => {
|
createSession
|
||||||
return this.#connection._createSession(
|
|
||||||
targetInfo,
|
|
||||||
isAutoAttachEmulated
|
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -41,7 +41,6 @@ export class Target {
|
|||||||
#session?: CDPSession;
|
#session?: CDPSession;
|
||||||
#targetInfo: Protocol.Target.TargetInfo;
|
#targetInfo: Protocol.Target.TargetInfo;
|
||||||
#sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>;
|
#sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>;
|
||||||
#workerPromise?: Promise<WebWorker>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -136,28 +135,7 @@ export class Target {
|
|||||||
* If the target is not of type `"service_worker"` or `"shared_worker"`, returns `null`.
|
* If the target is not of type `"service_worker"` or `"shared_worker"`, returns `null`.
|
||||||
*/
|
*/
|
||||||
async worker(): Promise<WebWorker | null> {
|
async worker(): Promise<WebWorker | null> {
|
||||||
if (
|
return null;
|
||||||
this.#targetInfo.type !== 'service_worker' &&
|
|
||||||
this.#targetInfo.type !== 'shared_worker'
|
|
||||||
) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (!this.#workerPromise) {
|
|
||||||
// TODO(einbinder): Make workers send their console logs.
|
|
||||||
this.#workerPromise = (
|
|
||||||
this.#session
|
|
||||||
? Promise.resolve(this.#session)
|
|
||||||
: this.#sessionFactory(false)
|
|
||||||
).then(client => {
|
|
||||||
return new WebWorker(
|
|
||||||
client,
|
|
||||||
this.#targetInfo.url,
|
|
||||||
() => {} /* consoleAPICalled */,
|
|
||||||
() => {} /* exceptionThrown */
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return this.#workerPromise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
url(): string {
|
url(): string {
|
||||||
@ -314,7 +292,9 @@ export class PageTarget extends Target {
|
|||||||
if (!this.pagePromise) {
|
if (!this.pagePromise) {
|
||||||
const session = this._session();
|
const session = this._session();
|
||||||
this.pagePromise = (
|
this.pagePromise = (
|
||||||
session ? Promise.resolve(session) : this._sessionFactory()(true)
|
session
|
||||||
|
? Promise.resolve(session)
|
||||||
|
: this._sessionFactory()(/* isAutoAttachEmulated=*/ false)
|
||||||
).then(client => {
|
).then(client => {
|
||||||
return CDPPage._create(
|
return CDPPage._create(
|
||||||
client,
|
client,
|
||||||
@ -338,3 +318,35 @@ export class PageTarget extends Target {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export class WorkerTarget extends Target {
|
||||||
|
#workerPromise?: Promise<WebWorker>;
|
||||||
|
|
||||||
|
override async worker(): Promise<WebWorker | null> {
|
||||||
|
if (!this.#workerPromise) {
|
||||||
|
const session = this._session();
|
||||||
|
// TODO(einbinder): Make workers send their console logs.
|
||||||
|
this.#workerPromise = (
|
||||||
|
session
|
||||||
|
? Promise.resolve(session)
|
||||||
|
: this._sessionFactory()(/* isAutoAttachEmulated=*/ false)
|
||||||
|
).then(client => {
|
||||||
|
return new WebWorker(
|
||||||
|
client,
|
||||||
|
this._getTargetInfo().url,
|
||||||
|
() => {} /* consoleAPICalled */,
|
||||||
|
() => {} /* exceptionThrown */
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return this.#workerPromise;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export class OtherTarget extends Target {}
|
||||||
|
Loading…
Reference in New Issue
Block a user