import { ref, readonly, watch, type Ref } from 'vue' /** * Like VueUse's refDebounced but with a flush() method to force immediate sync. * Useful when resetting filters - no need to wait for debounce delay. */ export function useDebouncedRef(source: Ref, delay: number) { const debounced = ref(source.value) as Ref const debouncedReadonly = readonly(debounced) let timeoutId: ReturnType | null = null watch(source, () => { if (timeoutId) { clearTimeout(timeoutId) } timeoutId = setTimeout(() => { debounced.value = source.value timeoutId = null }, delay) }) function flush() { if (timeoutId) { clearTimeout(timeoutId) timeoutId = null } debounced.value = source.value } return { debounced: debouncedReadonly, flush } }