• Stars
    star
    191
  • Rank 202,877 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A queue with custom concurrency and time limit.

timequeue.js

A queue with custom concurrency and time limits. Inspired by async/queue, but also with variable number of arguments in the worker, events, and with optional time limits.

Depfu codecov

Usage

const TimeQueue = require('timequeue');

const worker = (arg1, arg2, callback) => {
  someAsyncFunction(calculation(arg1, arg2), callback);
};

// Worker can be an async function.
const worker = async (arg1) => {
  let result1 = await anotherSyncFunction(arg1);
  return andAnotherOne(result1);
};

// create a queue with max 5 concurrency every second
let q = new TimeQueue(worker, { concurrency: 5, every: 1000 });

// Push tasks onto the queue.
q.push(42, 24);
q.push(2, 74);

// Optional callback when pushing tasks.
q.push(2, 2, (err, result) => {
  // task finished
});

// Can use promise/await syntax instead.
let result = await q.push(3, 5);

API

new TimeQueue(worker, [options])

Creates a new instance of a queue. Worker must be a function with a callback for its last argument. The callback must be called in order for the queue to know when the worker has finished a task. options defaults to the following

{
  // Maximum tasks to execute concurrently.
  concurrency: 1

  // How much time in milliseconds to allow no more than
  // the max number of concurrent tasks to run.
  // If the max amount of concurrent tasks finish faster than this time limit,
  // additional tasks will wait until enough time has passed before starting.
, every: 0

  // Maximum number of tasks to keep in the queue.
  // While full, pushed tasks will be ignored.
, maxQueued: Infinity

  // If set, tasks will error if they take too much time.
  // if callback was given to that task, it will be called with the error,
  // otherwise, the returned promise should be `caught`.
, timeout: 0

  // You can pass a custom store to share tasks between several queues.
  // Default is MemoryStore from `src/mem-store.ts`.
  // Look at `example/redis-store.ts` for an example that saves tasks onto redis.
, store: MemoryStore
}

All of these options can later be edited on the queue instance.

TimeQueue#active

How many tasks are currently active.

TimeQueue#intransit

If you use the every option to queue up tasks, this property will be delayed from being updated until there are free spots open for new tasks to begin. active will be updated as soon as a task finishes, even if the next one is just a timeout around the corner.

TimeQueue#finished

How many tasks have finished in total.

async TimeQueue#push(data..., [callback])

Pushes a new task to the queue. Any number of arguments can be given. An optional callback can also be given as the last parameter. The callback will be called when the task is finished or if there was any error running the worker.

If the queue is full, pushed tasks will be ignored.

async TimeQueue#store.getQueueLen()

How many tasks are currently in the queue.

TimeQueue#isFull()

Returns true if queue is full.

TimeQueue#die()

Empties queue and clears the timeouts TimeQueue sets to keep track of running tasks. Currently running tasks will still complete.

Events

Event: 'full'

Queue is full.

Event: 'empty'

Queue is empty, with tasks still running.

Event: 'drain'

Queue is empty and last task has finished.

Install

npm install timequeue

Tests

Tests are written with mocha

npm test

More Repositories

1

node-ytdl-core

YouTube video downloader in javascript.
JavaScript
4,507
star
2

randexp.js

Create random strings that match a given regular expression.
JavaScript
1,779
star
3

node-ytdl

Command line youtube video downloader.
JavaScript
1,188
star
4

node-m3u8stream

Concatenates segments from a m3u8/dash-mpd playlist into a consumable stream.
TypeScript
213
star
5

node-feedsub

Subscribes to RSS/Atom/JSON feeds and notifies on new items.
TypeScript
196
star
6

node-torrent

Torrent reader, writer, and hash checker for node.
JavaScript
190
star
7

feedme.js

RSS/Atom/JSON feed parser
TypeScript
155
star
8

clusterhub

Sync data in your cluster applications.
JavaScript
131
star
9

ret.js

Tokenizes a string that represents a regular expression.
JavaScript
90
star
10

socket.io-clusterhub

socket.io storage powered by clusterhub for multi process applications.
JavaScript
70
star
11

chrome-options

Options page for Chrome extensions
JavaScript
64
star
12

node-muk

Mock object methods and dependencies.
JavaScript
57
star
13

node-miniget

A small http(s) GET library.
TypeScript
52
star
14

node-streamify

Streamify helps you easily provide a streaming interface for your code.
JavaScript
51
star
15

pauseable.js

Create event emitters, intervals, and timeouts that can be paused and resumed.
JavaScript
47
star
16

node-streamspeed

A simple way to keep track of the speed of your node streams.
TypeScript
38
star
17

irc-colors.js

Color and formatting for irc bots made easy. Inspired by colors.js and cli-color.
JavaScript
35
star
18

node-kat

File and stream concatenation the right way.
JavaScript
25
star
19

node-stream-equal

Test that two readable streams are equal to each other.
TypeScript
24
star
20

node-streamin

Provide a better streaming api in your app.
JavaScript
20
star
21

npm-updates

Emits update events from the npm repository.
JavaScript
16
star
22

node-jstream

Continuously reads in JSON and outputs Javascript objects.
JavaScript
16
star
23

muk-prop.js

Mock object methods and properties
JavaScript
12
star
24

hook.io-feedsub

hook.io bindings for RSS/Atom/JSON feeds
JavaScript
11
star
25

node-eventyoshi

Allows several event emitters to be listened and emitted through a single one.
JavaScript
11
star
26

node-torrent-cli

CLI for reading, writing, and hash checking torrents
JavaScript
10
star
27

chrome-veefeed

Follow YouTube and Twitch channels, get notifications, categorize videos
JavaScript
9
star
28

pokecry

guess pokemon through their cries
JavaScript
8
star
29

node-newsemitter

An event emitter that emits only new events.
TypeScript
7
star
30

clusterchat

A simple multi process chat demo using socket.io-clusterhub
JavaScript
7
star
31

ordered-queue.js

Queue with concurrency that starts tasks in order and runs them in parallel.
JavaScript
7
star
32

node-module-boilerplate

My personal boilerplate that I use to create node modules.
JavaScript
6
star
33

vim-relative-indent

Hide leading indent
Vim Script
5
star
34

hook.io-npm

Hook that emits on npm module updates.
JavaScript
5
star
35

jps

A scraper for the jpopsuki tracker.
JavaScript
5
star
36

nickserv

Communicates with the NickServ IRC service
JavaScript
5
star
37

node-streamit

JavaScript
4
star
38

ann

IRC bot made to announce and for convenience.
CoffeeScript
4
star
39

vim-frozen

colorscheme for vim
Vim Script
3
star
40

twi

really basic twitter cli client
JavaScript
3
star
41

node-writestreamp

A writable file stream that creates directories as needed.
JavaScript
3
star
42

gifchat

gifphy bot for hipchat
JavaScript
2
star
43

queue2.js

A unordered queue and ordered queue working together.
JavaScript
2
star
44

pokenum

A way to remember numbers using Pokemon.
JavaScript
1
star
45

node-muk-require

Mock dependencies
JavaScript
1
star
46

nickie

Nickserv irc bot.
JavaScript
1
star
47

node-callme

Hey I just met you, and this is crazy, but here's my callback.
JavaScript
1
star
48

jquery.rubber.js

jQuery plugin that stretches text fields to fit their content.
JavaScript
1
star