Changed ajax requests queue implementation from a simple array to a

linked map.
The goal is to used the map feature to prevent making duplicated
requests.
This commit is contained in:
Aurélien Mino 2015-06-08 22:44:39 +02:00
parent 439201356f
commit cd8d7c6d98

View file

@ -24,7 +24,37 @@ var MBLinks = function (cachekey, expiration) {
}
}();
this.ajax_requests = [];
this.ajax_requests = {
// properties: "key": {handlers: [], next: property}
first: "",
last: "",
empty: function() {return this.first == "";},
push: function(key, handler) {
if (key in this) {
this[key].handlers.push(handler);
}
else {
this[key] = {handlers: [handler], next: ""};
if (this.first == "") {
this.first = this.last = key;
}
else {
this[this.last].next = key;
this.last = key;
}
}
},
shift: function() {
var key = this.first;
var handlers = this[key].handlers;
this.first = this[key].next;
delete this[key]; // delete this property
return handlers;
},
size: function() {
return Object.keys(this).length;
}
};
this.cache = {};
this.expirationMinutes = parseInt(expiration);
this.cache_key = cachekey;
@ -39,10 +69,16 @@ var MBLinks = function (cachekey, expiration) {
this.initAjaxEngine = function () {
var ajax_requests = this.ajax_requests;
setInterval(function () {
if (ajax_requests.length > 0) {
var request = ajax_requests.shift();
if (typeof request === "function") {
request();
if (ajax_requests.size() > 0) {
var requests = ajax_requests.shift();
if (typeof requests === "function") {
requests();
} else if (requests instanceof Array) {
$.each(requests, function(i, request) {
if (typeof request === "function") {
request();
}
})
}
}
}, 1000);
@ -100,7 +136,7 @@ var MBLinks = function (cachekey, expiration) {
insert_func(mblinks.createMusicBrainzLink(mb_url, _type));
});
} else {
mblinks.ajax_requests.push($.proxy(function () {
mblinks.ajax_requests.push(url, $.proxy(function () {
var context = this;
$.getJSON(mblinks.mb_server + '/ws/2/url?resource=' + encodeURIComponent(context.url)
+ '&inc=' + context.mb_type + '-rels',