Skip to content

Commit

Permalink
Remove debouncedRef & rename variables in throttledRef to be consiste…
Browse files Browse the repository at this point in the history
…nt with name
  • Loading branch information
Iapetus-11 committed Nov 28, 2024
1 parent a54e0ab commit 2bdf8ea
Showing 1 changed file with 6 additions and 40 deletions.
46 changes: 6 additions & 40 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
source: Ref<T>,
delayMs: number,
options?: WatchOptions<false> | undefined,
): DeepReadonly<Ref<T>> {
const debouncedValue = ref(source.value) as Ref<T>;
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<T>(
source: Ref<T>,
delayMs: number,
options?: WatchOptions<false> | undefined,
): DeepReadonly<Ref<T>> {
const debouncedValue = ref(source.value) as Ref<T>;
const throttledValue = ref(source.value) as Ref<T>;
let timeout: number | undefined;
let lastUpdate = performance.now();

Expand All @@ -182,11 +148,11 @@ export function throttledRef<T>(

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();
Expand All @@ -195,7 +161,7 @@ export function throttledRef<T>(
options,
);

return readonly(debouncedValue);
return readonly(throttledValue);
}

export function asyncEvent(): { waiter: Promise<void>; set: () => void } {
Expand Down

0 comments on commit 2bdf8ea

Please sign in to comment.