Skip to content

Commit

Permalink
refactor: add WorkerTarget and OtherTarget (#10181)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed May 15, 2023
1 parent d0c68ff commit 609584a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
33 changes: 19 additions & 14 deletions packages/puppeteer-core/src/common/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {ChromeTargetManager} from './ChromeTargetManager.js';
import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js';
import {FirefoxTargetManager} from './FirefoxTargetManager.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 {TaskQueue} from './TaskQueue.js';
import {waitWithTimeout} from './util.js';
Expand Down Expand Up @@ -318,34 +318,39 @@ export class CDPBrowser extends BrowserBase {
throw new Error('Missing browser context');
}

const createSession = (isAutoAttachEmulated: boolean) => {
return this.#connection._createSession(targetInfo, isAutoAttachEmulated);
};
if (this.#isPageTargetCallback(targetInfo)) {
return new PageTarget(
targetInfo,
session,
context,
this.#targetManager,
(isAutoAttachEmulated: boolean) => {
return this.#connection._createSession(
targetInfo,
isAutoAttachEmulated
);
},
createSession,
this.#ignoreHTTPSErrors,
this.#defaultViewport ?? null,
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,
session,
context,
this.#targetManager,
(isAutoAttachEmulated: boolean) => {
return this.#connection._createSession(
targetInfo,
isAutoAttachEmulated
);
}
createSession
);
};

Expand Down
60 changes: 36 additions & 24 deletions packages/puppeteer-core/src/common/Target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class Target {
#session?: CDPSession;
#targetInfo: Protocol.Target.TargetInfo;
#sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>;
#workerPromise?: Promise<WebWorker>;

/**
* @internal
Expand Down Expand Up @@ -136,28 +135,7 @@ export class Target {
* If the target is not of type `"service_worker"` or `"shared_worker"`, returns `null`.
*/
async worker(): Promise<WebWorker | null> {
if (
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;
return null;
}

url(): string {
Expand Down Expand Up @@ -314,7 +292,9 @@ export class PageTarget extends Target {
if (!this.pagePromise) {
const session = this._session();
this.pagePromise = (
session ? Promise.resolve(session) : this._sessionFactory()(true)
session
? Promise.resolve(session)
: this._sessionFactory()(/* isAutoAttachEmulated=*/ false)
).then(client => {
return CDPPage._create(
client,
Expand All @@ -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 {}

0 comments on commit 609584a

Please sign in to comment.