1 | define(["./_base"], function(){ |
---|
2 | dojo.experimental("dojox.timing.Streamer"); |
---|
3 | dojox.timing.Streamer = function( |
---|
4 | /* function */input, |
---|
5 | /* function */output, |
---|
6 | /* int */interval, |
---|
7 | /* int */minimum, |
---|
8 | /* array */initialData |
---|
9 | ){ |
---|
10 | // summary |
---|
11 | // Streamer will take an input function that pushes N datapoints into a |
---|
12 | // queue, and will pass the next point in that queue out to an |
---|
13 | // output function at the passed interval; this way you can emulate |
---|
14 | // a constant buffered stream of data. |
---|
15 | // input: the function executed when the internal queue reaches minimumSize |
---|
16 | // output: the function executed on internal tick |
---|
17 | // interval: the interval in ms at which the output function is fired. |
---|
18 | // minimum: the minimum number of elements in the internal queue. |
---|
19 | |
---|
20 | var self = this; |
---|
21 | var queue = []; |
---|
22 | |
---|
23 | // public properties |
---|
24 | this.interval = interval || 1000; |
---|
25 | this.minimumSize = minimum || 10; // latency usually == interval * minimumSize |
---|
26 | this.inputFunction = input || function(q){ }; |
---|
27 | this.outputFunction = output || function(point){ }; |
---|
28 | |
---|
29 | // more setup |
---|
30 | var timer = new dojox.timing.Timer(this.interval); |
---|
31 | var tick = function(){ |
---|
32 | self.onTick(self); |
---|
33 | |
---|
34 | if(queue.length < self.minimumSize){ |
---|
35 | self.inputFunction(queue); |
---|
36 | } |
---|
37 | |
---|
38 | var obj = queue.shift(); |
---|
39 | while(typeof(obj) == "undefined" && queue.length > 0){ |
---|
40 | obj = queue.shift(); |
---|
41 | } |
---|
42 | |
---|
43 | // check to see if the input function needs to be fired |
---|
44 | // stop before firing the output function |
---|
45 | // TODO: relegate this to the output function? |
---|
46 | if(typeof(obj) == "undefined"){ |
---|
47 | self.stop(); |
---|
48 | return; |
---|
49 | } |
---|
50 | |
---|
51 | // call the output function. |
---|
52 | self.outputFunction(obj); |
---|
53 | }; |
---|
54 | |
---|
55 | this.setInterval = function(/* int */ms){ |
---|
56 | // summary |
---|
57 | // sets the interval in milliseconds of the internal timer |
---|
58 | this.interval = ms; |
---|
59 | timer.setInterval(ms); |
---|
60 | }; |
---|
61 | |
---|
62 | this.onTick = function(/* dojox.timing.Streamer */obj){ }; |
---|
63 | // wrap the timer functions so that we can connect to them if needed. |
---|
64 | this.start = function(){ |
---|
65 | // summary |
---|
66 | // starts the Streamer |
---|
67 | if(typeof(this.inputFunction) == "function" && typeof(this.outputFunction) == "function"){ |
---|
68 | timer.start(); |
---|
69 | return; |
---|
70 | } |
---|
71 | throw new Error("You cannot start a Streamer without an input and an output function."); |
---|
72 | }; |
---|
73 | this.onStart = function(){ }; |
---|
74 | this.stop = function(){ |
---|
75 | // summary |
---|
76 | // stops the Streamer |
---|
77 | timer.stop(); |
---|
78 | }; |
---|
79 | this.onStop = function(){ }; |
---|
80 | |
---|
81 | // finish initialization |
---|
82 | timer.onTick = this.tick; |
---|
83 | timer.onStart = this.onStart; |
---|
84 | timer.onStop = this.onStop; |
---|
85 | if(initialData){ |
---|
86 | queue.concat(initialData); |
---|
87 | } |
---|
88 | }; |
---|
89 | return dojox.timing.Streamer; |
---|
90 | }); |
---|