-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.Synchronizer.js
executable file
·133 lines (112 loc) · 3.03 KB
/
crafity.Synchronizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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));