Add slider debounce (#1479)

Sliders for exposure and brightness would spam messages on the backend.
This used to cause crashes and can cause it to get quite laggy /
delayed. This will add a 20ms debounce which won't send the value to the
backend until the value hasn't changed for 20ms.

---------

Co-authored-by: Matt <matthew.morley.ca@gmail.com>
This commit is contained in:
Cameron (3539)
2024-10-21 00:48:01 -04:00
committed by GitHub
parent 4c84c87cf4
commit 7224561b76

View File

@@ -25,9 +25,22 @@ const emit = defineEmits<{
(e: "input", value: number): void;
}>();
// Debounce function
function debounce(func: (...args: any[]) => void, wait: number) {
let timeout: ReturnType<typeof setTimeout>;
return function (...args: any[]) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
const debouncedEmit = debounce((v: number) => {
emit("input", v);
}, 20);
const localValue = computed({
get: () => props.value,
set: (v) => emit("input", parseFloat(v as unknown as string))
set: (v) => debouncedEmit(parseFloat(v as unknown as string))
});
</script>