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

dyna-job-queue

v5.2.0

Published

About

Downloads

125

Readme

About

A Job Queue.

Add your jobs there and the .onJob will be called whenever is possible.

Only one job will be in progress each time.

Installation

In the root folder of you app run:

npm install --save dyna-job-queue

Methods

constructor(config: IDynaJobQueueConfig)

interface IDynaJobQueueConfig {
  parallels?: number;       // default 1, the number of the parallel jobs
}

jobFactory(func: (...params: any[]) => Promise, priority: number = 1): () => Promise

Although the signature looks quite complex, is the easiest method of the job queue ever.

It converts an already existed function/method that returns Promise, to a job that will be added to the queue.

Once you wrapped it the only you have is to call is as you did before. Nothing is changed.

For typescript writers, there is no need even to define the TResolve, as explicitly comes from method's definition.

example:

class NewsFeeder {
    private readonly feeds: number[] = [];
    private queue = new DynaJobQueue();
    
    constructor() {
      this.addFeed = this.queue.jobFactory(this.addFeed.bind(this));  // That's all
    }
    
    public addFeed(feed: number, afterDelay: number): Promise<number> {
      return new Promise((resolve: Function) => {
        setTimeout(() => {
          this.feeds.push(feed);
          resolve(feed);
        }, afterDelay);
      });
    }
}

addJob(command: string, data: any, priority: number = 1): void

Adds a job and will be executed when all other jobs will be executed (FIFO) according also the priority (where is optional).

The command is a string that will help you to understand what is this job.

The data can be anything, let's say, the parameters for this job.

The priority is optional. Default value is 1. Smaller numbers have priority.

example:

queue.addJob('loadConfig', {endPoint: 'http://example.com/awesomeCondig'});

queue.addJob('loadImage', {endPoint: 'http://example.com/awesomeCondig'}, 2); // <-- priotity 2

addJobCallback(callback: (done: Function) => void, priority: number = 1): void

This is another way to add a job. You don't define command and data but directly the callback function you want to call. The callback will be called with only the done: Function as argument.

example:

// implement an anonymous function
queue.addJobCallback((done: Function) => {
  // so something special here
  done();
});

// as above, define also the priority
queue.addJobCallback((done: Function) => {
  // so something special here
  done();
}, 2);  // <-- priority 2!

// use an already implemented function
queue.addJobCallback(this.processMyJob, 2);  // <-- priority 2!

addJobPromise(callback: (resolve: (data?: TResolve) => void, reject: (error?: any) => void) => void, priority: number = 1): Promise

This method adds a job with callback and returns a Promise. It create a new Promise. The callback provides two functions, the resolve and the reject when will fulfill the Promise. In resolve pass the output of the Promise.

The difference with the callback of other methods is that you have to call the resolve or reject instead of done; that's all!

So this method is a Promise generator. The benefit is that you can get the Promise that will be fulfilled on the proper time.

example:

queue.addJobPromise((resolve: Function, reject: Function) => {
  try{
    // do some work here
    resolve(data);    
  } catch (err) {
    reject(err);
  }
}, 2) // <-- this 2 is the priority
  .then((data: any) => {
    // our resloved data are here
  })
  .catch((err: any)) => {
    // our exception is dropped here
  });

addJobPromised(returnPromise: () => Promise, priority: number = 1): Promise

This method adds a job from a Promise.

Since the Promise by it's nature is executed instantly, you have to pass a callback that will execute the promise at the proper time.

example:

queue.addJobPromises((resolve: Function, reject: Function) => {
  return fetch('http://api.example.com/customer-info?:id=4853847343');
}, 2) // <-- this 2 is the priority
  .then((data: any) => {
    // our resloved data are here
  })
  .catch((err: any)) => {
    // our exception is dropped here
  });

alldone(): Promise

Promise that is resolved when the queue becomes empty.

Properties

stats: { jobs: number, running: number}}

jobs is the number of the jobs that pending

running the number of parallel running jobs

Note: it is possible to have jobs but not running in the rare case of switching the jobs.

Change log

v2.0.0

First stable version

v3.0.0

Export web and node versions.

You should import from dyna-job-queue/web or dyna-job-queue/node according your running environment.

For universal apps you should import with lazy load.

v5.1.0

New method allDone().