From 2bdf8ea31e0c4d71ee59892c2f6a44ef87139ebe Mon Sep 17 00:00:00 2001 From: Milo Weinberg Date: Thu, 28 Nov 2024 00:58:32 -0500 Subject: [PATCH] Remove debouncedRef & rename variables in throttledRef to be consistent with name --- src/utils.ts | 46 ++++++---------------------------------------- 1 file changed, 6 insertions(+), 40 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 9d78b35..2778d81 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -125,51 +125,17 @@ export function useInterval(callback: () => void, timeout: number): void { onBeforeUnmount(() => clearTimeout(interval!)); } -/** - * @param source The value that should be debounced when changed - * @param delayMs The delay in milliseconds that should be waited when debouncing - * @returns The debounced value of 'source' - */ -export function debouncedRef( - source: Ref, - delayMs: number, - options?: WatchOptions | undefined, -): DeepReadonly> { - const debouncedValue = ref(source.value) as Ref; - let timeout: number | undefined; - - watch( - source, - () => { - if (timeout) clearTimeout(timeout); - - timeout = setTimeout(() => { - if (Array.isArray(source.value)) { - debouncedValue.value = [...source.value] as T; - } else if (typeof source.value === 'object') { - debouncedValue.value = { ...source.value } as T; - } else { - debouncedValue.value = source.value; - } - }, delayMs); - }, - options, - ); - - return readonly(debouncedValue); -} - /** * @param source The value that should be throttled when changed * @param delayMs The delay in milliseconds that should be waited when throttling - * @returns The debounced value of 'source' + * @returns The throttled value of 'source' */ export function throttledRef( source: Ref, delayMs: number, options?: WatchOptions | undefined, ): DeepReadonly> { - const debouncedValue = ref(source.value) as Ref; + const throttledValue = ref(source.value) as Ref; let timeout: number | undefined; let lastUpdate = performance.now(); @@ -182,11 +148,11 @@ export function throttledRef( timeout = setTimeout(() => { if (Array.isArray(source.value)) { - debouncedValue.value = [...source.value] as T; + throttledValue.value = [...source.value] as T; } else if (typeof source.value === 'object') { - debouncedValue.value = { ...source.value } as T; + throttledValue.value = { ...source.value } as T; } else { - debouncedValue.value = source.value; + throttledValue.value = source.value; } lastUpdate = performance.now(); @@ -195,7 +161,7 @@ export function throttledRef( options, ); - return readonly(debouncedValue); + return readonly(throttledValue); } export function asyncEvent(): { waiter: Promise; set: () => void } {