All Projects β†’ cmseaton42 β†’ Task Easy

cmseaton42 / Task Easy

Licence: mit
A simple, customizable, and lightweight priority queue for promises.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Task Easy

Flowa
πŸ”₯Service level control flow for Node.js
Stars: ✭ 66 (-72.95%)
Mutual labels:  async, promise, task, runner
Fun Task
Abstraction for managing asynchronous code in JS
Stars: ✭ 363 (+48.77%)
Mutual labels:  async, promise, task
best-queue
Queue in runtime based promise
Stars: ✭ 26 (-89.34%)
Mutual labels:  task, queue, promise
Fennel
A task queue library for Python and Redis
Stars: ✭ 24 (-90.16%)
Mutual labels:  async, task, queue
Then
🎬 Tame async code with battle-tested promises
Stars: ✭ 908 (+272.13%)
Mutual labels:  async, promise, task
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (-41.39%)
Mutual labels:  async, promise, task
Await Of
await wrapper for easier errors handling without try-catch
Stars: ✭ 240 (-1.64%)
Mutual labels:  async, promise
Message Bus
Go simple async message bus
Stars: ✭ 166 (-31.97%)
Mutual labels:  async, queue
Maid
Markdown driven task runner.
Stars: ✭ 1,999 (+719.26%)
Mutual labels:  task, runner
Nake
Magic script-based C# task runner for .NET Core
Stars: ✭ 183 (-25%)
Mutual labels:  task, runner
Foy
A simple, light-weight and modern task runner for general purpose.
Stars: ✭ 157 (-35.66%)
Mutual labels:  promise, runner
Opq
A simple, in-memory queue with worker pooling and rate limiting in Elixir.
Stars: ✭ 178 (-27.05%)
Mutual labels:  in-memory, queue
Ppipe
pipes values through functions, an alternative to using the proposed pipe operator ( |> ) for ES
Stars: ✭ 192 (-21.31%)
Mutual labels:  async, promise
Metasync
Asynchronous Programming Library for JavaScript & Node.js
Stars: ✭ 164 (-32.79%)
Mutual labels:  async, promise
Async Busboy
Promise based multipart form parser for KoaJS
Stars: ✭ 159 (-34.84%)
Mutual labels:  async, promise
Kitsu
🦊 A simple, lightweight & framework agnostic JSON:API client
Stars: ✭ 166 (-31.97%)
Mutual labels:  async, promise
Node Rethinkdb Job Queue
A persistent job or task queue backed by RethinkDB.
Stars: ✭ 158 (-35.25%)
Mutual labels:  task, queue
Fluture
πŸ¦‹ Fantasy Land compliant (monadic) alternative to Promises
Stars: ✭ 2,249 (+821.72%)
Mutual labels:  async, promise
Tickedoff
Tiny library (<200B gzip) for deferring something by a "tick"
Stars: ✭ 213 (-12.7%)
Mutual labels:  async, promise
Promise Queue
Promise-based queue
Stars: ✭ 210 (-13.93%)
Mutual labels:  promise, queue

Task Easy Logo

npm license Travis Coveralls github GitHub stars

Task Easy - Promise Queue Made Easy βœ…

A simple, customizable, and lightweight priority queue for promise based tasks.

Now with types!!! Big thanks to Emiliano Heyns 🍻

  • See below example for typescript version

Getting Started

Install with npm

npm install task-easy --save

How it Works

TaskEasy is built as an extension of a simple heap data structure. Tasks are queued by simply passing a task (function) to the .schedule method along with an array of arguments to be called as well as an object to describe the task's relative priority. The caller is returned a promise which will resolve once the scheduled task has ran. Priority determination is left up to the user via a function that accepts task priority object and returns a judgement.

Usage

The usage of TaskEasy is best given by example so that is the route we will take.

In this example, we will be passing priority objects to the scheduler that will be marked by the following signature.

{
    // An integer representing priority,
    // the higher the number the higher the priority
    priority: Number,

    // A timestamp to illustrate when the task
    // was scheduled, used as a 'tie-breaker' for
    // tasks of the same priority
    timestamp: Date
}

NOTE

The priority object's signature that you want to queue your items with is 100% up to you. πŸ˜„

Now, let's create a function that will receive our priority objects and output a priority judgment so that TaskEasy knows how to handle queued tasks. Our function will be passed two arguments (the priority objects of two scheduled tasks) and return a judgement indicating which task is of higher priority. If we return true, then we are communicating to TaskEasy that the first task is higher priority than the second task or vice versa

