var CallbackBuffering = require("../lib");

var cb = new CallbackBuffering();

var buffer = [];
function callbacks() {
    for (var i = 0; i < buffer.length; ++i) {
        buffer[i].apply(this, arguments);
    }
    buffer = [];
}

function getUniqueRandom(i, callback) {
    buffer.push(callback);
    setTimeout(function() {
        callbacks(i);
    }, 10);
}

for (var i = 0; i < 10; ++i) {
    (function (i) {
        doSomethingUniqueAsync(i, function (c) {
            console.log("> Unique random is " + c);
        });
    })(i);
}

// Dependencies
var CallbackBuffering = require("../lib");

// Create a new callback buffer
var cb = new CallbackBuffering();

/**
 * getUniqueRandom
 * Callbacks a random number that is unique after doing something async.
 *
 * @name getUniqueRandom
 * @function
 * @param {Number} i The current index.
 * @param {Function} callback The callback function.
 * @return {undefined}
 */
function getUniqueRandom(i, callback) {
    console.log("> Unique random requested " + i + " times.");
    if (cb.isDone) { return cb.done(callback); }
    cb.add(callback);
    if (cb.isWaiting) { return; }
    cb.wait();
    console.log("* Generating unique random");
    var r = Math.random();
    setTimeout(function() {
        cb.callback(r);
    }, 10);
}

// Request unique random multiple times
for (var i = 1; i < 11; ++i) {
    (function (i) {
        getUniqueRandom(i, function (c) {
            console.log("> Unique random is " + c);
        });
    })(i);
}

Use the library to buffer the callbacks
Use the library to buffer the callbacks
