-
-
Save jhurliman/3113407 to your computer and use it in GitHub Desktop.
A simple mutex library for node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventEmitter = require('events').EventEmitter; | |
module.exports = function() { | |
var queue = new EventEmitter(); | |
var locked = false; | |
this.lock = function lock(fn) { | |
if (locked) { | |
queue.once('ready', function() { lock(fn); }); | |
} else { | |
locked = true; | |
fn(); | |
} | |
}; | |
this.release = function release() { | |
locked = false; | |
queue.emit('ready'); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice, will work for single process, but not multiple processes right? this.lock is global...?