This commit is contained in:
+52
@@ -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
@@ -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
@@ -0,0 +1 @@
|
||||
export * from "./debounced.svelte.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * from "./debounced.svelte.js";
|
||||
Reference in New Issue
Block a user