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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solid-primitives/scheduled

v1.4.4

Published

Primitives for creating scheduled — throttled or debounced — callbacks.

Downloads

115,287

Readme

@solid-primitives/scheduled

turborepo size version stage

Primitives for creating scheduled — throttled or debounced — callbacks.

  • debounce - Creates a callback that is debounced and cancellable.
  • throttle - Creates a callback that is throttled and cancellable.
  • scheduleIdle - Creates a callback throttled using window.requestIdleCallback().
  • leading - Creates a scheduled and cancellable callback that will be called on leading edge.
  • createScheduled - Creates a signal used for scheduling execution of solid computations by tracking.
  • Scheduling explanation

Installation

npm install @solid-primitives/scheduled
# or
yarn add @solid-primitives/scheduled

debounce

Creates a callback that is debounced and cancellable. The debounced callback is called on trailing edge.

The timeout will be automatically cleared on root dispose.

How to use it

import { debounce } from "@solid-primitives/scheduled";

const trigger = debounce((message: string) => console.log(message), 250);
trigger("Hello!");
trigger.clear(); // clears a timeout in progress

throttle

Creates a callback that is throttled and cancellable. The throttled callback is called on trailing edge.

The timeout will be automatically cleared on root dispose.

How to use it

import { throttle } from "@solid-primitives/scheduled";

const trigger = throttle((message: string) => console.log(message), 250);
trigger("Hello!");
trigger.clear(); // clears a timeout in progress

scheduleIdle

Creates a callback throttled using window.requestIdleCallback(). (MDN reference)

The throttled callback is called on trailing edge.

The timeout will be automatically cleared on root dispose.

Note: requestIdleCallback is not available in Safari. If it's not available, scheduleIdle will fallback to throttle with default timeout. (callbacks will be batched using setTimeout instead)

How to use it

import { scheduleIdle } from "@solid-primitives/scheduled";

const trigger = scheduleIdle(
  (message: string) => console.log(message),
  // timeout passed to requestIdleCallback is a maximum timeout before the callback is called
  250,
);
trigger("Hello!");
trigger.clear(); // clears a timeout in progress

leading

Creates a scheduled and cancellable callback that will be called on leading edge.

The timeout will be automatically cleared on root dispose.

How to use it

// with debounce
import { leading, debounce } from "@solid-primitives/scheduled";

const trigger = leading(debounce, (message: string) => console.log(message), 250);
trigger("Hello!");
trigger.clear(); // clears a timeout in progress

// with throttle
import { leading, throttle } from "@solid-primitives/scheduled";

const trigger = leading(throttle, (message: string) => console.log(message), 250);
trigger("Hello!");
trigger.clear(); // clears a timeout in progress

leadingAndTrailing

Creates a scheduled and cancellable callback that will be called on leading edge for the first call, and trailing edge thereafter.

The timeout will be automatically cleared on root dispose.

How to use it

// with debounce
import { leadingAndTrailing, debounce } from "@solid-primitives/scheduled";

const trigger = leadingAndTrailing(debounce, (message: string) => console.log(message), 250);
trigger("Hello!");
trigger.clear(); // clears a timeout in progress

// with throttle
import { leadingAndTrailing, throttle } from "@solid-primitives/scheduled";

const trigger = leadingAndTrailing(throttle, (message: string) => console.log(message), 250);
trigger("Hello!");
trigger.clear(); // clears a timeout in progress

createScheduled

Creates a signal used for scheduling execution of solid computations by tracking.

How to use it

createScheduled takes only one parameter - a schedule function. This function is called with a callback that should be scheduled. It should return a function for triggering the timeout.

// e.g. with debounce
createScheduled(fn => debounce(fn, 1000));
// e.g. with throttle
createScheduled(fn => throttle(fn, 1000));
// e.g. with leading debounce
createScheduled(fn => leading(debounce, fn, 1000));
// e.g. with leading throttle
createScheduled(fn => leading(throttle, fn, 1000));

It returns a signal that can be used to schedule execution of a solid computation. The signal returns true if it's dirty (callback should be called) and false otherwise.

import { createScheduled, debounce } from "@solid-primitives/scheduled";

const scheduled = createScheduled(fn => debounce(fn, 1000));

const [count, setCount] = createSignal(0);

createEffect(() => {
  // track source signal
  const value = count();
  // track the debounced signal and check if it's dirty
  if (scheduled()) {
    console.log("count", value);
  }
});

// or with createMemo
const debouncedCount = createMemo((p: number = 0) => {
  // track source signal
  const value = count();
  // track the debounced signal and check if it's dirty
  return scheduled() ? value : p;
});

Scheduling explanation

This package provides 4 different methods for scheduling a callback. Pick one that suits your application.

TOP: scheduled function triggered
BOTTOM: called user callback

1. debounce
2. throttle
3. leading debounce
4. leading throttle
5. leadingAndTrailing debounce
6. leadingAndTrailing throttle

   █   █     █
------------------------>
1.                  █
2.        █         █
3. █
4. █         █
5. █                █
6. █      █         █

Interactive DEMO of the schematic above

Changelog

See CHANGELOG.md