// This function is passed to the TaskEasy contructor and will be used internally to determine tasks order.
const prioritize = (obj1, obj2) => {
    return obj1.priority === obj2.priority
        ? obj1.timestamp.getTime() < obj2.timestamp.getTime() // Return true if task 1 is older than task 2
        : obj1.priority > obj2.priority; // return true if task 1 is higher priority than task 2
};

Now, we can initialize a new TaskEasy instance.

const { TaskEasy } = require("task-easy");

const max_tasks = 200; // How many tasks will we allow to be queued at a time (defaults to 100)
const queue = new TaskEasy(prioritize, max_tasks);

Now, lets build an async function to demo the scheduler.

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

NOTE

Scheduled tasks MUST be functions that return promises. This works well with async functions or with ES2017 ASYNC/AWAIT functions.

Now, that we have a task to schedule, let's schedule some tasks. The .schedule method takes three arguments, the task to call, an array of arguments, and a priority object that is associated with the scheduled task. It will return a promise that will resolve or reject once the task has been ran.

// .schedule accepts the task signature,
// an array or arguments, and a priority object
const task1 = queue
    .schedule(delay, [100], { priority: 1, timestamp: new Date() })
    .then(() => console.log("Task 1 ran..."));

const task2 = queue
    .schedule(delay, [100], { priority: 1, timestamp: new Date() })
    .then(() => console.log("Task 2 ran..."));

const task3 = queue
    .schedule(delay, [100], { priority: 2, timestamp: new Date() })
    .then(() => console.log("Task 3 ran..."));

const task4 = queue
    .schedule(delay, [100], { priority: 1, timestamp: new Date() })
    .then(() => console.log("Task 4 ran..."));

const task5 = queue
    .schedule(delay, [100], { priority: 3, timestamp: new Date() })
    .then(() => console.log("Task 5 ran..."));

// OUTPUT
// Task 1 ran...
// Task 5 ran...
// Task 3 ran...
// Task 2 ran...
// Task 4 ran...

NOTE

In the above example, task1 resolved first as it once put onto the queue first and was immediately called as it was the only task on the queue at that time.

Now with Typescript

import { TaskEasy } from "task-easy";

// Define interface for priority
//  objects to be used in the
//  TaskEasy instance
interface IPriority {
    priority: number;
    timestamp: Date;
}

// Define delay function type
// -> Must extend  Task<T>: (...args) => Promise<T>
type delayFn = (ms: number) => Promise<undefined>;

// Define delay function of type 'delayFn' defined above
const delay: delayFn = ms => new Promise(resolve => setTimeout(resolve, ms));

// Define priority function
// -> Must extend (obj1: T, obj2: T) =>
const prioritize = (obj1: IPriority, obj2: IPriority) => {
    return obj1.priority === obj2.priority
        ? obj1.timestamp.getTime() < obj2.timestamp.getTime() // Return true if task 1 is older than task 2
        : obj1.priority > obj2.priority; // return true if task 1 is higher priority than task 2
};

// Initialize new queue
const queue = new TaskEasy(prioritize); // equivalent of TaskEasy<IPriority>(prioritize) via type inference

// .schedule accepts the task signature,
// an array or arguments, and a priority object
// -> with type inference
const task1 = queue
    .schedule(delay, [100], { priority: 1, timestamp: new Date() })
    .then(() => console.log("Task 1 ran..."));

const task2 = queue
    .schedule(delay, [100], { priority: 1, timestamp: new Date() })
    .then(() => console.log("Task 2 ran..."));

// Definitely typed
const task3 = queue
    .schedule<undefined, delayFn>(delay, [100], { priority: 2, timestamp: new Date() })
    .then(() => console.log("Task 3 ran..."));

const task4 = queue
    .schedule<undefined, delayFn>(delay, [100], { priority: 1, timestamp: new Date() })
    .then(() => console.log("Task 4 ran..."));

const task5 = queue
    .schedule<undefined, delayFn>(delay, [100], { priority: 3, timestamp: new Date() })
    .then(() => console.log("Task 5 ran..."));

// OUTPUT
// Task 1 ran...
// Task 5 ran...
// Task 3 ran...
// Task 2 ran...
// Task 4 ran...

Built With

Contributers

License

This project is licensed under the MIT License - see the LICENCE file for details

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].