feat: reset frontend value to last value on exception

When changing a value in the frontend and this operation triggers an
exception in the backend, reset the frontend value to the last known
value.
This commit is contained in:
Mose Müller
2025-07-03 15:38:31 +02:00
parent 661603ef71
commit d24893a989
4 changed files with 22 additions and 7 deletions

View File

@@ -50,7 +50,7 @@ const createDisplayNameFromAccessPath = (fullAccessPath: string): string => {
function changeCallback(
value: SerializedObject,
callback: (ack: unknown) => void = () => {},
callback: (ack: undefined | SerializedObject) => void = () => {},
) {
updateValue(value, callback);
}

View File

@@ -38,7 +38,10 @@ interface NumberComponentProps {
isInstantUpdate: boolean;
unit?: string;
addNotification: (message: string, levelname?: LevelName) => void;
changeCallback?: (value: SerializedObject, callback?: (ack: unknown) => void) => void;
changeCallback?: (
value: SerializedObject,
callback?: (ack: undefined | SerializedObject) => void,
) => void;
displayName?: string;
id: string;
}
@@ -217,6 +220,15 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
id,
} = props;
const handleChange = (newValue: SerializedObject) => {
changeCallback(newValue, (result: undefined | SerializedObject) => {
if (result === undefined) return;
if (result.type == "Exception") {
setInputString(value.toString());
}
});
};
// Create a state for the cursor position
const cursorPositionRef = useRef<number | null>(null);
@@ -319,7 +331,7 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
};
}
changeCallback(serializedObject);
handleChange(serializedObject);
return;
} else {
console.debug(key);
@@ -350,7 +362,7 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
};
}
changeCallback(serializedObject);
handleChange(serializedObject);
}
setInputString(newValue);
@@ -384,7 +396,7 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
};
}
changeCallback(serializedObject);
handleChange(serializedObject);
}
};
useEffect(() => {

View File

@@ -19,7 +19,10 @@ interface SliderComponentProps {
stepSize: NumberObject;
isInstantUpdate: boolean;
addNotification: (message: string, levelname?: LevelName) => void;
changeCallback?: (value: SerializedObject, callback?: (ack: unknown) => void) => void;
changeCallback?: (
value: SerializedObject,
callback?: (ack: undefined | SerializedObject) => void,
) => void;
displayName: string;
id: string;
}

View File

@@ -28,7 +28,7 @@ export const socket = io(URL, {
export const updateValue = (
serializedObject: SerializedObject,
callback?: (ack: unknown) => void,
callback?: (ack: undefined | SerializedObject) => void,
) => {
if (callback) {
socket.emit(