forked from mtrpcic/polljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.js
104 lines (100 loc) · 3.52 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
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
var Poll = {
"version": "0.3",
"start": function(config){
if(typeof config === 'string')
{
if(Poll.exists(config, true))
{
config = Poll.timers[config].config;
}
else
{
throw "PollJS: The interval you are trying to re-activate does not exist.";
}
}
else
{
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, "active": true, "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, "active": true, "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, "active": true, "value": setTimeout(config.action, config.start)};
}
} else if(config.interval){
if(config.increment){
Poll.timers[config.name] = {"type": "interval", "config": config, "attempts": 0, "active": true, "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, "active": true, "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();
}
},
"exists": function(name, ignore_active_state){
if(Poll.timers[name] !== undefined && (ignore_active_state === true || Poll.timers[name].active === true))
{
return true;
}
else
{
return false;
}
},
"stop": function(name){
if(Poll.exists(name))
{
var instance = Poll.timers[name];
if(instance.type == "interval"){
clearInterval(instance.value);
instance.active = false;
} else {
clearTimeout(instance.value);
instance.active = false;
}
}
},
"timers":{}
};