synchronizer

0.1.0 • Public • Published

SYNCHRONIZER

Build Status NPM Version License

utility for exclusive access control

Installation

npm install synchronizer

API

  • synchronizer.create(object: any): function
    • return a synchronized decorator function

Examples

For example, the below function needs exclusive access control.

function asyncFuncNeedsExclusiveAccessControl(message) {
  if (asyncFuncNeedsExclusiveAccessControl.LOCKED) {
    console.error("FATAL ERROR: THIS FUNCTION IS LOCKED!!");
    return process.exit(1);
  }
  asyncFuncNeedsExclusiveAccessControl.LOCKED = true;
 
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      console.log(message);
 
      asyncFuncNeedsExclusiveAccessControl.LOCKED = false;
 
      resolve(message);
    }, 500);
  });
}

The above function locks a single resource. So you cannot execute the below code.

var object = { name: "alice" };
 
asyncFuncNeedsExclusiveAccessControl(object.name + "!!").then(function(res) {
  console.log("-->", res);
});
 
asyncFuncNeedsExclusiveAccessControl(object.name + "??").then(function(res) {
  console.log("-->", res);
});

In using synchronizer, you can execute that code using synchronized decorator.

var object = { name: "alice" };
var synchronized = synchronizer.create(object);
 
synchronized(function(object) {
  return asyncFuncNeedsExclusiveAccessControl(object.name + "!!");
}).then(function(res) {
  console.log("-->", res);
});
 
synchronized(function(object) {
  return asyncFuncNeedsExclusiveAccessControl(object.name + "??");
}).then(function(res) {
  console.log("-->", res);
});

LICENSE

MIT

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i synchronizer

    Weekly Downloads

    4

    Version

    0.1.0

    License

    MIT

    Last publish

    Collaborators

    • mohayonao