Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Object Locking for JavaScript: It's what you need when you need it, but you have to wait in line to get it.
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
README.md
package.json

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);
Something went wrong with that request. Please try again.