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