Object Locking for JavaScript: It's what you need when you need it, but you have to wait in line to get it.
Switch branches/tags
Clone or download
Latest commit c05c5fc Mar 13, 2013
Permalink
Failed to load latest commit information.
lib ws Mar 13, 2013
test Simplify usage Mar 13, 2013
.gitignore git ignore Mar 13, 2013
README.md bind this Mar 13, 2013
package.json Release 0.0.3 Mar 13, 2013

README.md

xx: Object Locking for JavaScript

It's what you need when you need it, but you have to wait in line to get it.

xx simplifies the synchronization of data objects between concurrent tasks.

Installation

  • Node.JS: npm install xx
  • Browsers: Add lib/xx.js to your HTML page.

Intro

Here's a brief example that shows how to use xx:

var xx = require('xx'); // import xx

var config = {}; // Have some data to guard

config.xx = xx(); // Create a lock object

// Acquire lock on the object with an async callback:
config.xx(
	function() {
		// Invoked when the object is available

		// Call without a function when you want to unlock the object:
		config.xx();
	}.bind(this)
);

Usage with Async Libraries

Here's a brief example that shows how it can be used with an async library (such as ff):

var xx = require('xx'); // import xx
var ff = require('ff'); // Use ff for a demo

var config = {
	fileA: "A.txt",
	fileB: "B.txt"
}; // Have some data to guard

config.xx = xx(); // Create a lock object

var f = ff(this, function () {

	// Added: Wait to acquire lock
	config.xx(f());

}, function () {
    fs.readFile(config.fileA, f());
    fs.readFile(config.fileB, f());
}, function (fileA, fileB) {
    concatFiles(fileA, fileB, f());
}, function (result) {
	config.result = result.toUpperCase();

	// Added: Done with lock here!
	config.xx();

    f();
}).cb(cb);