-
Notifications
You must be signed in to change notification settings - Fork 8
/
poll.js
73 lines (71 loc) · 2.93 KB
/
poll.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
var Poll = {
"version": "0.3",
"start": function(config){
action = config.action;
config.internal_action = config.action;
config.action = function(){
Poll.util.attempts(config.name, config.internal_action);
};
if(config.start){
if(config.interval){
if(config.increment){
Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "value": setTimeout(function(){
Poll.util.timeout(config.name, config.action, config.interval);
}, config.start)};
} else {
Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "value": setTimeout(function(){
config.action();
Poll.timers[config.name]["value"] = setInterval(config.action, config.interval);
Poll.timers[config.name]["type"] = "interval";
}, config.start)};
}
} else {
Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "value": setTimeout(config.action, config.start)};
}
} else if(config.interval){
if(config.increment){
Poll.timers[config.name] = {"type": "interval", "config": config, "attempts": 0, "value": setTimeout(function(){
Poll.util.timeout(config.name, config.action, (config.interval + config.increment));
}, config.interval)};
} else {
Poll.timers[config.name] = {"type": "interval", "config": config, "attempts": 0, "value": setInterval(config.action, config.interval)};
}
} else {
throw "PollJS: You need to define a start, an interval, or both.";
}
},
"util": {
"attempts": function(name, fn){
var ret, instance = Poll.timers[name];
Poll.timers[name].attempts += 1;
ret = fn();
if(ret === false){
Poll.stop(name);
}
if(instance.config.attempts){
if(instance.attempts == instance.config.attempts){
Poll.stop(name);
instance.config.fallback();
}
}
},
"timeout": function(name, fn, start){
var time, config = Poll.timers[name].config;
time = (start + (config.increment || 0));
Poll.timers[name].value = setTimeout(function(){
Poll.util.timeout(config.name, fn, time);
}, time);
Poll.timers[name].type = "timeout";
fn();
}
},
"stop": function(name){
var instance = Poll.timers[name];
if(instance.type == "interval"){
clearInterval(instance.value);
} else {
clearTimeout(instance.value);
}
},
"timers":{}
};