Skip to content

Commit

Permalink
feat: support history in websocket mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazawazi committed Nov 28, 2023
1 parent 3ecffd9 commit a003521
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions frontend/src/components/FunixFunction/InputPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const InputPanel = (props: {
const { setInput, setOutput } = useFunixHistory();
const { enqueueSnackbar } = useSnackbar();

const [tempOutput, setTempOutput] = useState<string | null>(null);

useEffect(() => {
setWaiting(() => !requestDone);
}, [asyncWaiting]);
Expand Down Expand Up @@ -72,6 +74,26 @@ const InputPanel = (props: {
setForm(formData);
};

const saveOutput = async (
now: number,
name: string,
path: string,
output: string
) => {
try {
await setOutput(now, name, path, output);
} catch (e) {
enqueueSnackbar(
"Cannot save output to history, check your console for more information",
{
variant: "error",
}
);
console.error("Funix History Error:");
console.error(e);
}
};

const widgets = {
TextWidget: TextExtendedWidget,
CheckboxWidget: SwitchWidget,
Expand Down Expand Up @@ -107,7 +129,7 @@ const InputPanel = (props: {
: form
: form;

if (saveHistory && !props.preview.websocket) {
if (saveHistory) {
try {
await setInput(now, props.preview.name, props.preview.path, newForm);
} catch (error) {
Expand Down Expand Up @@ -136,42 +158,34 @@ const InputPanel = (props: {

socket.addEventListener("message", function (event) {
props.setResponse(() => event.data);
setTempOutput(() => event.data);
});

socket.addEventListener("close", async function () {
enqueueSnackbar("In websocket mode, history will disable", {
variant: "warning",
});
setWaiting(() => false);
setRequestDone(() => true);
if (saveHistory && tempOutput) {
await saveOutput(
now,
props.preview.name,
props.preview.path,
tempOutput
);
}
});
} else {
const response = await callFunctionRaw(
new URL(`/call/${props.detail.id}`, props.backend),
newForm
);
if (saveHistory) {
try {
await setOutput(
now,
props.preview.name,
props.preview.path,
response
);
} catch (error) {
enqueueSnackbar(
"Cannot save output to history, check your console for more information",
{
variant: "error",
}
);
console.error("Funix History Error:");
console.error(error);
}
}
props.setResponse(() => response.toString());
const result = response.toString();
props.setResponse(() => result);
setWaiting(() => false);
setRequestDone(() => true);

if (saveHistory) {
await saveOutput(now, props.preview.name, props.preview.path, result);
}
}
};

Expand Down

0 comments on commit a003521

Please sign in to comment.