Streamer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. define("dojox/timing/Streamer", ["./_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. var self = this;
  20. var queue = [];
  21. // public properties
  22. this.interval = interval || 1000;
  23. this.minimumSize = minimum || 10; // latency usually == interval * minimumSize
  24. this.inputFunction = input || function(q){ };
  25. this.outputFunction = output || function(point){ };
  26. // more setup
  27. var timer = new dojox.timing.Timer(this.interval);
  28. var tick = function(){
  29. self.onTick(self);
  30. if(queue.length < self.minimumSize){
  31. self.inputFunction(queue);
  32. }
  33. var obj = queue.shift();
  34. while(typeof(obj) == "undefined" && queue.length > 0){
  35. obj = queue.shift();
  36. }
  37. // check to see if the input function needs to be fired
  38. // stop before firing the output function
  39. // TODO: relegate this to the output function?
  40. if(typeof(obj) == "undefined"){
  41. self.stop();
  42. return;
  43. }
  44. // call the output function.
  45. self.outputFunction(obj);
  46. };
  47. this.setInterval = function(/* int */ms){
  48. // summary
  49. // sets the interval in milliseconds of the internal timer
  50. this.interval = ms;
  51. timer.setInterval(ms);
  52. };
  53. this.onTick = function(/* dojox.timing.Streamer */obj){ };
  54. // wrap the timer functions so that we can connect to them if needed.
  55. this.start = function(){
  56. // summary
  57. // starts the Streamer
  58. if(typeof(this.inputFunction) == "function" && typeof(this.outputFunction) == "function"){
  59. timer.start();
  60. return;
  61. }
  62. throw new Error("You cannot start a Streamer without an input and an output function.");
  63. };
  64. this.onStart = function(){ };
  65. this.stop = function(){
  66. // summary
  67. // stops the Streamer
  68. timer.stop();
  69. };
  70. this.onStop = function(){ };
  71. // finish initialization
  72. timer.onTick = this.tick;
  73. timer.onStart = this.onStart;
  74. timer.onStop = this.onStop;
  75. if(initialData){
  76. queue.concat(initialData);
  77. }
  78. };
  79. return dojox.timing.Streamer;
  80. });