Skip to content

Commit

Permalink
[fix]: type cleanup
Browse files Browse the repository at this point in the history
Fix some types and add a couple convenience methods.

Add convenience for Browser.close since chrome
sometimes exits before sending the response to
Browser.close.
  • Loading branch information
krisselden committed Jun 21, 2019
1 parent edb329d commit a9d5573
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 79 deletions.
72 changes: 48 additions & 24 deletions @tracerbench/protocol-connection/src/newEventHook.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import Protocol from "devtools-protocol";
import { ProtocolConnection, SessionID, TargetID, TargetInfo } from "../types";
import {
SessionConnection,
SessionID,
SessionIdentifier,
TargetID,
TargetInfo,
} from "../types";

const CONNECTION = Symbol("connection");

export type NewConnection = (session: Session) => ProtocolConnection;
export type NewConnection = (session: Session) => SessionConnection;
export type DestroyConnection = (sessionId: SessionID) => void;

export type EventHook = (event: string, params?: any) => void;
export type GetConnection = (
session: SessionIdentifier,
) => ProtocolConnection | undefined;
export type GetConnection = {
(session: SessionIdentifier, throwIfNotAttached?: true): SessionConnection;
(session: SessionIdentifier, throwIfNotAttached: boolean | undefined):
| SessionConnection
| undefined;
};
export type ClearSessions = () => void;

export interface Session {
Expand All @@ -22,18 +31,9 @@ interface Attachment {
sessionId: SessionID;
targetId: TargetID;
targetInfo: TargetInfo;
[CONNECTION]: ProtocolConnection | undefined;
[CONNECTION]: SessionConnection | undefined;
}

export type SessionIdentifier =
| SessionID
| {
targetId: TargetID;
}
| {
sessionId: SessionID;
};

export default function newEventHook(
newConnection: NewConnection,
detachConnection: DestroyConnection,
Expand All @@ -57,7 +57,7 @@ export default function newEventHook(
}
}

function getSessionId(session: SessionIdentifier) {
function getSessionId(session: SessionIdentifier, throwIfNotAttached = true) {
let sessionId: SessionID | undefined;
if (typeof session === "string") {
sessionId = session;
Expand All @@ -69,19 +69,31 @@ export default function newEventHook(
if ("sessionId" in session) {
sessionId = session.sessionId;
} else if ("targetId" in session) {
const { targetId } = session;
sessionId = sessionIds.get(session.targetId);
if (!sessionId && throwIfNotAttached) {
throw new Error(`Target ${targetId} is not attached.`);
}
}
}
return sessionId;
}

function getSession(session: SessionIdentifier) {
if (attachments === undefined) {
function getSession(session: SessionIdentifier, throwIfNotAttached = true) {
const sessionId = getSessionId(session, throwIfNotAttached);
if (sessionId === undefined) {
return;
}
const sessionId = getSessionId(session);
if (sessionId !== undefined) {
return attachments.get(sessionId);

if (attachments !== undefined) {
const attachment = attachments.get(sessionId);
if (attachment !== undefined) {
return attachment;
}
}

if (throwIfNotAttached) {
throw new Error(`Session ${sessionId} is no longer attached.`);
}
}

Expand Down Expand Up @@ -128,14 +140,26 @@ export default function newEventHook(
function targetInfoChanged({
targetInfo,
}: Protocol.Target.TargetInfoChangedEvent) {
const attachment = getSession(targetInfo);
const attachment = getSession(targetInfo, false);
if (attachment !== undefined) {
attachment.targetInfo = targetInfo;
}
}

function getConnection(session: SessionIdentifier) {
const attachment = getSession(session);
function getConnection(session: SessionIdentifier): SessionConnection;
function getConnection(
session: SessionIdentifier,
throwIfNotAttached?: true,
): SessionConnection;
function getConnection(
session: SessionIdentifier,
throwIfNotAttached: boolean | undefined,
): SessionConnection | undefined;
function getConnection(
session: SessionIdentifier,
throwIfNotAttached = true,
) {
const attachment = getSession(session, throwIfNotAttached);
if (attachment === undefined) {
return;
}
Expand Down
87 changes: 68 additions & 19 deletions @tracerbench/protocol-connection/src/newProtocolConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import {
AttachProtocolTransport,
AttachSession,
} from "@tracerbench/protocol-transport";
import Protocol from "devtools-protocol";
import {
combineRaceCancellation,
disposablePromise,
RaceCancellation,
throwIfCancelled,
} from "race-cancellation";
import { NewEventEmitter, ProtocolConnection, SessionID } from "../types";
import {
NewEventEmitter,
ProtocolConnection,
RootConnection,
SessionConnection,
SessionID,
TargetID,
} from "../types";
import newEventHook, { Session } from "./newEventHook";

/**
Expand All @@ -21,22 +29,31 @@ import newEventHook, { Session } from "./newEventHook";
export default function newRootConnection(
attach: AttachProtocolTransport<SessionID>,
newEventEmitter: NewEventEmitter,
): ProtocolConnection {
): RootConnection {
return newProtocolConnection(attach, newEventEmitter);
}

function newSessionConnection(
attachSession: AttachSession<SessionID>,
newEventEmitter: NewEventEmitter,
session: Session,
) {
): SessionConnection {
return newProtocolConnection(
attachSession(session.sessionId),
newEventEmitter,
session,
);
}

function newProtocolConnection(
attachTransport: AttachProtocolTransport<SessionID>,
newEventEmitter: NewEventEmitter,
session: Session,
): SessionConnection;
function newProtocolConnection(
attachTransport: AttachProtocolTransport<SessionID>,
newEventEmitter: NewEventEmitter,
): RootConnection;
function newProtocolConnection(
attachTransport: AttachProtocolTransport<SessionID>,
newEventEmitter: NewEventEmitter,
Expand All @@ -58,35 +75,67 @@ function newProtocolConnection(
onTargetDetached,
);

return {
const base: RootConnection = {
attachToTarget,
connection,
off: emitter.removeListener.bind(emitter),
on: emitter.on.bind(emitter),
once: emitter.once.bind(emitter),
raceDetached,
removeAllListeners: emitter.removeAllListeners.bind(emitter),
removeListener: emitter.removeListener.bind(emitter),
send,
setAutoAttach,
until,
get isDetached() {
return isDetached;
},
get targetId() {
if (session !== undefined) {
return session.targetId;
}
},
get sessionId() {
if (session !== undefined) {
return session.sessionId;
}
},
get targetInfo() {
if (session !== undefined) {
return session.targetInfo;
}
},
};

if (session !== undefined) {
return Object.create(base, {
sessionId: {
get: () => session.sessionId,
},
targetId: {
get: () => session.targetId,
},
targetInfo: {
get: () => session.targetInfo,
},
});
}

return base;

async function attachToTarget(targetId: TargetID | { targetId: TargetID }) {
if (typeof targetId === "object" && targetId !== null) {
targetId = targetId.targetId;
}
const request = { flatten: true, targetId };
const conn = connection(request, false);
if (conn !== undefined) {
return conn;
}
const resp: Protocol.Target.AttachToTargetResponse = await send(
"Target.attachToTarget",
request,
);
return connection(resp);
}

async function setAutoAttach(
autoAttach: boolean,
waitForDebuggerOnStart = false,
) {
const request: Protocol.Target.SetAutoAttachRequest = {
autoAttach,
flatten: true,
waitForDebuggerOnStart,
};
await send("Target.setAutoAttach", request);
}

function onEvent(event: string, params: any) {
eventHook(event, params);
emitter.emit(event, params);
Expand Down
Loading

0 comments on commit a9d5573

Please sign in to comment.