Refactor code + add auto-retry

This commit is contained in:
2023-07-17 21:29:40 +05:30
parent 5fa82341c2
commit 718fe03d80
8 changed files with 458 additions and 305 deletions
+27
View File
@@ -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;
};
}