0.4.5 • Published 3 years ago

core-queue v0.4.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

core-queue

Simple, zero dependency, promise-based queue.

Package is under development. API might change.

Features

  • Promise based queue.
  • Concurrency.
  • Build-in flow control methods.

Installation

Via npm:

npm install core-queue

Via yarn:

yarn add core-queue

Loading the module

import { CoreQueue, CoreQueueOptions } from "core-queue";

Common usage

Create options
const options: CoreQueueOptions = {
  maxConcurrency: 1,
  maxTasks: 10,
  autostart: true
};
Create queue
const coreQueue: CoreQueue = new CoreQueue(options);
Add async task and handle result with promises
coreQueue
  .enqueue(() => someTask())
  .then(result => {})
  .catch(error => {});
Add async task and await result
try { 
  await coreQueue.enqueue(() => someTask());
}catch(error){
  // handle error
}
Await until every task is settled (resolved or rejected)
await coreQueue.done();

API

CoreQueue(options)

Returns new CoreQueue instance.

Getters

size number
  • Returns sum of tasks awaiting in queue and tasks which are currently in progress.
pendingSize number
  • Returns number of tasks in queue.
inProgressSize number
  • Returns number of tasks which currently beign executed.
isEmpty boolean
  • Returns true if the number of awaiting tasks is zero.
isFull boolean
  • Returns true if the options limit is exceeded.
  • Is calculated as pedningTasks + awaitingTasks.
peek
  • Returns first element without removing it.
  • Returns null if queue is empty.

Methods

start()
  • Start task execution (Have to be called if autostart is set to false).
  • Could be called after stop()
stop()
  • Stop task execution.
  • Task that was already started will settle.
clear()
  • Removes all pending tasks.
done() promise
  • Resolves after all tasks are settled and queue is empty.
enqueue(()=> {})
  • Add new async task into queue.
  • Throws error if maxTasks is exceeded.

CoreQueueOptions

maxTasks number
  • Maximum of pending and idle tasks in queue.
maxConcurrency number
  • Maximum of concurrently executed tasks.
autostart boolean
  • Specify if tasks should be resolved as soon as possible.
  • If set to false, then start() have to be called.

License

MIT

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago