-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsesCatcher.js
51 lines (43 loc) · 1.17 KB
/
responsesCatcher.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
var ResponsesCatcher = function(cb) {
this.queries = [];
this.cb = cb;
};
ResponsesCatcher.prototype.debug = function() {
var arr = [];
for (var i = 0; i < this.queries.length; i ++) {
var query = this.queries[i];
arr[i] = query.response == undefined
? undefined
:(query.response.isSuccessful() ? true : false);
}
console.log(JSON.stringify(arr));
};
ResponsesCatcher.prototype.isFinished = function() {
for (var i = 0; i < this.queries.length; i ++) {
if (this.queries[i].response == undefined) return false;
}
return true;
};
ResponsesCatcher.prototype.isSuccessful = function() {
for (var i = 0; i < this.queries.length; i ++) {
var query = this.queries[i];
if (query.response && !query.response.isSuccessful()) return false;
}
return true;
};
ResponsesCatcher.prototype.setResponse = function(query, response) {
query.response = response;
if (this.cb) this.cb(this, query.data, response);
};
ResponsesCatcher.prototype.catcher = function(data) {
var self = this;
var query = {
"data": data,
"response": undefined
};
this.queries.push(query);
return function(response) {
self.setResponse(query, response);
}
};
module.exports = ResponsesCatcher;