Skip to content

Commit

Permalink
fix first and subsequent message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marhaupe committed Oct 12, 2024
1 parent db81e66 commit 791d30e
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ export function App() {
};

eventSource.onmessage = (event: MessageEvent) => {
try {
const incomingLogs = JSON.parse(event.data) as Log[];
if (incomingLogs.length !== 1) {
logListRef.current?.resetVirtualization();
setLogs(incomingLogs);
} else {
setLogs((prevLogs) => [...prevLogs, ...incomingLogs]);
}
} catch (error) {
console.error("Error parsing log", error);
}
const incomingLogs = safeParseLogs(event.data);
logListRef.current?.resetVirtualization();
setLogs(incomingLogs);
eventSource.onmessage = (event: MessageEvent) => {
const incomingLogs = safeParseLogs(event.data);
setLogs((prevLogs) => [...prevLogs, ...incomingLogs]);
};
};

return () => {
Expand Down Expand Up @@ -151,3 +147,12 @@ export function App() {
</div>
);
}

function safeParseLogs(data: string) {
try {
return JSON.parse(data) as Log[];
} catch (error) {
console.error("Error parsing log", error);
return [];
}
}

0 comments on commit 791d30e

Please sign in to comment.