mirror of
https://github.com/bendtherules/multimeter-connect-web.git
synced 2026-08-18 13:52:26 +00:00
Refactor code + add auto-retry
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
export function debounceReadingFunc(
|
||||
callbackFn: (input: string | number) => void,
|
||||
debounceMillis = 1000,
|
||||
) {
|
||||
let timerId: ReturnType<typeof setTimeout> | undefined = undefined;
|
||||
let lastReturnedValue: number | string | undefined = undefined;
|
||||
let lastNewValue: number | string | undefined = undefined;
|
||||
|
||||
return function debouncedReading(newValue: string | number | undefined) {
|
||||
if (newValue !== undefined && newValue !== lastNewValue) {
|
||||
if (timerId !== undefined) {
|
||||
clearTimeout(timerId);
|
||||
timerId = undefined;
|
||||
}
|
||||
|
||||
if (newValue !== lastReturnedValue) {
|
||||
timerId = setTimeout(() => {
|
||||
lastReturnedValue = newValue;
|
||||
timerId = undefined;
|
||||
|
||||
callbackFn(newValue);
|
||||
}, debounceMillis);
|
||||
}
|
||||
}
|
||||
lastNewValue = newValue;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user