Streamer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.timing.Streamer"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.timing.Streamer"] = true;
  8. dojo.provide("dojox.timing.Streamer");
  9. dojo.require("dojox.timing._base");
  10. dojox.timing.Streamer = function(
  11. /* function */input,
  12. /* function */output,
  13. /* int */interval,
  14. /* int */minimum,
  15. /* array */initialData
  16. ){
  17. // summary
  18. // Streamer will take an input function that pushes N datapoints into a
  19. // queue, and will pass the next point in that queue out to an
  20. // output function at the passed interval; this way you can emulate
  21. // a constant buffered stream of data.
  22. // input: the function executed when the internal queue reaches minimumSize
  23. // output: the function executed on internal tick
  24. // interval: the interval in ms at which the output function is fired.
  25. // minimum: the minimum number of elements in the internal queue.
  26. var self = this;
  27. var queue = [];
  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. // more setup
  34. var timer = new dojox.timing.Timer(this.interval);
  35. var tick = function(){
  36. self.onTick(self);
  37. if(queue.length < self.minimumSize){
  38. self.inputFunction(queue);
  39. }
  40. var obj = queue.shift();
  41. while(typeof(obj) == "undefined" && queue.length > 0){
  42. obj = queue.shift();
  43. }
  44. // check to see if the input function needs to be fired
  45. // stop before firing the output function
  46. // TODO: relegate this to the output function?
  47. if(typeof(obj) == "undefined"){
  48. self.stop();
  49. return;
  50. }
  51. // call the output function.
  52. self.outputFunction(obj);
  53. };
  54. this.setInterval = function(/* int */ms){
  55. // summary
  56. // sets the interval in milliseconds of the internal timer
  57. this.interval = ms;
  58. timer.setInterval(ms);
  59. };
  60. this.onTick = function(/* dojox.timing.Streamer */obj){ };
  61. // wrap the timer functions so that we can connect to them if needed.
  62. this.start = function(){
  63. // summary
  64. // starts the Streamer
  65. if(typeof(this.inputFunction) == "function" && typeof(this.outputFunction) == "function"){
  66. timer.start();
  67. return;
  68. }
  69. throw new Error("You cannot start a Streamer without an input and an output function.");
  70. };
  71. this.onStart = function(){ };
  72. this.stop = function(){
  73. // summary
  74. // stops the Streamer
  75. timer.stop();
  76. };
  77. this.onStop = function(){ };
  78. // finish initialization
  79. timer.onTick = this.tick;
  80. timer.onStart = this.onStart;
  81. timer.onStop = this.onStop;
  82. if(initialData){
  83. queue.concat(initialData);
  84. }
  85. };
  86. }