Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

🚦 A utility for rate-limiting function calls with fine-grained control.

License

Notifications You must be signed in to change notification settings

shahradelahi/ts-throttle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@se-oss/throttle
CI NPM Version MIT License npm bundle size Install Size

@se-oss/throttle is a utility for rate-limiting function calls, offering fine-grained control with features like strict mode, weighted throttling, and abort signals.


πŸ“¦ Installation

npm install @se-oss/throttle
Install using your favorite package manager

pnpm

pnpm install @se-oss/throttle

yarn

yarn add @se-oss/throttle

πŸ“– Usage

Basic Usage

Throttle a function to be called at most twice per second.

import { throttle } from '@se-oss/throttle';

const throttled = throttle(async (id) => fetchData(id), {
  limit: 2,
  interval: 1000,
});

for (let i = 1; i <= 6; i++) {
  throttled(i).then(console.log);
}

Abort Signal

Abort pending executions using an AbortSignal.

import { throttle } from '@se-oss/throttle';

const controller = new AbortController();
const throttled = throttle(work, {
  limit: 1,
  interval: 1000,
  signal: controller.signal,
});

await throttled();
controller.abort('stopped');
await throttled(); // Rejects with 'stopped'

Delay Notifications

Get notified when function calls are delayed due to limits.

const throttled = throttle(work, {
  limit: 1,
  interval: 1000,
  onDelay: (...args) => console.log('Delayed:', ...args),
});

Weighted Throttling

Assign custom costs to different function calls.

const throttled = throttle(fetchItems, {
  limit: 100,
  interval: 1000,
  weight: (count) => 1 + count,
});

await throttled(10); // Costs 11 points

Queue Management

Monitor and manage the execution queue size.

const throttled = throttle(work, { limit: 1, interval: 1000 });

if (throttled.queueSize < 5) {
  await throttled();
}

πŸ“š Documentation

For all configuration options, please see the API docs.

🀝 Contributing

Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.

Thanks again for your support, it is much appreciated! πŸ™

License

MIT Β© Shahrad Elahi and contributors.

About

🚦 A utility for rate-limiting function calls with fine-grained control.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors