| 1 |
define(["./_base"], function(){ |
| 2 |
dojo.experimental("dojox.timing.Streamer"); |
| 3 |
|
| 4 |
return dojox.timing.Streamer = function( |
| 5 |
input, |
| 6 |
output, |
| 7 |
interval, |
| 8 |
minimum, |
| 9 |
initialData |
| 10 |
){ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
var self = this; |
| 26 |
var queue = []; |
| 27 |
|
| 28 |
|
| 29 |
this.interval = interval || 1000; |
| 30 |
this.minimumSize = minimum || 10; |
| 31 |
this.inputFunction = input || function(q){ }; |
| 32 |
this.outputFunction = output || function(point){ }; |
| 33 |
|
| 34 |
|
| 35 |
var timer = new dojox.timing.Timer(this.interval); |
| 36 |
var tick = function(){ |
| 37 |
self.onTick(self); |
| 38 |
|
| 39 |
if(queue.length < self.minimumSize){ |
| 40 |
self.inputFunction(queue); |
| 41 |
} |
| 42 |
|
| 43 |
var obj = queue.shift(); |
| 44 |
while(typeof(obj) == "undefined" && queue.length > 0){ |
| 45 |
obj = queue.shift(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
if(typeof(obj) == "undefined"){ |
| 52 |
self.stop(); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
self.outputFunction(obj); |
| 58 |
}; |
| 59 |
|
| 60 |
this.setInterval = function(ms){ |
| 61 |
|
| 62 |
|
| 63 |
this.interval = ms; |
| 64 |
timer.setInterval(ms); |
| 65 |
}; |
| 66 |
|
| 67 |
this.onTick = function(obj){ }; |
| 68 |
|
| 69 |
this.start = function(){ |
| 70 |
|
| 71 |
|
| 72 |
if(typeof(this.inputFunction) == "function" && typeof(this.outputFunction) == "function"){ |
| 73 |
timer.start(); |
| 74 |
return; |
| 75 |
} |
| 76 |
throw new Error("You cannot start a Streamer without an input and an output function."); |
| 77 |
}; |
| 78 |
this.onStart = function(){ }; |
| 79 |
this.stop = function(){ |
| 80 |
|
| 81 |
|
| 82 |
timer.stop(); |
| 83 |
}; |
| 84 |
this.onStop = function(){ }; |
| 85 |
|
| 86 |
|
| 87 |
timer.onTick = this.tick; |
| 88 |
timer.onStart = this.onStart; |
| 89 |
timer.onStop = this.onStop; |
| 90 |
if(initialData){ |
| 91 |
queue.concat(initialData); |
| 92 |
} |
| 93 |
}; |
| 94 |
}); |