From 42e941083a7f6292b430d953a3ed1e6b4b1da208 Mon Sep 17 00:00:00 2001 From: Ilia Mashkov Date: Fri, 16 Jan 2026 12:38:57 +0300 Subject: [PATCH] doc(createDeboucnedState): add JSDoc for createDebouncedState --- .../createDebouncedState.svelte.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/shared/lib/helpers/createDebouncedState/createDebouncedState.svelte.ts b/src/shared/lib/helpers/createDebouncedState/createDebouncedState.svelte.ts index 047f676..93840cc 100644 --- a/src/shared/lib/helpers/createDebouncedState/createDebouncedState.svelte.ts +++ b/src/shared/lib/helpers/createDebouncedState/createDebouncedState.svelte.ts @@ -1,5 +1,28 @@ import { debounce } from '$shared/lib/utils'; +/** + * Creates reactive state with immediate and debounced values. + * + * Useful for UI inputs that need instant feedback but debounced logic + * (e.g., search fields with API calls). The immediate value updates on + * every change for UI binding, while debounced updates after a delay. + * + * @param initialValue - Initial value for both immediate and debounced state + * @param wait - Delay in milliseconds before updating debounced value (default: 300) + * @returns Object with immediate/debounced getters, immediate setter, and reset method + * + * @example + * ```svelte + * + * + * + * + *

Typing: {search.immediate}

+ *

Searching: {search.debounced}

+ * ``` + */ export function createDebouncedState(initialValue: T, wait: number = 300) { let immediate = $state(initialValue); let debounced = $state(initialValue); @@ -9,6 +32,7 @@ export function createDebouncedState(initialValue: T, wait: number = 300) { }, wait); return { + /** Current value with immediate updates (for UI binding) */ get immediate() { return immediate; }, @@ -17,9 +41,14 @@ export function createDebouncedState(initialValue: T, wait: number = 300) { // Manually trigger the debounce on write updateDebounced(value); }, + /** Current value with debounced updates (for logic/operations) */ get debounced() { return debounced; }, + /** + * Resets both values to initial or specified value. + * @param value - Optional value to reset to (defaults to initialValue) + */ reset(value?: T) { const resetValue = value ?? initialValue; immediate = resetValue;