-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterval.js
More file actions
58 lines (47 loc) · 1.35 KB
/
interval.js
File metadata and controls
58 lines (47 loc) · 1.35 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
58
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
(["./EventSource"], function(EventSource){
"use strict";
var Value = EventSource.Value;
function Interval(ms, suspended){
EventSource.call(this);
this.ms = ms;
if(!suspended){
startTicks(this);
}
}
Interval.prototype = Object.create(EventSource.prototype);
Interval.prototype.suspend = function suspend(){
if(this.handle){
clearInterval(this.handle);
delete this.handle;
}
};
Interval.prototype.resume = function resume(){
if(!this.handle){
startTicks(this);
}
};
Interval.prototype.isSuspended = function isSuspended(){
return !this.handle;
};
Interval.prototype.getValue = function getValue(){
return this.lastValue && this.lastValue.x;
};
Interval.prototype.isValueAvailable = function isValueAvailable(){
return !!this.lastValue;
};
Interval.prototype.release = function release(){
EventSource.prototype.release.call(this);
this.suspend();
};
function interval(ms, suspended){
return new Interval(ms, suspended);
}
interval.Interval = Interval;
return interval;
function startTicks(stream){
stream.handle = setInterval(function(){
stream.micro.send(stream.lastValue = new Value(new Date().getTime()));
}, ms);
}
});