Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datasource-polling.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('datasource-polling', function(Y) {
  9. /**
  10. * Extends DataSource with polling functionality.
  11. *
  12. * @module datasource
  13. * @submodule datasource-polling
  14. */
  15. /**
  16. * Adds polling to the DataSource Utility.
  17. * @class Pollable
  18. * @extends DataSource.Local
  19. */
  20. function Pollable() {
  21. this._intervals = {};
  22. }
  23. Pollable.prototype = {
  24. /**
  25. * @property _intervals
  26. * @description Hash of polling interval IDs that have been enabled,
  27. * stored here to be able to clear all intervals.
  28. * @private
  29. */
  30. _intervals: null,
  31. /**
  32. * Sets up a polling mechanism to send requests at set intervals and
  33. * forward responses to given callback.
  34. *
  35. * @method setInterval
  36. * @param msec {Number} Length of interval in milliseconds.
  37. * @param request {Object} An object literal with the following properties:
  38. * <dl>
  39. * <dt><code>request</code></dt>
  40. * <dd>The request to send to the live data source, if any.</dd>
  41. * <dt><code>callback</code></dt>
  42. * <dd>An object literal with the following properties:
  43. * <dl>
  44. * <dt><code>success</code></dt>
  45. * <dd>The function to call when the data is ready.</dd>
  46. * <dt><code>failure</code></dt>
  47. * <dd>The function to call upon a response failure condition.</dd>
  48. * <dt><code>argument</code></dt>
  49. * <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
  50. * </dl>
  51. * </dd>
  52. * <dt><code>cfg</code></dt>
  53. * <dd>Configuration object, if any.</dd>
  54. * </dl>
  55. * @return {Number} Interval ID.
  56. */
  57. setInterval: function(msec, request) {
  58. var x = Y.later(msec, this, this.sendRequest, [ request ], true);
  59. this._intervals[x.id] = x;
  60. // First call happens immediately, but async
  61. Y.later(0, this, this.sendRequest, [request]);
  62. return x.id;
  63. },
  64. /**
  65. * Disables polling mechanism associated with the given interval ID.
  66. *
  67. * @method clearInterval
  68. * @param id {Number} Interval ID.
  69. */
  70. clearInterval: function(id, key) {
  71. // In case of being called by clearAllIntervals()
  72. id = key || id;
  73. if(this._intervals[id]) {
  74. // Clear the interval
  75. this._intervals[id].cancel();
  76. // Clear from tracker
  77. delete this._intervals[id];
  78. }
  79. },
  80. /**
  81. * Clears all intervals.
  82. *
  83. * @method clearAllIntervals
  84. */
  85. clearAllIntervals: function() {
  86. Y.each(this._intervals, this.clearInterval, this);
  87. }
  88. };
  89. Y.augment(Y.DataSource.Local, Pollable);
  90. }, '3.4.0' ,{requires:['datasource-local']});