Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sad-cars-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"runed": patch
---

fix: add jsdoc comments to use-throttle
Original file line number Diff line number Diff line change
@@ -1,14 +1,71 @@
import { untrack } from "svelte";
import type { MaybeGetter } from "$lib/internal/types.js";

/**
* Represents the throttled wrapper returned by {@link useThrottle}.
*
* The wrapper is an async function that returns a {@link Promise} resolving to
* the original callback's return value. In addition to being callable, it
* provides two helper properties `cancel()` and `pending`.
*/
type UseThrottleReturn<Args extends unknown[], Return> = ((
this: unknown,
...args: Args
) => Promise<Return>) & {
/**
* Aborts any pending throttled execution and rejects the awaiting promise
* with the string `"Cancelled"`.
*/
cancel: () => void;

/**
* Whether a throttled call is currently waiting to be executed.
*/
pending: boolean;
};

/**
* Creates a throttled wrapper around a callback function.
*
* The returned function ensures that the original callback is invoked at most
* once per specified interval. Calls made during the wait period are queued
* and resolved with the result of the most recent invocation.
*
* @see https://runed.dev/docs/utilities/use-throttle
*
* @param callback - The function to be throttled.
* @param interval - The time interval in milliseconds, or a {@link MaybeGetter}
* returning the interval. If omitted, the default is `250`.
*
* @returns A throttled version of the callback: {@link UseThrottleReturn}.
*
* @example
* ```svelte
* <script lang="ts">
* import { useThrottle } from 'runed';
*
* let search = $state('');
* let throttledSearch = $state('');
*
* // Create a throttled fn – it runs at most once per 250ms
* const throttledUpdate = useThrottle(() => {
* throttledSearch = search;
* });
* </script>
*
* <input
* bind:value={{
* () => search,
* (v) => {
* search = v;
* throttledUpdate();
* }
* }}
* />
*
* <p>You searched for: <b>{throttledSearch}</b></p>
* ```
*/
export function useThrottle<Args extends unknown[], Return>(
callback: (...args: Args) => Return,
interval: MaybeGetter<number> = 250
Expand Down