mirror of
https://github.com/murdos/musicbrainz-userscripts
synced 2025-03-17 15:06:57 +00:00
Merge handlers in ajax request context, to prevent duplicated requests
to MB webservice.
This commit is contained in:
parent
cd8d7c6d98
commit
2b54c81745
1 changed files with 25 additions and 23 deletions
|
@ -25,16 +25,17 @@ var MBLinks = function (cachekey, expiration) {
|
|||
}();
|
||||
|
||||
this.ajax_requests = {
|
||||
// properties: "key": {handlers: [], next: property}
|
||||
// properties: "key": {handler: function, next: property, context: {}}
|
||||
first: "",
|
||||
last: "",
|
||||
empty: function() {return this.first == "";},
|
||||
push: function(key, handler) {
|
||||
push: function(key, handler, context) {
|
||||
if (key in this) {
|
||||
this[key].handlers.push(handler);
|
||||
this[key].handler = handler;
|
||||
this[key].context = context;
|
||||
}
|
||||
else {
|
||||
this[key] = {handlers: [handler], next: ""};
|
||||
this[key] = {handler: handler, next: "", context: context};
|
||||
if (this.first == "") {
|
||||
this.first = this.last = key;
|
||||
}
|
||||
|
@ -45,14 +46,13 @@ var MBLinks = function (cachekey, expiration) {
|
|||
}
|
||||
},
|
||||
shift: function() {
|
||||
if (this.empty()) { return; }
|
||||
var key = this.first;
|
||||
var handlers = this[key].handlers;
|
||||
var handler = this[key].handler;
|
||||
var context = this[key].context;
|
||||
this.first = this[key].next;
|
||||
delete this[key]; // delete this property
|
||||
return handlers;
|
||||
},
|
||||
size: function() {
|
||||
return Object.keys(this).length;
|
||||
return $.proxy(handler, context);
|
||||
}
|
||||
};
|
||||
this.cache = {};
|
||||
|
@ -69,16 +69,10 @@ var MBLinks = function (cachekey, expiration) {
|
|||
this.initAjaxEngine = function () {
|
||||
var ajax_requests = this.ajax_requests;
|
||||
setInterval(function () {
|
||||
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();
|
||||
}
|
||||
})
|
||||
if (!ajax_requests.empty()) {
|
||||
var request = ajax_requests.shift();
|
||||
if (typeof request === "function") {
|
||||
request();
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
|
@ -136,7 +130,13 @@ var MBLinks = function (cachekey, expiration) {
|
|||
insert_func(mblinks.createMusicBrainzLink(mb_url, _type));
|
||||
});
|
||||
} else {
|
||||
mblinks.ajax_requests.push(url, $.proxy(function () {
|
||||
// Merge with previous context if there's already a pending ajax request
|
||||
var handlers = [];
|
||||
if (url in mblinks.ajax_requests) {
|
||||
handlers = mblinks.ajax_requests[url].context.handlers;
|
||||
}
|
||||
handlers.push(insert_func);
|
||||
mblinks.ajax_requests.push(url, function () {
|
||||
var context = this;
|
||||
$.getJSON(mblinks.mb_server + '/ws/2/url?resource=' + encodeURIComponent(context.url)
|
||||
+ '&inc=' + context.mb_type + '-rels',
|
||||
|
@ -152,7 +152,9 @@ var MBLinks = function (cachekey, expiration) {
|
|||
var mb_url = mblinks.mb_server + '/' + context.mb_type + '/' + relation[_type]['id'];
|
||||
if ($.inArray(mb_url, mblinks.cache[context.url].urls) == -1) { // prevent dupes
|
||||
mblinks.cache[context.url].urls.push(mb_url);
|
||||
context.insert_func(mblinks.createMusicBrainzLink(mb_url, _type));
|
||||
$.each(context.handlers, function(i, handler) {
|
||||
handler(mblinks.createMusicBrainzLink(mb_url, _type))
|
||||
})
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -161,9 +163,9 @@ var MBLinks = function (cachekey, expiration) {
|
|||
});
|
||||
}, {
|
||||
'url': url,
|
||||
'insert_func': insert_func,
|
||||
'handlers': handlers,
|
||||
'mb_type': mb_type
|
||||
}));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue