-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresultwrapper.js
executable file
·68 lines (63 loc) · 2.36 KB
/
resultwrapper.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
/**
* This class allow for easy handling of async calls
*/
var ResultWrapper = function(ux, timeout) {
this.cmdmap = {};
this.ux = ux;
this.timeout = timeout;
this.blockcount = 0;
this.blocktimer = null;
/**
* Handles the results generated by functions in MultiRemoteClient.
* If there is a mapping between ID and function, it's carried out,
* otherwise nothing happens.
*
* @param id The ID of the command which finished
* @param success true/false
* @param data Any resulting data (or null)
*/
this.handler = function(id, success, data) {
console.log("Wrap: Queued commands: " + JSON.stringify(this.cmdmap, null, 2));
if (id in this.cmdmap) {
state = this.cmdmap[id];
delete this.cmdmap[id];
console.log('Wrap: #' + id + ' Result: ' + success + ' (Extra data: ' + JSON.stringify(data, null, 2) + ')');
if (state.blocking) {
//console.log(state);
if (--this.blockcount == 0) {
clearTimeout(this.blocktimer);
this.ux.showBusyIndicator(false);
this.ux.blockUI(false);
} else if (this.blockcount < 0) {
console.log("ERROR: Blockcount is " + this.blockcount);
}
}
state.handler(success, data);
} else {
console.log("ERROR: No handler for command #" + id);
}
}
/**
* Takes any MultiRemoteCommand and maps a function to be called
* once the function returns. This is necessary since a lot of
* functions are async and will return before they're done.
* This simplifies the use of these functions.
*
* @param funcCommand Must return a multiremoteclient async id
* @param funcFollowUp The function which should be called when command finishes
* @param blockUI Wether or not to show the visual indicator when things take too long
*/
this.wrap = function(funcCommand, funcFollowUp, blockUI) {
console.log("Wrap: Wrapping function " + funcCommand);
id = funcCommand();
console.log("Wrap: #" + id + " \"" + funcCommand + "\"");
self2 = this;
this.cmdmap[id] = {handler: funcFollowUp, blocking: blockUI, timerid: 0};
if (blockUI) {
this.ux.blockUI(true);
if (this.blockcount++ == 0) {
this.blocktimer = setTimeout(function(){_id = id; console.log("Wrap: #" + _id + ' timeout'); if (_id in self2.cmdmap) self2.ux.showBusyIndicator(true); }, this.timeout);
}
}
}
}