Skip to content

Commit

Permalink
chore: fix reset at end
Browse files Browse the repository at this point in the history
  • Loading branch information
cjkoepke committed Dec 17, 2024
1 parent 91d9439 commit ddf5761
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
8 changes: 4 additions & 4 deletions ui/src/app/api/messages/batch/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ export async function GET(req: Request, res: Response) {
}, 100);

rl.on("line", (line: string) => {
try {
eventBuffer.push(line);
} catch (error) {
controller.error(error);
if (!line) {
return;
}

eventBuffer.push(line);
});

rl.on("close", () => {
Expand Down
10 changes: 1 addition & 9 deletions ui/src/components/Graph/hooks/useStreamMessagesHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useMemo, useRef, useState } from "react";

import { defaultAggregatedData, useGraphContext } from "@/contexts/GraphContext/context";
import { useGraphContext } from "@/contexts/GraphContext/context";
import { ISimulationAggregatedDataState } from "@/contexts/GraphContext/types";

export const useStreamMessagesHandler = () => {
Expand Down Expand Up @@ -42,14 +42,6 @@ export const useStreamMessagesHandler = () => {
const stopStream = useCallback(() => {
eventSource.current?.close();
setStreaming(false);

dispatch({
type: "BATCH_UPDATE",
payload: {
currentNode: undefined,
aggregatedData: defaultAggregatedData,
},
});
}, []);

return useMemo(
Expand Down
13 changes: 12 additions & 1 deletion ui/src/components/Graph/modules/Controls.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { FC, memo } from "react";

import { defaultAggregatedData, useGraphContext } from "@/contexts/GraphContext/context";
import { useHandlers } from "../hooks/useHandlers";
import { useStreamMessagesHandler } from "../hooks/useStreamMessagesHandler";

export const Controls: FC = memo(() => {
const { dispatch } = useGraphContext();
const { handleResetSim } = useHandlers();
const { startStream, streaming, stopStream } = useStreamMessagesHandler();

Expand All @@ -18,7 +20,16 @@ export const Controls: FC = memo(() => {
</button>
<button
className="bg-gray-400 text-white w-[80px] rounded-md px-4 py-2"
onClick={streaming ? stopStream : handleResetSim}
onClick={streaming ? () => {
stopStream();
dispatch({
type: "BATCH_UPDATE",
payload: {
currentNode: undefined,
aggregatedData: defaultAggregatedData,
},
});
} : handleResetSim}
>
{streaming ? "Cancel" : "Reset"}
</button>
Expand Down

0 comments on commit ddf5761

Please sign in to comment.