Path: blob/master/webroot/rsrc/externals/javelin/lib/Router.js
12242 views
/**1* @provides javelin-router2* @requires javelin-install3* javelin-util4* @javelin5*/67/**8* Route requests. Primarily, this class provides a quality-of-service9* priority queue so large numbers of background loading tasks don't block10* interactive requests.11*/12JX.install('Router', {1314construct: function() {15this._queue = [];16},1718events: ['queue', 'start', 'done'],1920members: {21_queue: null,22_active: 0,23_limit: 5,2425queue: function(routable) {26this._queue.push(routable);2728this.invoke('queue', routable);29this._update();30},3132getRoutableByKey: function(key) {33for (var ii = 0; ii < this._queue.length; ii++) {34if (this._queue[ii].getKey() == key) {35return this._queue[ii];36}37}38return null;39},4041/**42* Start new requests if we have slots free for them.43*/44_update: function() {45var active = this._active;46var limit = this._limit;4748if (active >= limit) {49// If we're already at the request limit, we can't add any more50// requests.51return;52}5354// If we only have one free slot, we reserve it for a request with55// at least priority 1000.56var minimum;57if ((active + 1) == limit) {58minimum = 1000;59} else {60minimum = 0;61}6263var idx = this._getNextRoutable(minimum);64if (idx === null) {65return;66}6768var routable = this._queue[idx];69this._queue.splice(idx, 1);707172routable.listen('done', JX.bind(this, this._done, routable));7374this._active++;75routable.start();76this.invoke('start', routable);7778this._update();79},8081_done: function(routable) {82this._active--;83this.invoke('done', routable);8485this._update();86},8788_getNextRoutable: function(minimum) {89var best = (minimum - 1);9091var routable = null;92for (var ii = 0; ii < this._queue.length; ii++) {93var priority = this._queue[ii].getPriority();94if (priority > best) {95best = priority;96routable = ii;97}98}99100return routable;101}102103},104105statics: {106_instance: null,107getInstance: function() {108if (!JX.Router._instance) {109JX.Router._instance = new JX.Router();110}111return JX.Router._instance;112}113}114});115116117