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

live-limit

v1.1.0

Published

A promise based generic rate limiter. Runs a limited number of async function calls at a time, queuing or discarding remaining calls.

Downloads

6

Readme

Live Limit

npm npm bundle size coverage badge tests badge 0 dependencies MIT license

Need to limit the number of concurrent requests to a server? Or make sure only one function call is running at a time? Live Limit to the rescue!

Docs

See the Live Limit Docs for details.

Install

# with npm
npm install --save live-limit
# with yarn
yarn add live-limit

Requirements

Live Limit requires Promises and Map.values. This is available on everything newer than node.js 12 and any non-IE browser.

If Internet Explorer support is required, consider polyfills.

Example

import { LiveLimit } from "live-limit";

// Setup
// Only allows 3 function calls to be live at a time
const LIMITER = new LiveLimit({ maxLive: 3 });

// Making a single call:
const param = 1;
LIMITER.limit(async () => {
    // code here
});

// Making many calls:
//
// With these limiter settings, the first 3 calls will start immediately.
// The next 3 will pause until one of the earlier calls resolve or reject,
// at which point another call will start.
const params = [1, 2, 3, 4, 5, 6];
params.forEach((param) => {
    LIMITER.limit(async () => {
        myFunc(param);
    });
});

// The async calls can also return a result.
// The promise returned by `LIMITER.limit` will be the result that your function returned.
const out = await LIMITER.limit(async () => {
    return 'foo';
});
console.log(out); // prints 'foo'

Example with Axios, React, and Redux

// ... other imports
import { LiveLimit } from "live-limit";

// 3 concurrent connections at max
const LIMITER = new LiveLimit({ maxLive: 3 });

function SendButton() {
    const dataToSend: any[] = selector((state) => state.data.to.send);
    const [state, setState] = useState<"done" | "loading" | undefined>();

    return (
        <Button
            // Disable button while loading or done
            disabled={state !== undefined}
            // When clicked, make all the requests
            onClick={() => {
                // Disable the button
                setState("loading");
                // Wait until it's done to mark as done
                Promise.all(
                    dataToSend.map(
                        // Creating a promise for each request
                        async (data) => {
                            // Every request goes through the limiter
                            await LIMITER.limit(async () => {
                                await axios.post("/url/to/send/to", data);
                            });
                        }
                    )
                ).then(() => {
                    setState("done");
                });
            }}
        >
            Send all the data
        </Button>
    );
}