Skip to content

Commit

Permalink
Update event types to improve inference
Browse files Browse the repository at this point in the history
  • Loading branch information
gatzjames committed Aug 6, 2022
1 parent 85a2022 commit ec057a9
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/insomnia/src/main/network/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ export interface WebSocketConnection extends WebSocket {
export type WebsocketOpenEvent = Omit<OpenEvent, 'target'> & {
_id: string;
requestId: string;
type: 'open';
};

export type WebsocketMessageEvent = Omit<MessageEvent, 'target'> & {
_id: string;
requestId: string;
direction: 'OUTGOING' | 'INCOMING';
type: 'message';
};

export type WebsocketErrorEvent = Omit<ErrorEvent, 'target'> & {
_id: string;
requestId: string;
type: 'error';
};

export type WebsocketCloseEvent = Omit<CloseEvent, 'target'> & {
_id: string;
requestId: string;
type: 'close';
};

export type WebsocketEvent =
Expand Down Expand Up @@ -74,11 +78,11 @@ async function createWebSocketConnection(
event.sender.send(readyStateChannel, ws.readyState);
WebSocketConnections.set(options.requestId, ws);

ws.addEventListener('open', ({ type }) => {
ws.addEventListener('open', () => {
const openEvent: WebsocketOpenEvent = {
_id: uuidV4(),
requestId: options.requestId,
type,
type: 'open',
};

WebSocketEventLogs.set(options.requestId, [openEvent]);
Expand All @@ -87,28 +91,28 @@ async function createWebSocketConnection(
event.sender.send(readyStateChannel, ws.readyState);
});

ws.addEventListener('message', ({ data, type }) => {
ws.addEventListener('message', ({ data }) => {
const msgs = WebSocketEventLogs.get(options.requestId) || [];
const messageEvent: WebsocketMessageEvent = {
_id: uuidV4(),
requestId: options.requestId,
data,
type,
type: 'message',
direction: 'INCOMING',
};

WebSocketEventLogs.set(options.requestId, [...msgs, messageEvent]);
event.sender.send(eventChannel, messageEvent);
});

ws.addEventListener('close', ({ code, reason, type, wasClean }) => {
ws.addEventListener('close', ({ code, reason, wasClean }) => {
const msgs = WebSocketEventLogs.get(options.requestId) || [];
const closeEvent: WebsocketCloseEvent = {
_id: uuidV4(),
requestId: options.requestId,
code,
reason,
type,
type: 'close',
wasClean,
};

Expand Down

0 comments on commit ec057a9

Please sign in to comment.