apply same for Browser

This commit is contained in:
Simon Knott 2024-08-01 16:11:44 +02:00
parent 461818dfd2
commit 2e20ac19b2
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
6 changed files with 16 additions and 10 deletions

View file

@ -49,7 +49,7 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.BrowserInitializer) {
super(parent, type, guid, initializer);
this._name = initializer.name;
this._channel.on('close', () => this._didClose());
this._channel.on('close', ({ reason }) => this._didClose(reason));
this._closedPromise = new Promise(f => this.once(Events.Browser.Disconnected, f));
}
@ -136,10 +136,9 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
}
async close(options: { reason?: string } = {}): Promise<void> {
this._closeReason = options.reason;
try {
if (this._shouldCloseConnectionOnClose)
this._connection.close();
this._connection.close(options.reason);
else
await this._channel.close(options);
await this._closedPromise;
@ -150,8 +149,9 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
}
}
_didClose() {
_didClose(reason?: string) {
this._isConnected = false;
this._closeReason = reason;
this.emit(Events.Browser.Disconnected, this);
}
}

View file

@ -608,7 +608,9 @@ scheme.BrowserInitializer = tObject({
version: tString,
name: tString,
});
scheme.BrowserCloseEvent = tOptional(tObject({}));
scheme.BrowserCloseEvent = tObject({
reason: tOptional(tString),
});
scheme.BrowserCloseParams = tObject({
reason: tOptional(tString),
});

View file

@ -155,7 +155,7 @@ export abstract class Browser extends SdkObject {
context._browserClosed();
if (this._defaultContext)
this._defaultContext._browserClosed();
this.emit(Browser.Events.Disconnected);
this.emit(Browser.Events.Disconnected, { reason: this._closeReason });
this.instrumentation.onBrowserClose(this);
}

View file

@ -34,11 +34,11 @@ export class BrowserDispatcher extends Dispatcher<Browser, channels.BrowserChann
constructor(scope: BrowserTypeDispatcher, browser: Browser) {
super(scope, browser, 'Browser', { version: browser.version(), name: browser.options.name });
this.addObjectListener(Browser.Events.Disconnected, () => this._didClose());
this.addObjectListener(Browser.Events.Disconnected, ({ reason }) => this._didClose(reason));
}
_didClose() {
this._dispatchEvent('close');
_didClose(reason?: string) {
this._dispatchEvent('close', { reason });
this._dispose();
}

View file

@ -1131,7 +1131,9 @@ export interface BrowserChannel extends BrowserEventTarget, Channel {
startTracing(params: BrowserStartTracingParams, metadata?: CallMetadata): Promise<BrowserStartTracingResult>;
stopTracing(params?: BrowserStopTracingParams, metadata?: CallMetadata): Promise<BrowserStopTracingResult>;
}
export type BrowserCloseEvent = {};
export type BrowserCloseEvent = {
reason?: string,
};
export type BrowserCloseParams = {
reason?: string,
};

View file

@ -1009,6 +1009,8 @@ Browser:
events:
close:
parameters:
reason: string?
ConsoleMessage:
type: mixin