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

task-execution-limiter

v1.0.0

Published

Help you manage asynchronous tasks

Downloads

9

Readme

Task Execution Limiter

Build Status codecov

Help you manage asynchronous tasks

Features

  • Can limit the execution rate with interval and limitation
  • Can limit the number of concurrent
  • Can limit the queue length
  • Can hook many lifecycle events
  • Ability to customize the processing logic when task queue overflows
  • Ability to change interval during run time, and it won't break the alignment of the interval

Limitation

  • Execution interval can not be greater than 2147483647ms or less than 1ms. See setInterval

Install

npm install --save task-execution-limiter

yarn add task-execution-limiter

Intro

task-execution-limiter mainly exposes three interfaces, including TaskExecutionLimiter, buildTaskExecutionLimiter and buildWithLimit. You can use class TaskExecutionLimiter, or you can use the tool functions buildTaskExecutionLimiter and buildWithLimit to encapsulate higher-order functions.

task-execution-limiter also exposes the QueueOverflowError class, which you can customize for other error types when you choose to implement a task queue overflow handler.

Quick Start

see gist examples

  • Using TaskExecutionLimiter class. case1 and case2

    • schedule method
    • withLimit method
  • Using functions. case5 and case6

    • buildTaskExecutionLimiter

    buildTaskExecutionLimiter is a simple encapsulation for TaskExecutionLimiter.schedule

    • buildWithLimit

    buildWithLimit is a simple encapsulation for TaskExecutionLimiter.withLimit

  • Using custom overflow handler. case3 and case4

  • Using limit with decimals. case7

API

TaskExecutionLimiter

constructor(options) => limiter: TaskExecutionLimiter

  • options.interval: Rate limit, representing the interval to the next tick. When interval is larger than 2147483647 or less than 1, the interval will be set to 1.setInterval Default: 1
    • Number
  • options.minInterval: Rate limit, representing the lower bound of interval. (Can be an integer or decimal). Default: 1
    • Number
  • options.maxInterval: Rate limit, representing the upper bound of interval. (Can be an integer or decimal). Default: 2147483647
    • Number
  • options.limit: Rate limit, representing the number of tasks that would be started each tick (Can be an integer or decimal). Default: Infinity
    • Number
  • options.concurrency: Concurrent quantitative restrictions. Default: Infinity
    • Number
  • options.queueLength: Task queue length. Default: Infinity
    • Number
  • options.queueOverflowHandler: Handle function when task queue length overflows. By default, the last task is rejected, and an error will be thrown, you can refer to QueueOverflowError to custom error type. see lib/util.defaultQueueOverflowHandler
    • Function: (q: Array, item: Object<task, resolve, reject>) => newq: Array

schedule(task) => limitedTask: Promise

  • task: Tasks to be processed.
    • Function: () => Promise
    • Function: () => Number, Array, Object...(primitive type)
    • Number, Array, Object...(primitive type)
  • limitedTask: Wrapped promise.
    • Promise

withLimit(fn) => limitedFn: Function

  • fn: Function to be processed.
    • Function: (...args) => any
  • limitedFn: Function to be processed with limitation.
    • Function: (...args) => any

interval(setter)

You can change interval of TaskExecutionLimiter, and it won't break the alignment of the interval(through the delay in execution).

const speedUp = () => { limiter.interval /= 2 }
const slowDown = () => { limiter.interval *= 2 }

willTick(setter)

Lifecycle event, called before interval tick. Default: noop

  • fn: () => any

didTick(setter)

Lifecycle event, called after interval tick. Default: noop

  • fn: () => any

willStart(setter)

Lifecycle event, called before limiter start running. Default: noop

  • fn: () => any

didStart(setter)

Lifecycle event, called after limiter start running. Default: noop

  • fn: () => any

willStop(setter)

Lifecycle event, called before limiter stop running. Default: noop

  • fn: () => any

didStop(setter)

Lifecycle event, called after limiter stop running. Default: noop

  • fn: () => any

buildTaskExecutionLimiter(options) => limiter: Function

  • options: The same as the constructor arguments of TaskExecutionLimiter.
  • limiter:
    • Function: (task) => limitedTask
      • task: Tasks to be processed.
        • Function: () => Promise
        • Function: () => Number, Array, Object...(primitive type)
        • Number, Array, Object...(primitive type)
      • limitedTask: Wrapped promise.
        • Promise

buildWithLimit(options) => limitedFn: Function

  • options: The same as the constructor arguments of TaskExecutionLimiter.
  • limitedFn: Function to be processed with limitation.
    • Function: (...args) => any

QueueOverflowError

You can extends this class when you choose to implement a task queue overflow handler.