fix: types in Connection.ts to be compatible with strict mode Typescript (#7919)

Issues: #6769
This commit is contained in:
Alex Rudenko 2022-01-21 11:46:18 +01:00 committed by Alex Rudenko
parent a8ec0aadc9
commit d80d6027ea
2 changed files with 16 additions and 12 deletions

View File

@ -71,7 +71,7 @@ export class Connection extends EventEmitter {
this._transport.onclose = this._onClose.bind(this); this._transport.onclose = this._onClose.bind(this);
} }
static fromSession(session: CDPSession): Connection { static fromSession(session: CDPSession): Connection | undefined {
return session._connection; return session._connection;
} }
@ -170,8 +170,8 @@ export class Connection extends EventEmitter {
_onClose(): void { _onClose(): void {
if (this._closed) return; if (this._closed) return;
this._closed = true; this._closed = true;
this._transport.onmessage = null; this._transport.onmessage = undefined;
this._transport.onclose = null; this._transport.onclose = undefined;
for (const callback of this._callbacks.values()) for (const callback of this._callbacks.values())
callback.reject( callback.reject(
rewriteError( rewriteError(
@ -201,7 +201,11 @@ export class Connection extends EventEmitter {
targetId: targetInfo.targetId, targetId: targetInfo.targetId,
flatten: true, flatten: true,
}); });
return this._sessions.get(sessionId); const session = this._sessions.get(sessionId);
if (!session) {
throw new Error('CDPSession creation failed.');
}
return session;
} }
} }
@ -254,7 +258,7 @@ export class CDPSession extends EventEmitter {
/** /**
* @internal * @internal
*/ */
_connection: Connection; _connection?: Connection;
private _sessionId: string; private _sessionId: string;
private _targetType: string; private _targetType: string;
private _callbacks: Map<number, ConnectionCallback> = new Map(); private _callbacks: Map<number, ConnectionCallback> = new Map();
@ -269,7 +273,7 @@ export class CDPSession extends EventEmitter {
this._sessionId = sessionId; this._sessionId = sessionId;
} }
connection(): Connection { connection(): Connection | undefined {
return this._connection; return this._connection;
} }
@ -307,8 +311,8 @@ export class CDPSession extends EventEmitter {
* @internal * @internal
*/ */
_onMessage(object: CDPSessionOnMessageObject): void { _onMessage(object: CDPSessionOnMessageObject): void {
if (object.id && this._callbacks.has(object.id)) { const callback = object.id ? this._callbacks.get(object.id) : undefined;
const callback = this._callbacks.get(object.id); if (object.id && callback) {
this._callbacks.delete(object.id); this._callbacks.delete(object.id);
if (object.error) if (object.error)
callback.reject( callback.reject(
@ -347,7 +351,7 @@ export class CDPSession extends EventEmitter {
) )
); );
this._callbacks.clear(); this._callbacks.clear();
this._connection = null; this._connection = undefined;
this.emit(CDPSessionEmittedEvents.Disconnected); this.emit(CDPSessionEmittedEvents.Disconnected);
} }
@ -386,6 +390,6 @@ function rewriteError(
originalMessage?: string originalMessage?: string
): Error { ): Error {
error.message = message; error.message = message;
error.originalMessage = originalMessage; error.originalMessage = originalMessage ?? error.originalMessage;
return error; return error;
} }

View File

@ -18,8 +18,8 @@
* @public * @public
*/ */
export interface ConnectionTransport { export interface ConnectionTransport {
send(string); send(message: string): void;
close(); close(): void;
onmessage?: (message: string) => void; onmessage?: (message: string) => void;
onclose?: () => void; onclose?: () => void;
} }