synchronizer
v0.1.0
Published
utility for exclusive access control
Downloads
15
Readme
SYNCHRONIZER
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