GithubHelp home page GithubHelp logo

harish-tipstat / process-sync Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shabeer-ali-m/process-sync

0.0 2.0 0.0 120 KB

Process sync (ps) is a nodejs package to process your function in a Synchronous Queue. All the process will be in a queue and it will run one by one like mutex.

License: MIT License

JavaScript 100.00%

process-sync's Introduction

Process Sync

Process sync (ps) is a nodejs package to process your function in a Synchronous Queue. All the process will be in a queue and it will run one by one like mutex.

Features

  • Simple to Use
  • Light Weight
  • Zero Dependency

Working

The working of the Process Sync is very simple. While starting any process you can lock it, and once your process is done you can unlock it. So in between if any other process try to execute, it will automatically add to the process to the queue without running. Once you release the lock it will automatically start executing the next process which is in the queue.

Version

1.0.0

Example 1

These examples will demonstrate you how to use Process Sync. In this example we console 3 message with a interval of 3 seconds.

//including process-sync 
var ps = require('process-sync')

var demo = function(){
	/*
	* lock the process
	* This code will lock the process. So if any other process came it won’t execute it will add to a queue. Once you unlock the process the queued process will run simultaneously. 
	*/
	ps.lock();
	setTimeout(function(){
		console.log("This is a demo message. Next message will come only after 5 seconds.");
		/*
		* Unlocking the process
		* Once you finished your process you can release the lock so that the other process can run. if you didn’t unlocked the other process won’t run.
		*/
		ps.unlock();
	},3000);
}

/*
* Calling your process with process-queue
* ps.process(<function name>)
* This function is used to call your function through process queue.
*/
ps.process(demo);
ps.process(demo);
ps.process(demo);

Example 2

This will demonstrate you how to convert your function to sync with arguments.

//including process-sync 
var ps = require('process-sync')
var demo = function(msg){
	//locking the process
	ps.lock();
	setTimeout(function(){
		console.log("Message : "+msg+" .Next message will come only after 5 seconds.");
		//unlocking the process
		ps.unlock();
	},3000);
}

//calling your process with process-queue
ps.process(demo,["My First Message"]);
ps.process(demo,["My Second Message"]);
ps.process(demo,["My Third Message"]);

Example 3

Put all your process in a stack and process it.To make your list as stack enable the variable ps.stack as true.

var ps = require('process-sync')

var demo = function(msg){
	//locking the process
	ps.lock();
	setTimeout(function(){
		console.log("Message : "+msg+" .Next message will come only after 3 seconds.");
		//unlocking the process
		ps.unlock();
	},3000);
}

//setting your process sync as stack
ps.stack=true;
//locking the process at the beginning itself
ps.lock();
ps.process(demo,["My First Message"]);
ps.process(demo,["My Second Message"]);
ps.process(demo,["My Third Message"]);
//unlocking the process-sync after adding all process sync
ps.unlock();

License

MIT

process-sync's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.