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

@reactutils/use-delayed-stack

v1.0.3

Published

A React hook used for batching real-time or periodic data processing by accumulating data in a stack and calling a callback function with a specified delay.

Downloads

23

Readme

@reactutils/use-delayed-stack

NPM NPM

a React hook used for batching real-time or periodic data processing by accumulating data in a stack and calling a callback function with a specified delay.

The purpose of the use-delayed-stack hook is to allow for efficient handling of real-time or periodic data by accumulating it in a stack over time and calling a user-provided callback function after a specified delay. This allows for batching of data processing, resulting in improved performance over calling the callback function for each individual data point.

Installation

npm install @reactutils/use-delayed-stack

# or

yarn add @reactutils/use-delayed-stack

This is a custom React hook named use-delayed-stack. It takes in three arguments:

callback: A function that takes an array of type T as its argument and returns void. This function will be called whenever the stack is flushed.

delay: A number which represents the delay in milliseconds after which the stack is flushed. initialValue: An optional argument which determines the initial value of the stack. If not provided, the initial value of the stack will be an empty array. The hook returns an array of two values:

pushToStack: A function that takes a value of type T as its argument and adds it to the stack.

cancel: A function that aborts the current timer and creates a new abort controller. The hook uses useRef to create references to the current stack, timer and abort controller. It also uses useEffect to create a new interval timer when the hook is called and to clear it up when it's unmounted.

Whenever the timer is triggered, it checks if the stack has any items, and if it does, it will call the provided callback and empty the stack. The cancel function aborts the current timer and creates a new one so that the stack will start accumulating new items again.

Usage:

import useDelayedStack from '@reactutils/use-delayed-stack';

const [pushToStack, cancel] = useDelayedStack(callback, delay, initialValue);

// To push a value onto the stack
pushToStack(value);

// To stop the timer manually
cancel();