Skip to content

Commit

Permalink
Added an Synchronizer to use on the client side
Browse files Browse the repository at this point in the history
  • Loading branch information
briemens committed Jul 4, 2013
1 parent 9a43c60 commit 23de1dc
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions crafity.Synchronizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*jslint node:true, bitwise: true, unparam: true, maxerr: 50, white: true */
/*!
* crafity.Synchronizer - Synchronize multiple events
* Copyright(c) 2011 Crafity
* Copyright(c) 2011 Bart Riemens
* Copyright(c) 2011 Galina Slavova
* MIT Licensed
*/

(function (crafity, undefined) {

(function (core) {

/**
* Module dependencies.
*/
if (!core.EventEmitter) {
throw new Error("Dependency 'EventEmitter' not found")
}

/**
* Initialize module
*/

core.Synchronizer = function Synchronizer(finish) {
"use strict";

var self = this
, finished = false
, lastError = null
, onfinishCalled = false
, handlers = []
, data = {}
, registerCalled = false;

this.register = function () {
registerCalled = true;
if (onfinishCalled) {
throw new Error("Can not register new callbacks after onfinish is called");
}
finished = false;

var callback, index, keys = [];
for (index = 0; index < arguments.length; index += 1) {
if (typeof arguments[index] === 'string') {
keys.push(arguments[index]);
} else if (index === arguments.length - 1
&& typeof arguments[index] === 'function') {
callback = arguments[index];
}
}

if (!callback) {
callback = function () {
};
}

handlers.push(callback);

return function ondone() {
var args = Array.prototype.slice.call(arguments)
, handlerIndex = handlers.indexOf(callback)
, index
, subData = data, err;

if (handlerIndex === -1) {
return;
}
handlers.splice(handlerIndex, 1);

for (index = 0; index < keys.length; index += 1) {
if (index === keys.length - 1) {
subData[keys[index]] = args[0];
} else if (!subData[keys[index]]) {
subData[keys[index]] = {};
}
subData = subData[keys[index]];
}

if (err) {
finished = true;
lastError = err;
if (self.listeners("finished").length) {
onfinishCalled = true;
self.emit("finished", err);
}
} else if (!finished) {
try {
callback.apply(callback, arguments);
} catch (callbackErr) {
finished = true;
lastError = callbackErr;
if (self.listeners("finished").length) {
onfinishCalled = true;
self.emit("finished", callbackErr);
}
}
if (handlers.length === 0 && !finished) {
finished = true;
if (self.listeners("finished").length) {
onfinishCalled = true;
self.emit("finished", lastError, data);
}
}
}
// Do nothing
};
};

this.onfinish = function (finish) {
self.on("finished", finish);

if (lastError) {
finish(lastError, null);
} else if (finished && !handlers.length) {
finish(null, data);
}
if (!registerCalled) {
finish(null, data);
}
};

if (finish) {
self.onfinish(finish);
}
};

core.Synchronizer.prototype = new core.EventEmitter();

}(crafity.core = crafity.core || {}));

}(window.crafity = window.crafity || {}, window.undefined));

0 comments on commit 23de1dc

Please sign in to comment.