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

fix: building vertices not working on Safari and improve stop button #3569

Merged
merged 4 commits into from
Aug 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,6 @@ export const MenuBar = ({}: {}): JSX.Element => {
disabled={!isBuilding}
onClick={(_) => {
if (isBuilding) {
setIsBuilding(false);
revertBuiltStatusFromBuilding();
setLockChat(false);
window.stop();
}
}}
Expand Down
65 changes: 40 additions & 25 deletions src/frontend/src/controllers/API/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export type StreamingRequestParams = {
onData: (event: object) => Promise<boolean>;
body?: object;
onError?: (statusCode: number) => void;
onNetworkError?: (error: Error) => void;
};

async function performStreamingRequest({
Expand All @@ -216,6 +217,7 @@ async function performStreamingRequest({
onData,
body,
onError,
onNetworkError,
}: StreamingRequestParams) {
let headers = {
"Content-Type": "application/json",
Expand Down Expand Up @@ -248,35 +250,48 @@ async function performStreamingRequest({
if (response.body === null) {
return;
}
for await (const chunk of response.body) {
const decodedChunk = await textDecoder.decode(chunk);
let all = decodedChunk.split("\n\n");
for (const string of all) {
if (string.endsWith("}")) {
const allString = current.join("") + string;
let data: object;
try {
data = JSON.parse(allString);
current = [];
} catch (e) {
try {
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const decodedChunk = textDecoder.decode(value);
let all = decodedChunk.split("\n\n");
for (const string of all) {
if (string.endsWith("}")) {
const allString = current.join("") + string;
let data: object;
try {
data = JSON.parse(allString);
current = [];
} catch (e) {
current.push(string);
continue;
}
const shouldContinue = await onData(data);
if (!shouldContinue) {
controller.abort();
return;
}
} else {
current.push(string);
continue;
}
const shouldContinue = await onData(data);
if (!shouldContinue) {
controller.abort();
return;
}
} else {
current.push(string);
}
}
}
if (current.length > 0) {
const allString = current.join("");
if (allString) {
const data = JSON.parse(current.join(""));
await onData(data);
if (current.length > 0) {
const allString = current.join("");
if (allString) {
const data = JSON.parse(current.join(""));
await onData(data);
}
}
} catch (e: any) {
if (onNetworkError) {
onNetworkError(e);
} else {
throw e;
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/frontend/src/stores/flowStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,14 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
get().setLockChat(false);
},
onBuildUpdate: handleBuildUpdate,
onBuildStopped: () => {
get().setIsBuilding(false);
setErrorData({
title: "Build stopped",
});
get().revertBuiltStatusFromBuilding();
get().setLockChat(false);
},
onBuildError: (title: string, list: string[], elementList) => {
const idList = elementList
.map((element) => element.id)
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/src/utils/buildUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type BuildVerticesParams = {
) => void; // Replace any with the actual type if it's not any
onBuildComplete?: (allNodesValid: boolean) => void;
onBuildError?: (title, list, idList: VertexLayerElementType[]) => void;
onBuildStopped?: () => void;
onBuildStart?: (idList: VertexLayerElementType[]) => void;
onValidateNodes?: (nodes: string[]) => void;
nodes?: Node[];
Expand Down Expand Up @@ -143,6 +144,7 @@ export async function buildFlowVertices({
onBuildUpdate,
onBuildComplete,
onBuildError,
onBuildStopped,
onBuildStart,
onValidateNodes,
nodes,
Expand Down Expand Up @@ -297,6 +299,8 @@ export async function buildFlowVertices({
}
throw new Error("error in streaming request");
},
// network error are likely caused by the window.stop() called in the stopBuild function
onNetworkError: onBuildStopped,
});
}

Expand Down
Loading