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

* fix: building vertices not working on Safari

* fix: building vertices not working on Safari

* fix: stop build button seems to not work

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Nicolò Boschi 2024-08-27 14:19:26 +02:00 • committed by GitHub
commit 8e9ba9cca1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 52 additions and 28 deletions

View file

@ -315,9 +315,6 @@ export const MenuBar = ({}: {}): JSX.Element => {
disabled={!isBuilding} disabled={!isBuilding}
onClick={(_) => { onClick={(_) => {
if (isBuilding) { if (isBuilding) {
setIsBuilding(false);
revertBuiltStatusFromBuilding();
setLockChat(false);
window.stop(); window.stop();
} }
}} }}

View file

@ -208,6 +208,7 @@ export type StreamingRequestParams = {
onData: (event: object) => Promise<boolean>; onData: (event: object) => Promise<boolean>;
body?: object; body?: object;
onError?: (statusCode: number) => void; onError?: (statusCode: number) => void;
onNetworkError?: (error: Error) => void;
}; };
async function performStreamingRequest({ async function performStreamingRequest({
@ -216,6 +217,7 @@ async function performStreamingRequest({
onData, onData,
body, body,
onError, onError,
onNetworkError,
}: StreamingRequestParams) { }: StreamingRequestParams) {
let headers = { let headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -248,8 +250,14 @@ async function performStreamingRequest({
if (response.body === null) { if (response.body === null) {
return; return;
} }
for await (const chunk of response.body) { try {
const decodedChunk = await textDecoder.decode(chunk); 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"); let all = decodedChunk.split("\n\n");
for (const string of all) { for (const string of all) {
if (string.endsWith("}")) { if (string.endsWith("}")) {
@ -279,6 +287,13 @@ async function performStreamingRequest({
await onData(data); await onData(data);
} }
} }
} catch (e: any) {
if (onNetworkError) {
onNetworkError(e);
} else {
throw e;
}
}
} }
export { ApiInterceptor, api, performStreamingRequest }; export { ApiInterceptor, api, performStreamingRequest };

View file

@ -642,6 +642,14 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
get().setLockChat(false); get().setLockChat(false);
}, },
onBuildUpdate: handleBuildUpdate, onBuildUpdate: handleBuildUpdate,
onBuildStopped: () => {
get().setIsBuilding(false);
setErrorData({
title: "Build stopped",
});
get().revertBuiltStatusFromBuilding();
get().setLockChat(false);
},
onBuildError: (title: string, list: string[], elementList) => { onBuildError: (title: string, list: string[], elementList) => {
const idList = elementList const idList = elementList
.map((element) => element.id) .map((element) => element.id)

View file

@ -26,6 +26,7 @@ type BuildVerticesParams = {
) => void; // Replace any with the actual type if it's not any ) => void; // Replace any with the actual type if it's not any
onBuildComplete?: (allNodesValid: boolean) => void; onBuildComplete?: (allNodesValid: boolean) => void;
onBuildError?: (title, list, idList: VertexLayerElementType[]) => void; onBuildError?: (title, list, idList: VertexLayerElementType[]) => void;
onBuildStopped?: () => void;
onBuildStart?: (idList: VertexLayerElementType[]) => void; onBuildStart?: (idList: VertexLayerElementType[]) => void;
onValidateNodes?: (nodes: string[]) => void; onValidateNodes?: (nodes: string[]) => void;
nodes?: Node[]; nodes?: Node[];
@ -143,6 +144,7 @@ export async function buildFlowVertices({
onBuildUpdate, onBuildUpdate,
onBuildComplete, onBuildComplete,
onBuildError, onBuildError,
onBuildStopped,
onBuildStart, onBuildStart,
onValidateNodes, onValidateNodes,
nodes, nodes,
@ -297,6 +299,8 @@ export async function buildFlowVertices({
} }
throw new Error("error in streaming request"); throw new Error("error in streaming request");
}, },
// network error are likely caused by the window.stop() called in the stopBuild function
onNetworkError: onBuildStopped,
}); });
} }