2150 |
mathias |
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");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.timing._base");
|
|
|
6 |
|
|
|
7 |
dojox.timing.Streamer = function(
|
|
|
8 |
/* function */input,
|
|
|
9 |
/* function */output,
|
|
|
10 |
/* int */interval,
|
|
|
11 |
/* int */minimum,
|
|
|
12 |
/* array */initialData
|
|
|
13 |
){
|
|
|
14 |
// summary
|
|
|
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.
|
|
|
23 |
|
|
|
24 |
var self = this;
|
|
|
25 |
var queue = [];
|
|
|
26 |
|
|
|
27 |
// public properties
|
|
|
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){ };
|
|
|
32 |
|
|
|
33 |
// more setup
|
|
|
34 |
var timer = new dojox.timing.Timer(this.interval);
|
|
|
35 |
var tick = function(){
|
|
|
36 |
self.onTick(self);
|
|
|
37 |
|
|
|
38 |
if(queue.length < self.minimumSize){
|
|
|
39 |
self.inputFunction(queue);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
var obj = queue.shift();
|
|
|
43 |
while(typeof(obj) == "undefined" && queue.length > 0){
|
|
|
44 |
obj = queue.shift();
|
|
|
45 |
}
|
|
|
46 |
|
|
|
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"){
|
|
|
51 |
self.stop();
|
|
|
52 |
return;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// call the output function.
|
|
|
56 |
self.outputFunction(obj);
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
this.setInterval = function(/* int */ms){
|
|
|
60 |
// summary
|
|
|
61 |
// sets the interval in milliseconds of the internal timer
|
|
|
62 |
this.interval = ms;
|
|
|
63 |
timer.setInterval(ms);
|
|
|
64 |
};
|
|
|
65 |
|
|
|
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(){
|
|
|
69 |
// summary
|
|
|
70 |
// starts the Streamer
|
|
|
71 |
if(typeof(this.inputFunction) == "function" && typeof(this.outputFunction) == "function"){
|
|
|
72 |
timer.start();
|
|
|
73 |
return;
|
|
|
74 |
}
|
|
|
75 |
throw new Error("You cannot start a Streamer without an input and an output function.");
|
|
|
76 |
};
|
|
|
77 |
this.onStart = function(){ };
|
|
|
78 |
this.stop = function(){
|
|
|
79 |
// summary
|
|
|
80 |
// stops the Streamer
|
|
|
81 |
timer.stop();
|
|
|
82 |
};
|
|
|
83 |
this.onStop = function(){ };
|
|
|
84 |
|
|
|
85 |
// finish initialization
|
|
|
86 |
timer.onTick = this.tick;
|
|
|
87 |
timer.onStart = this.onStart;
|
|
|
88 |
timer.onStop = this.onStop;
|
|
|
89 |
if(initialData){
|
|
|
90 |
queue.concat(initialData);
|
|
|
91 |
}
|
|
|
92 |
};
|
|
|
93 |
|
|
|
94 |
}
|