martinstark / throttle-ts

Correctly typed, generic, typescript throttle function

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

throttle-ts

Correctly typed, generic, tiny (431B), typescript throttle function.

throttle typescript

Yields the return value of the throttled function, or undefined when throttled/cancelled.

The throttled function keeps the type signature of the original function, plus void.

Returns a cancel function which enables cleanup of the timeout, and blocks future calls to the throttled function. Useful when unmounting react/view components.

Usage

import { throttle } from "@martinstark/throttle-ts";
const fn = () => "executed";

const [throttledFn] = throttle(fn, 200);

throttledFn(); // "executed"
throttledFn(); // undefined
throttledFn(); // undefined
setTimeout(throttledFn, 500); // "executed"

Using Cancel

const fn = () => "executed";

const [throttledFn, cancel] = throttle(fn, 200);

throttledFn(); // "executed"
setTimeout(throttledFn, 500); // undefined
cancel();

About

Correctly typed, generic, typescript throttle function

License:MIT License


Languages

Language:TypeScript 45.3%Language:JavaScript 43.7%Language:Shell 11.0%