npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

keytask-core

v1.0.0

Published

A tiny key-based task controller for timeout and RAF work.

Readme

logo

Table of contents

About

keytask is a tiny TypeScript library for key-based control of timing-sensitive work.

It is designed for render-heavy UI, scroll libraries, animation systems and games: places where you need to schedule, replace, lock, loop and cancel work without scattering raw setTimeout, requestAnimationFrame and cleanup state across the codebase.

It is not an animation engine. It does not provide easing, tweens, timelines, physics or rendering. It only controls when work is allowed to run.

The core idea is simple - the same task key always describes the same unit of work.

Installation

npm install keytask-core
import { setTask } from "keytask-core";

✦ Note:

  • Supports both ESM (import) and CommonJS (require) builds.
  • Supports one shared global manager and isolated local managers.
  • Written in TypeScript and ships declaration files.
  • No runtime dependencies.
  • RAF mode uses requestAnimationFrame / cancelAnimationFrame when available and falls back to setTimeout(..., 16) with a console warning when those APIs are missing.

API

— GLOBAL —

Usage:

setTask(callback, timer, key?);
  • callback - work to run once.
  • timer - timeout in milliseconds or "raf".
  • key - optional string key, reusing the same key replaces the previous task state.

Description: Schedules one task and returns its key. This is useful for "latest wins" work: scroll-end handlers, delayed cleanup, render batching, or any task where only the newest pending callback should survive. ✦ A single key can only reference one active task within the same manager instance.

Return: Returns the passed key, or an automatically generated key when key is omitted.

const key = setTask(callback, "raf");

cancelTask(key);

Example:

setTask(
  () => {
    doSomething();
  },
  "raf",
  "my-key",
);

Usage:

setLockTask(callback, timer, key?);
  • callback - work to run immediately.
  • timer - lock duration in milliseconds or "raf" for a frame lock.
  • key - optional string key, calls are ignored while the key already belongs to any task state.

Description: Runs the callback immediately, then blocks repeated calls with the same key until the timeout ends or the next RAF unlocks it. This is useful for "first wins" work: frame locks, cooldowns, input guards, or preventing repeated calls from doing too much work in the same frame/window. ✦ A single key can only reference one active task within the same manager instance.

Return: Returns the passed key, or an automatically generated key when key is omitted.

const key = setLockTask(callback, "raf");

cancelTask(key);

Example:

setLockTask(
  () => {
    doSomething();
  },
  "raf",
  "my-key",
);

Usage:

setThrottleTask(callback, timer, key?);
  • callback - work to run immediately, or save as the latest trailing call while throttled.
  • timer - throttle window in milliseconds or "raf" for a frame throttle.
  • key - optional string key, repeated calls with the same key keep only the latest trailing callback.

Description: Runs the first callback immediately. While the key is throttled, repeated calls do not run immediately. Instead, only the latest callback is saved and runs when the throttle window opens again. This is useful for "first now, latest later" work: pointer updates, resize updates, scroll sync, render requests or other cases where dropping the final update would be incorrect. ✦ A single key can only reference one active task within the same manager instance.

Return: Returns the passed key, or an automatically generated key when key is omitted.

const key = setThrottleTask(callback, "raf");

cancelTask(key);

Example:

setThrottleTask(
  () => {
    doSomething();
  },
  "raf",
  "my-key",
);

Usage:

setLoopTask(callback, timer, key?);
  • callback - repeated work. Receives tick, time, and delta.
  • timer - interval in milliseconds or "raf" for an animation-frame loop.
  • key - optional string key, reusing the same key replaces the previous task state.

Description: Starts repeated work and returns its key. The callback receives a 1-based tick count, current time and delta since the previous tick. Return false to stop the loop. ✦ A single key can only reference one active task within the same manager instance.

Return: Returns the passed key, or an automatically generated key when key is omitted.

const key = setLoopTask(callback, "raf");

cancelTask(key);

Example:

setLoopTask(
  (tick, time, delta) => {
    doSomething();

    if (tick > 10) return false; // exit
  },
  "raf",
  "my-key",
);

Usage:

cancelTask("key");
cancelTask(["key1", "key2"]);
cancelTask(); // clear all tasks

Description: Cancels one, several, or all tasks in the global manager.

Usage:

hasTask("key");

Description: Check pending tasks, active locks, active throttles or active loops. Returns true when the key belongs to any active task state in the global manager.

Example:

if (hasTask("render")) {
  cancelTask("render");
}
— LOCAL —

Usage:

const tasks = createTaskManager();

The returned manager has the same task methods as the global API, plus clear().

Description: Creates a local manager with its own one-shot tasks, locks, throttles and loops. Use it for components, scenes, entities or modules that need isolated cleanup. Calling clear() cancels everything inside that local manager without touching the global manager.

Example:

const sceneTasks = createTaskManager();

sceneTasks.setLoopTask(callback, "raf", "my-raf-key");
sceneTasks.setTask(callback, 1000, "my-timeout-key");

function destroyScene() {
  sceneTasks.clear();
}

Common patterns

setTask(
  () => {
    render();
  },
  "raf",
  "render",
);
setTask(
  () => {
    saveDraft();
  },
  300,
  "save-draft",
);
setLockTask(
  () => {
    submitInput();
  },
  150,
  "input",
);
setThrottleTask(
  () => {
    syncPointerState();
  },
  "raf",
  "pointer",
);
setLoopTask(
  (tick, time, delta) => {
    update(delta);

    if (tick > 10) return false;
  },
  "raf",
  "loop",
);
const tasks = createTaskManager();

tasks.setLoopTask(updateScene, "raf", "loop");
tasks.setTask(removeHint, 1000, "hint");

function destroy() {
  tasks.clear();
}

License