"use strict";
( function ( NS ) {
var QUEUE = 1,
IGNORE = 2,
ABORT = 3;
var IOQueue = NS.Class({
Extends: NS.Object,
Private Property
O.IOQueue#_queue
- Array
- private
Queue of request objects waiting for current transactions to finish.
Property
O.IOQueue#_recent
- (O.HttpRequest|null)
A reference to the most recent request.
Property
O.IOQueue#activeConnections
- Number
The number of active connections
Property
O.IOQueue#link
- Number
The property is used to determine what to do if a request is made and there are already the maximum allowed number of connections. Accepted values are the constants IOQueue.QUEUE, IOQueue.IGNORE and IOQueue.ABORT.
- QUEUE: adds the request to a queue and then waits for the next active connection to finish before dispatching the oldest waiting request and so on until the queue is empty.
- IGNORE: ignores the request if there are no free connections.
- ABORT: aborts the most recent active request and immediately dispatches the new request.
link: QUEUE,
Property
O.IOQueue#maxConnections
- Number
The maximum number of concurrent connections to make with this IOQueue object. Note, this is a per-instance value; each IOQueue instance may make up to maxConnections to the server as defined on that object.
maxConnections: 1,
Constructor
O.IOQueue( mixin )
Parameters
mixin | Object An object containing new defaults for any of the public properties defined on the object. Can also contain methods to override the normal methods to create an anonymous subclass. |
---|
init: function ( mixin ) {
this._queue = [];
this._recent = null;
this.activeConnections = 0;
IOQueue.parent.init.call( this, mixin );
},
Method
O.IOQueue#send( request )
If the number of active requests is equal to the maximum allowed number of concurrent connections, the request will be queued, ignored or cause the most recent active request to abort as specified in the O.IOQueue#link property.
Parameters
request | O.HttpRequest |
---|
Returns
O.IOQueue Returns self.
send: function ( request ) {
if ( this.get( 'activeConnections' ) >= this.get( 'maxConnections' ) ) {
switch ( this.get( 'link' ) ) {
case QUEUE:
this._queue.push( request );
/* falls through */
case IGNORE:
return this;
case ABORT:
this._recent.abort();
break;
default:
throw new Error( 'Invalid O.IOQueue link type.' );
}
}
this.increment( 'activeConnections', 1 );
// If already set, presume it will bubble to us
if ( !request.get( 'nextEventTarget' ) ) {
request.set( 'nextEventTarget', this );
}
// Store reference in case we need to abort a request.
this._recent = request.send();
return this;
},
Method
O.IOQueue#abort( request )
Abort the request if it is currently running, or remove it from the waiting queue if it has not yet run.
Parameters
request | O.HttpRequest |
---|
Returns
O.IOQueue Returns self.
abort: function ( request ) {
this._queue.erase( request );
request.abort();
return this;
},
Private Method
O.IOQueue#_complete( transport )
Cleans up any state set by the IOQueue methods on the Transport object and starts the next request in the queue, if any.
Parameters
transport | Transport The transport object. |
---|
_complete: function ( event ) {
var request = event.target;
if ( this._recent === request ) {
this._recent = null;
}
if ( request.get( 'nextEventTarget' ) === this ) {
request.set( 'nextEventTarget', null );
}
this.increment( 'activeConnections', -1 );
if ( this._queue.length ) {
this.send( this._queue.shift() );
}
}.on( 'io:end' )
});
IOQueue.QUEUE = 1;
IOQueue.IGNORE = 2;
IOQueue.ABORT = 3;
NS.IOQueue = IOQueue;
}( O ) );