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

global-process-queue-js

v1.0.4

Published

With the Global Process Queue you can create a Queue of ASYNC and AWAIT Processes for your Node.JS System. This Package provides an API to create an Initializer and a Finisher Tasks to run before and after your Processes Queue be executed.

Downloads

1

Readme

Global Process Queue

With the Global Process Queue, you can create a Queue of ASYNC and AWAIT Processes for your Node.JS System. This Package provides an API to create an Initializer and a Finisher Tasks to run before and after your Processes Queue be executed.

How to Use

The best way to explain how this package works is by example. This documentation provides an example that works like a real-world use case, though a simple example.

A Hypothetical Use Case

Suppose you have a Cart on your e-commerce and this cart has several tasks or processes (Add Product, Remove Product, Calculate Total, etc). So with the Global Process Queue, you can orchestrate the Cart Flow - that is your System Process Flow - and what happens globally via the initializer and the finisher methods, both optional.

We will use this simple example to provide a documentation as close as possible to a real-world example.

The Initializer Method

This method provides an API to you create a global process that happens before your queue be executed.

For example, suppose that you have a process called Update Stock. Probably this method needs to access a Database or a Cache Layer to be executed, thus with the Initializer Method you can get, for example, a Connection of your MySQL Connection Pool before the method be called by the Queue Execution, so when the Update Stock process be called, the MySQL Connection will be ready to execute queries in your database.

You can do something like that:

/**  
 * This function will be called before the process queue start  
 * Here you can start a mysql or redis connection for example  
 */  
const optionalInitializer = () => {  
  global.DB = (new MySQL()).getConnectionFromPool();  
};

Note that we declare a global variable called DB, to be accessible by your others methods, we'll see about it further, later on, this documentation.

The Finisher Method

Similar to the Initializer method, the Finisher method provides you with an API to create a global process, which runs after the queue execution.

As an example suppose that you need to close the MySQL Connection just after all process is executed. May you do it:

/**
 * This function will be called when all process on the queue are executed
 * Here you release the mysql connection from pool or a redis connection for example
 */
const optionalFinisher = () => {
  global.DB.finish();
};

The System Flow and The Process Queue

With the Process Queue API, you can add or remove ASYNC and AWAIT processes into a Queue. You can pass the Initializer and the Finisher methods to the Queue Handler to be executed before and after the queue process.

The System Flow

To make the most with the package you need to keep your system processes well delimited. First, you need to create your program flow. In the below example, a CartFlow object is created to abstract the flow. You can work in a functional way, for example:

/**
 * Example of an e-commerce cart flow
 *
 * @type {{CheckCartItems: function(), CalculateTotal: function(), UpdateStock: function()}}
 */
const CartFlow = {
  CheckCartItems: async () => {
    return new Promise((resolve) => setTimeout(() => {
      // You can use the global.DB here for example ...  
      console.log('CartFlow.CheckCartItems()');
      resolve();
    }, 1500));
  },
  CalculateTotal: async () => {
    return new Promise((resolve) => setTimeout(() => {
      // You can use the global.DB here for example ...  
      console.log('CartFlow.CalculateTotal()');
      resolve();
    }, 3000));
  },
  UpdateStock: () => {
    // You can use the global.DB here for example ...  
    console.log('CartFlow.UpdateStock()');
  }
};

The Process Queue

Once you delimited your system flows, you may create a service to execute that flow, using the Initializer, Finisher and your custom methods.

Adding Processes to the Queue

You can add processes into the desired position of the queue execution (optional), note the third parameter of the add() method:

const Process = require('global-process-queue');  
  
/**  
 * Executing the cart flow processes
 */  
const CartProcess = new Process.Handler(optionalInitializer, optionalFinisher);  
CartProcess.add(CartFlow.CheckCartItems, Process.ASYNC, 1);  
  
// will be executed after the first process, because the first process have a delay of 1.5s  
// you can await the the first process changing the type to Process.AWAIT (try by yourself)  
CartProcess.add(CartFlow.UpdateStock, Process.ASYNC, 3);  
  
// will be pushed into the queue at the latest position  
CartProcess.add(CartFlow.CalculateTotal, Process.AWAIT);  
  
// Executing the queue  
CartProcess.exec();

May you need to do something after the queue execution, you can do a similar thing like the below examples.

Removing Process From the Queue

Sometimes, depending on your flow, may you need to remove a specific process from the queue, may you do it:

const TaskToRemove = CartProcess.add(CartFlow.CalculateTotal, Process.AWAIT, 2);  
CartProcess.del(TaskToRemove);

Working With a Promise Response

const process = CartProcess.exec();
  
process.then(done => {
  // do something here...
  console.log('DONE:', done);  
});  

Await the Response

(async () => {  
  const done = await CartProcess.exec();  
    if (done) {
      // do something here...
      console.log('DONE:', done);  
    }  
})();

Notes About Async/Await Processes

  • Each process can be added to a specific position into the queue:
CartProcess.add(method, methodType, positionNumber);
  • You can have ASYNC and AWAIT processes.

  • All processes will be executed after the optional initializer method change to a ready state.

  • All processes will be executed before the optional finisher method be executed.

  • If you pushed ASYNC processes into your queue, be aware that the queue processing could be in a finished state before all ASYNC methods are executed - see the .then() or the if(done) lines in the example above.

Community Support

If you need help with this bundle please consider open a question on StackOverflow using the global-process-queue-js tag, it is the official support platform for this bundle.

Github Issues are dedicated to bug reports and feature requests.

Contributing

You can contribute to this project cloning this repository and in your clone, you just need to create a new branch using a name related to the new functionality which you'll create.
When you finish your work, you just need to create a pull request which will be revised, merged to master branch (if the code doesn't break the project) and published as a new release.