Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse the order of WebSocket events #5148

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/insomnia/src/main/network/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ const dispatchWebSocketEvent = (target: Electron.WebContents, eventChannel: stri
// Otherwise, append to send queue for this event channel.
const sendQueue = sendQueueMap.get(eventChannel);
if (sendQueue) {
sendQueue.push(wsEvent);
// Add the event to the top of queue so that the latest message is first.
sendQueue.unshift(wsEvent);
} else {
sendQueueMap.set(eventChannel, [wsEvent]);
}
Expand Down Expand Up @@ -433,7 +434,10 @@ const findMany = async (
}
const body = await fs.promises.readFile(response.bodyPath);
return body.toString().split('\n').filter(e => e?.trim())
.map(e => JSON.parse(e)) || [];
// Parse the message
.map(e => JSON.parse(e))
// Reverse the list of messages so that we get the latest message first
.reverse() || [];
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ export function useWebSocketConnectionEvents({ responseId }: { responseId: strin
if (isMounted) {
setEvents(allEvents);
}

const afterLatestEvent = (event: WebSocketEvent, prevEvents: WebSocketEvent[]) => {
if (prevEvents.length === 0) {
return true;
}

return event.timestamp > prevEvents[0]?.timestamp;
};

// Subscribe to new events and update the state.
unsubscribe = window.main.on(`webSocket.${responseId}.event`,
(_, events: WebSocketEvent[]) => {
console.log('received events', events);
if (isMounted) {
setEvents(allEvents => allEvents.concat(events));
setEvents(prevEvents => events.filter(event => afterLatestEvent(event, prevEvents)).concat(prevEvents));
}

// Wait to give the CTS signal until we've rendered a frame.
Expand Down