Use .prototype for RequestManager

This commit is contained in:
Michael Wiencek 2014-12-28 23:30:39 -06:00
parent 9818accd04
commit eb66785385

View file

@ -690,16 +690,7 @@ function batch_recording_rels() {
var artist_mbid = window.location.href.match(MBID_REGEX)[0],
artist_name = $("h1 a").text(),
$artist_works_msg = $("<td></td>"),
ws_requests = new RequestManager(1000, 1),
edit_requests = new RequestManager(1500, 2);
var current_reqs = 0;
setInterval(function () {
if (current_reqs > 0) {
current_reqs -= 1;
}
}, 1000);
$artist_works_msg = $("<td></td>");
// Load performance relationships
@ -1619,13 +1610,23 @@ function batch_recording_rels() {
return $row.children('td:has(' + TITLE_SELECTOR + ')');
}
// Request rate limiting
var REQUEST_COUNT = 0;
setInterval(function () {
if (REQUEST_COUNT > 0) {
REQUEST_COUNT -= 1;
}
}, 1000);
function RequestManager(rate, count) {
this.queue = [];
this.last = 0;
this.active = false;
this.stopped = false;
}
this.next = function () {
RequestManager.prototype.next = function () {
if (this.stopped || !this.queue.length) {
this.active = false;
return;
@ -1633,30 +1634,32 @@ function batch_recording_rels() {
this.queue.shift()();
this.last = new Date().getTime();
current_reqs += count;
if (current_reqs >= 10) {
var diff = current_reqs - 9, timeout = diff * 1000;
REQUEST_COUNT += count;
if (REQUEST_COUNT >= 10) {
var diff = REQUEST_COUNT - 9;
var timeout = diff * 1000;
setTimeout(function (foo) { foo.next() }, rate + timeout, this);
} else {
setTimeout(function (foo) { foo.next() }, rate, this);
}
}
};
this.push = function (req) {
RequestManager.prototype.push = function (req) {
this.queue.push(req);
if (!(this.active || this.stopped)) {
this.start_queue();
}
}
};
this.unshift = function (req) {
RequestManager.prototype.unshift = function (req) {
this.queue.unshift(req);
if (!(this.active || this.stopped)) {
this.start_queue();
}
}
};
this.start_queue = function () {
RequestManager.prototype.start_queue = function () {
if (this.active) {
return;
}
@ -1669,6 +1672,8 @@ function batch_recording_rels() {
var timeout = rate - now + this.last;
setTimeout(function (foo) { foo.next() }, timeout, this);
}
}
}
};
var ws_requests = new RequestManager(1000, 1);
var edit_requests = new RequestManager(1500, 2);
}