Skip to content

Commit

Permalink
fix: types in Connection.ts to be compatible with strict mode Typescr…
Browse files Browse the repository at this point in the history
…ipt (#7919)

Issues: #6769
  • Loading branch information
OrKoN committed Jan 21, 2022
1 parent a8ec0aa commit d80d602
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/common/Connection.ts
Expand Up @@ -71,7 +71,7 @@ export class Connection extends EventEmitter {
this._transport.onclose = this._onClose.bind(this);
}

static fromSession(session: CDPSession): Connection {
static fromSession(session: CDPSession): Connection | undefined {
return session._connection;
}

Expand Down Expand Up @@ -170,8 +170,8 @@ export class Connection extends EventEmitter {
_onClose(): void {
if (this._closed) return;
this._closed = true;
this._transport.onmessage = null;
this._transport.onclose = null;
this._transport.onmessage = undefined;
this._transport.onclose = undefined;
for (const callback of this._callbacks.values())
callback.reject(
rewriteError(
Expand Down Expand Up @@ -201,7 +201,11 @@ export class Connection extends EventEmitter {
targetId: targetInfo.targetId,
flatten: true,
});
return this._sessions.get(sessionId);
const session = this._sessions.get(sessionId);
if (!session) {
throw new Error('CDPSession creation failed.');
}
return session;
}
}

Expand Down Expand Up @@ -254,7 +258,7 @@ export class CDPSession extends EventEmitter {
/**
* @internal
*/
_connection: Connection;
_connection?: Connection;
private _sessionId: string;
private _targetType: string;
private _callbacks: Map<number, ConnectionCallback> = new Map();
Expand All @@ -269,7 +273,7 @@ export class CDPSession extends EventEmitter {
this._sessionId = sessionId;
}

connection(): Connection {
connection(): Connection | undefined {
return this._connection;
}

Expand Down Expand Up @@ -307,8 +311,8 @@ export class CDPSession extends EventEmitter {
* @internal
*/
_onMessage(object: CDPSessionOnMessageObject): void {
if (object.id && this._callbacks.has(object.id)) {
const callback = this._callbacks.get(object.id);
const callback = object.id ? this._callbacks.get(object.id) : undefined;
if (object.id && callback) {
this._callbacks.delete(object.id);
if (object.error)
callback.reject(
Expand Down Expand Up @@ -347,7 +351,7 @@ export class CDPSession extends EventEmitter {
)
);
this._callbacks.clear();
this._connection = null;
this._connection = undefined;
this.emit(CDPSessionEmittedEvents.Disconnected);
}

Expand Down Expand Up @@ -386,6 +390,6 @@ function rewriteError(
originalMessage?: string
): Error {
error.message = message;
error.originalMessage = originalMessage;
error.originalMessage = originalMessage ?? error.originalMessage;
return error;
}
4 changes: 2 additions & 2 deletions src/common/ConnectionTransport.ts
Expand Up @@ -18,8 +18,8 @@
* @public
*/
export interface ConnectionTransport {
send(string);
close();
send(message: string): void;
close(): void;
onmessage?: (message: string) => void;
onclose?: () => void;
}

0 comments on commit d80d602

Please sign in to comment.