1 if(!dojo._hasResource["dojox.timing.Streamer"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.timing.Streamer"] = true;
3 dojo.provide("dojox.timing.Streamer");
5 dojo.require("dojox.timing._base");
7 dojox.timing.Streamer = function(
12 /* array */initialData
15 // Streamer will take an input function that pushes N datapoints into a
16 // queue, and will pass the next point in that queue out to an
17 // output function at the passed interval; this way you can emulate
18 // a constant buffered stream of data.
19 // input: the function executed when the internal queue reaches minimumSize
20 // output: the function executed on internal tick
21 // interval: the interval in ms at which the output function is fired.
22 // minimum: the minimum number of elements in the internal queue.
28 this.interval = interval || 1000;
29 this.minimumSize = minimum || 10; // latency usually == interval * minimumSize
30 this.inputFunction = input || function(q){ };
31 this.outputFunction = output || function(point){ };
34 var timer = new dojox.timing.Timer(this.interval);
35 var tick = function(){
38 if(queue.length < self.minimumSize){
39 self.inputFunction(queue);
42 var obj = queue.shift();
43 while(typeof(obj) == "undefined" && queue.length > 0){
47 // check to see if the input function needs to be fired
48 // stop before firing the output function
49 // TODO: relegate this to the output function?
50 if(typeof(obj) == "undefined"){
55 // call the output function.
56 self.outputFunction(obj);
59 this.setInterval = function(/* int */ms){
61 // sets the interval in milliseconds of the internal timer
63 timer.setInterval(ms);
66 this.onTick = function(/* dojox.timing.Streamer */obj){ };
67 // wrap the timer functions so that we can connect to them if needed.
68 this.start = function(){
70 // starts the Streamer
71 if(typeof(this.inputFunction) == "function" && typeof(this.outputFunction) == "function"){
75 throw new Error("You cannot start a Streamer without an input and an output function.");
77 this.onStart = function(){ };
78 this.stop = function(){
83 this.onStop = function(){ };
85 // finish initialization
86 timer.onTick = this.tick;
87 timer.onStart = this.onStart;
88 timer.onStop = this.onStop;
90 queue.concat(initialData);