API Docs for: 3.17.2
Show:

File: io/js/io-queue.js

  1. /**
  2. Extends IO to implement Queue for synchronous
  3. transaction processing.
  4. @module io
  5. @submodule io-queue
  6. @for IO
  7. **/
  8. var io = Y.io._map['io:0'] || new Y.IO();
  9.  
  10. Y.mix(Y.IO.prototype, {
  11. /**
  12. * Array of transactions queued for processing
  13. *
  14. * @property _q
  15. * @private
  16. * @static
  17. * @type {Object}
  18. */
  19. _q: new Y.Queue(),
  20. _qActiveId: null,
  21. _qInit: false,
  22.  
  23. /**
  24. * Property to determine whether the queue is set to
  25. * 1 (active) or 0 (inactive). When inactive, transactions
  26. * will be stored in the queue until the queue is set to active.
  27. *
  28. * @property _qState
  29. * @private
  30. * @static
  31. * @type {Number}
  32. */
  33. _qState: 1,
  34.  
  35. /**
  36. * Method Process the first transaction from the
  37. * queue in FIFO order.
  38. *
  39. * @method _qShift
  40. * @private
  41. * @static
  42. */
  43. _qShift: function() {
  44. var io = this,
  45. o = io._q.next();
  46.  
  47. io._qActiveId = o.id;
  48. io._qState = 0;
  49. io.send(o.uri, o.cfg, o.id);
  50. },
  51.  
  52. /**
  53. * Method for queueing a transaction before the request is sent to the
  54. * resource, to ensure sequential processing.
  55. *
  56. * @method queue
  57. * @static
  58. * @return {Object}
  59. */
  60. queue: function(uri, c) {
  61. var io = this,
  62. o = { uri: uri, cfg:c, id: this._id++ };
  63.  
  64. if(!io._qInit) {
  65. Y.on('io:complete', function(id, o) { io._qNext(id); }, io);
  66. io._qInit = true;
  67. }
  68.  
  69. io._q.add(o);
  70. if (io._qState === 1) {
  71. io._qShift();
  72. }
  73.  
  74. Y.log('Object queued. Transaction id is' + o.id, 'info', 'io');
  75. return o;
  76. },
  77.  
  78. _qNext: function(id) {
  79. var io = this;
  80. io._qState = 1;
  81. if (io._qActiveId === id && io._q.size() > 0) {
  82. io._qShift();
  83. }
  84. },
  85.  
  86. /**
  87. * Method for promoting a transaction to the top of the queue.
  88. *
  89. * @method promote
  90. * @static
  91. */
  92. qPromote: function(o) {
  93. this._q.promote(o);
  94. },
  95.  
  96. /**
  97. * Method for removing a specific, pending transaction from
  98. * the queue.
  99. *
  100. * @method remove
  101. * @private
  102. * @static
  103. */
  104. qRemove: function(o) {
  105. this._q.remove(o);
  106. },
  107.  
  108. /**
  109. * Method for cancel all pending transaction from
  110. * the queue.
  111. *
  112. * @method empty
  113. * @static
  114. * @since 3.7.3
  115. */
  116. qEmpty: function() {
  117. this._q = new Y.Queue();
  118. },
  119.  
  120. qStart: function() {
  121. var io = this;
  122. io._qState = 1;
  123.  
  124. if (io._q.size() > 0) {
  125. io._qShift();
  126. }
  127. Y.log('Queue started.', 'info', 'io');
  128. },
  129.  
  130. /**
  131. * Method for setting queue processing to inactive.
  132. * Transaction requests to YUI.io.queue() will be stored in the queue, but
  133. * not processed until the queue is reset to "active".
  134. *
  135. * @method _stop
  136. * @private
  137. * @static
  138. */
  139. qStop: function() {
  140. this._qState = 0;
  141. Y.log('Queue stopped.', 'info', 'io');
  142. },
  143.  
  144. /**
  145. * Method to query the current size of the queue.
  146. *
  147. * @method _size
  148. * @private
  149. * @static
  150. * @return {Number}
  151. */
  152. qSize: function() {
  153. return this._q.size();
  154. }
  155.  
  156. }, true);
  157.  
  158. function _queue(u, c) {
  159. return io.queue.apply(io, [u, c]);
  160. }
  161.  
  162. _queue.start = function () { io.qStart(); };
  163. _queue.stop = function () { io.qStop(); };
  164. _queue.promote = function (o) { io.qPromote(o); };
  165. _queue.remove = function (o) { io.qRemove(o); };
  166. _queue.size = function () { io.qSize(); };
  167. _queue.empty = function () { io.qEmpty(); };
  168. Y.io.queue = _queue;
  169.