fix: types in Browser.ts to be compatible with strict mode Typescript (#7918)
Issues: #6769
This commit is contained in:
parent
08c0e8b75d
commit
a8ec0aadc9
@ -268,7 +268,7 @@ export class Browser extends EventEmitter {
|
||||
this._closeCallback = closeCallback || function (): void {};
|
||||
this._targetFilterCallback = targetFilterCallback || ((): boolean => true);
|
||||
|
||||
this._defaultContext = new BrowserContext(this._connection, this, null);
|
||||
this._defaultContext = new BrowserContext(this._connection, this);
|
||||
this._contexts = new Map();
|
||||
for (const contextId of contextIds)
|
||||
this._contexts.set(
|
||||
@ -296,7 +296,7 @@ export class Browser extends EventEmitter {
|
||||
* {@link Puppeteer.connect}.
|
||||
*/
|
||||
process(): ChildProcess | null {
|
||||
return this._process;
|
||||
return this._process ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -357,8 +357,11 @@ export class Browser extends EventEmitter {
|
||||
* Used by BrowserContext directly so cannot be marked private.
|
||||
*/
|
||||
async _disposeContext(contextId?: string): Promise<void> {
|
||||
if (!contextId) {
|
||||
return;
|
||||
}
|
||||
await this._connection.send('Target.disposeBrowserContext', {
|
||||
browserContextId: contextId || undefined,
|
||||
browserContextId: contextId,
|
||||
});
|
||||
this._contexts.delete(contextId);
|
||||
}
|
||||
@ -373,6 +376,10 @@ export class Browser extends EventEmitter {
|
||||
? this._contexts.get(browserContextId)
|
||||
: this._defaultContext;
|
||||
|
||||
if (!context) {
|
||||
throw new Error('Missing browser context');
|
||||
}
|
||||
|
||||
const shouldAttachToTarget = this._targetFilterCallback(targetInfo);
|
||||
if (!shouldAttachToTarget) {
|
||||
this._ignoredTargets.add(targetInfo.targetId);
|
||||
@ -384,7 +391,7 @@ export class Browser extends EventEmitter {
|
||||
context,
|
||||
() => this._connection.createSession(targetInfo),
|
||||
this._ignoreHTTPSErrors,
|
||||
this._defaultViewport,
|
||||
this._defaultViewport ?? null,
|
||||
this._screenshotTaskQueue
|
||||
);
|
||||
assert(
|
||||
@ -402,6 +409,11 @@ export class Browser extends EventEmitter {
|
||||
private async _targetDestroyed(event: { targetId: string }): Promise<void> {
|
||||
if (this._ignoredTargets.has(event.targetId)) return;
|
||||
const target = this._targets.get(event.targetId);
|
||||
if (!target) {
|
||||
throw new Error(
|
||||
`Missing target in _targetDestroyed (id = ${event.targetId})`
|
||||
);
|
||||
}
|
||||
target._initializedCallback(false);
|
||||
this._targets.delete(event.targetId);
|
||||
target._closedCallback();
|
||||
@ -418,7 +430,11 @@ export class Browser extends EventEmitter {
|
||||
): void {
|
||||
if (this._ignoredTargets.has(event.targetInfo.targetId)) return;
|
||||
const target = this._targets.get(event.targetInfo.targetId);
|
||||
assert(target, 'target should exist before targetInfoChanged');
|
||||
if (!target) {
|
||||
throw new Error(
|
||||
`Missing target in targetInfoChanged (id = ${event.targetInfo.targetId})`
|
||||
);
|
||||
}
|
||||
const previousURL = target.url();
|
||||
const wasInitialized = target._isInitialized;
|
||||
target._targetInfoChanged(event.targetInfo);
|
||||
@ -469,11 +485,19 @@ export class Browser extends EventEmitter {
|
||||
browserContextId: contextId || undefined,
|
||||
});
|
||||
const target = this._targets.get(targetId);
|
||||
assert(
|
||||
await target._initializedPromise,
|
||||
'Failed to create target for page'
|
||||
);
|
||||
if (!target) {
|
||||
throw new Error(`Missing target for page (id = ${targetId})`);
|
||||
}
|
||||
const initialized = await target._initializedPromise;
|
||||
if (!initialized) {
|
||||
throw new Error(`Failed to create target for page (id = ${targetId})`);
|
||||
}
|
||||
const page = await target.page();
|
||||
if (!page) {
|
||||
throw new Error(
|
||||
`Failed to create a page for context (id = ${contextId})`
|
||||
);
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
@ -491,7 +515,13 @@ export class Browser extends EventEmitter {
|
||||
* The target associated with the browser.
|
||||
*/
|
||||
target(): Target {
|
||||
return this.targets().find((target) => target.type() === 'browser');
|
||||
const browserTarget = this.targets().find(
|
||||
(target) => target.type() === 'browser'
|
||||
);
|
||||
if (!browserTarget) {
|
||||
throw new Error('Browser target is not found');
|
||||
}
|
||||
return browserTarget;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -728,7 +758,7 @@ export class BrowserContext extends EventEmitter {
|
||||
.filter((target) => target.type() === 'page')
|
||||
.map((target) => target.page())
|
||||
);
|
||||
return pages.filter((page) => !!page);
|
||||
return pages.filter((page): page is Page => !!page);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user