INIT
CI / build (push) Has been skipped
CI / deploy (push) Successful in 1m11s

This commit is contained in:
eewing
2026-02-18 15:17:47 -06:00
parent e74c106f85
commit ec317eb17c
11532 changed files with 1631690 additions and 1 deletions
+52
View File
@@ -0,0 +1,52 @@
import type { Getter, MaybeGetter } from "../../internal/types.js";
/**
* A wrapper over {@link useDebounce} that creates a debounced state.
* It takes a "getter" function which returns the state you want to debounce.
* Every time this state changes a timer (re)starts, the length of which is
* configurable with the `wait` arg. When the timer ends the `current` value
* is updated.
*
* @see https://runed.dev/docs/utilities/debounced
*
* @example
*
* <script lang="ts">
* import { Debounced } from "runed";
*
* let search = $state("");
* const debounced = new Debounced(() => search, 500);
* </script>
*
* <div>
* <input bind:value={search} />
* <p>You searched for: <b>{debounced.current}</b></p>
* </div>
*/
export declare class Debounced<T> {
#private;
/**
* @param getter A function that returns the state to watch.
* @param wait The length of time to wait in ms, defaults to 250.
*/
constructor(getter: Getter<T>, wait?: MaybeGetter<number>);
/**
* Get the debounced value.
*/
get current(): T;
/**
* Whether a timer is currently pending.
*/
get pending(): boolean;
/**
* Cancel the latest timer.
*/
cancel(): void;
/**
* Run the debounced function immediately.
*/
updateImmediately(): Promise<void>;
/**
* Set the `current` value without waiting.
*/
setImmediately(v: T): void;
}
+78
View File
@@ -0,0 +1,78 @@
import { useDebounce } from "../use-debounce/use-debounce.svelte.js";
import { watch } from "../watch/watch.svelte.js";
import { noop } from "../../internal/utils/function.js";
/**
* A wrapper over {@link useDebounce} that creates a debounced state.
* It takes a "getter" function which returns the state you want to debounce.
* Every time this state changes a timer (re)starts, the length of which is
* configurable with the `wait` arg. When the timer ends the `current` value
* is updated.
*
* @see https://runed.dev/docs/utilities/debounced
*
* @example
*
* <script lang="ts">
* import { Debounced } from "runed";
*
* let search = $state("");
* const debounced = new Debounced(() => search, 500);
* </script>
*
* <div>
* <input bind:value={search} />
* <p>You searched for: <b>{debounced.current}</b></p>
* </div>
*/
export class Debounced {
#current = $state();
#debounceFn;
/**
* @param getter A function that returns the state to watch.
* @param wait The length of time to wait in ms, defaults to 250.
*/
constructor(getter, wait = 250) {
this.#current = getter(); // immediately set the initial value
this.cancel = this.cancel.bind(this);
this.setImmediately = this.setImmediately.bind(this);
this.updateImmediately = this.updateImmediately.bind(this);
this.#debounceFn = useDebounce(() => {
this.#current = getter();
}, wait);
watch(getter, () => {
this.#debounceFn().catch(noop);
});
}
// isn't the name "current" slightly misleading? it sounds like the latest, raw value
/**
* Get the debounced value.
*/
get current() {
return this.#current;
}
/**
* Whether a timer is currently pending.
*/
get pending() {
return this.#debounceFn.pending;
}
/**
* Cancel the latest timer.
*/
cancel() {
this.#debounceFn.cancel();
}
/**
* Run the debounced function immediately.
*/
updateImmediately() {
return this.#debounceFn.runScheduledNow();
}
/**
* Set the `current` value without waiting.
*/
setImmediately(v) {
this.cancel();
this.#current = v;
}
}
+1
View File
@@ -0,0 +1 @@
export * from "./debounced.svelte.js";
+1
View File
@@ -0,0 +1 @@
export * from "./debounced.svelte.js";