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( function changeCallback(
value: SerializedObject, value: SerializedObject,
callback: (ack: unknown) => void = () => {}, callback: (ack: undefined | SerializedObject) => void = () => {},
) { ) {
updateValue(value, callback); updateValue(value, callback);
} }

View File

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

View File

@@ -19,7 +19,10 @@ interface SliderComponentProps {
stepSize: NumberObject; stepSize: NumberObject;
isInstantUpdate: boolean; isInstantUpdate: boolean;
addNotification: (message: string, levelname?: LevelName) => void; 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; displayName: string;
id: string; id: string;
} }

View File

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