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