INIT
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
export * from "./throttled.svelte.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * from "./throttled.svelte.js";
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import type { Getter, MaybeGetter } from "../../internal/types.js";
|
||||
export declare class Throttled<T> {
|
||||
#private;
|
||||
constructor(getter: Getter<T>, wait?: MaybeGetter<number>);
|
||||
get current(): T;
|
||||
cancel(): void;
|
||||
setImmediately(v: T): void;
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
import { useThrottle } from "../use-throttle/use-throttle.svelte.js";
|
||||
import { watch } from "../watch/watch.svelte.js";
|
||||
import { noop } from "../../internal/utils/function.js";
|
||||
export class Throttled {
|
||||
#current = $state();
|
||||
#throttleFn;
|
||||
constructor(getter, wait = 250) {
|
||||
this.#current = getter(); // Immediately set the initial value
|
||||
this.#throttleFn = useThrottle(() => {
|
||||
this.#current = getter();
|
||||
}, wait);
|
||||
watch(getter, () => {
|
||||
this.#throttleFn()?.catch(noop);
|
||||
});
|
||||
}
|
||||
get current() {
|
||||
return this.#current;
|
||||
}
|
||||
cancel() {
|
||||
this.#throttleFn.cancel();
|
||||
}
|
||||
setImmediately(v) {
|
||||
this.cancel();
|
||||
this.#current = v;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user