-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpoller.js
More file actions
57 lines (54 loc) · 1.64 KB
/
poller.js
File metadata and controls
57 lines (54 loc) · 1.64 KB
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
function Poller(method) {
if (typeof method !== "function") {
throw new TypeError("Constructor must be passed a function!");
}
this.queryMethod = method;
this.storage = null;
this.interval = null;
}
Poller.prototype.queryThis = function() {
return this.queryMethod();
};
Poller.prototype.queryAndStore = function() {
this.storage = this.queryMethod();
return this.storage;
};
Poller.prototype.start = function(timeInterval) {
if (timeInterval <= 0) {
throw new RangeError("time passed to start method must be physically possible");
}
this.interval = setInterval(this.queryMethod, timeInterval);
return this.interval;
};
Poller.prototype.stop = function() {
clearInterval(this.interval);
};
Poller.prototype.startAndStore = function(timeInterval) {
if (timeInterval <= 0) {
throw new RangeError("time passed to start method must be physically possible");
}
this.interval = setInterval(function() {
this.storage = this.queryMethod();
}, timeInterval);
return this.interval;
};
Poller.prototype.retrieveStorage = function() {
return this.storage;
};
//if method that needs to be called is async
//use this instead of Poller.prototype.startAndStore
//make that method have 1 argument that's a callback and calls it w/ the data to be stored as an argument
Poller.prototype.startAsyncStore = function(timeInterval) {
if (timeInterval < 0) {
throw new RangeError("time passed to start method must be physically possible");
}
this.interval = setInterval(function() {
this.queryMethod(function(data) {
this.storage = data;
});
}, timeInterval);
return this.interval;
};
module.exports = {
Poller
};