https://github.com/seriousbug/live-limit
A promise based generic rate limiter. Runs a limited number of async function calls at a time, queuing or discarding remaining calls.
https://github.com/seriousbug/live-limit
Last synced: 12 months ago
JSON representation
A promise based generic rate limiter. Runs a limited number of async function calls at a time, queuing or discarding remaining calls.
- Host: GitHub
- URL: https://github.com/seriousbug/live-limit
- Owner: SeriousBug
- License: mit
- Created: 2022-06-04T18:55:59.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-04-17T23:40:52.000Z (about 3 years ago)
- Last Synced: 2025-07-01T23:38:31.889Z (12 months ago)
- Language: TypeScript
- Homepage:
- Size: 322 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Live Limit
[](https://www.npmjs.com/package/live-limit) [](https://bundlephobia.com/package/live-limit@1.0.0) [](https://app.codecov.io/gh/SeriousBug/live-limit) [](https://github.com/SeriousBug/live-limit/actions/workflows/test.yml) [](https://www.npmjs.com/package/live-limit) [](https://github.com/SeriousBug/live-limit/blob/main/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](https://seriousbug.github.io/live-limit/) for details.
## Install
```sh
# with npm
npm install --save live-limit
# with yarn
yarn add live-limit
```
#### Requirements
Live Limit requires [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility) and [`Map.values`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values#browser_compatibility). This is available on everything newer than node.js 12 and any non-IE browser.
If Internet Explorer support is required, consider polyfills.
## Example
```ts
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
```tsx
// ... 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 (
{
// 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
);
}
```