f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass/**
f659f13d619daf68f78e85c84c5926f56bfe1439Dav GlassExtends IO to implement Queue for synchronous
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glasstransaction processing.
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass@module io
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass@submodule io-queue
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass@for IO
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass**/
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glassvar io = Y.io._map['io:0'] || new Y.IO();
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass
f659f13d619daf68f78e85c84c5926f56bfe1439Dav GlassY.mix(Y.IO.prototype, {
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass /**
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * Array of transactions queued for processing
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass *
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * @property _q
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * @private
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * @static
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * @type {Object}
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass */
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass _q: new Y.Queue(),
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass _qActiveId: null,
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass _qInit: false,
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass /**
038c85d1f8981a1d1479152b76a7c47ff219417dDav Glass * Property to determine whether the queue is set to
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * 1 (active) or 0 (inactive). When inactive, transactions
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * will be stored in the queue until the queue is set to active.
038c85d1f8981a1d1479152b76a7c47ff219417dDav Glass *
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * @property _qState
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * @private
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * @static
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass * @type {Number}
f659f13d619daf68f78e85c84c5926f56bfe1439Dav Glass */
_qState: 1,
/**
* Method Process the first transaction from the
* queue in FIFO order.
*
* @method _qShift
* @private
* @static
*/
_qShift: function() {
var io = this,
o = io._q.next();
io._qActiveId = o.id;
io._qState = 0;
io.send(o.uri, o.cfg, o.id);
},
/**
* Method for queueing a transaction before the request is sent to the
* resource, to ensure sequential processing.
*
* @method queue
* @static
* @return {Object}
*/
queue: function(uri, c) {
var io = this,
o = { uri: uri, cfg:c, id: this._id++ };
if(!io._qInit) {
Y.on('io:complete', function(id, o) { io._qNext(id); }, io);
io._qInit = true;
}
io._q.add(o);
if (io._qState === 1) {
io._qShift();
}
Y.log('Object queued. Transaction id is' + o.id, 'info', 'io');
return o;
},
_qNext: function(id) {
var io = this;
io._qState = 1;
if (io._qActiveId === id && io._q.size() > 0) {
io._qShift();
}
},
/**
* Method for promoting a transaction to the top of the queue.
*
* @method promote
* @static
*/
qPromote: function(o) {
this._q.promote(o);
},
/**
* Method for removing a specific, pending transaction from
* the queue.
*
* @method remove
* @private
* @static
*/
qRemove: function(o) {
this._q.remove(o);
},
qStart: function() {
var io = this;
io._qState = 1;
if (io._q.size() > 0) {
io._qShift();
}
Y.log('Queue started.', 'info', 'io');
},
/**
* Method for setting queue processing to inactive.
* Transaction requests to YUI.io.queue() will be stored in the queue, but
* not processed until the queue is reset to "active".
*
* @method _stop
* @private
* @static
*/
qStop: function() {
this._qState = 0;
Y.log('Queue stopped.', 'info', 'io');
},
/**
* Method to query the current size of the queue.
*
* @method _size
* @private
* @static
* @return {Number}
*/
qSize: function() {
return this._q.size();
}
}, true);
function _queue(u, c) {
return io.queue.apply(io, [u, c]);
}
_queue.start = function () { io.qStart(); };
_queue.stop = function () { io.qStop(); };
_queue.promote = function (o) { io.qPromote(o); };
_queue.remove = function (o) { io.qRemove(o); };
_queue.size = function () { io.qSize(); };
Y.io.queue = _queue;