mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
Add ajax request tracking and aborting to channel
The channel module now keeps track pending ajax requests and has an abort_all function to angle all pending requests. (imported from commit 4e78ab24d2295bd67de5633e3a200dfa489825b1)
This commit is contained in:
@@ -1,6 +1,22 @@
|
|||||||
var channel = (function () {
|
var channel = (function () {
|
||||||
|
|
||||||
var exports = {};
|
var exports = {};
|
||||||
|
var pending_requests = [];
|
||||||
|
|
||||||
|
function add_pending_request (jqXHR) {
|
||||||
|
pending_requests.push(jqXHR);
|
||||||
|
if (pending_requests.length > 50) {
|
||||||
|
blueslip.warn('The length of pending_requests is over 50. Most likely ' +
|
||||||
|
'they are not being correctly removed.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function remove_pending_request (jqXHR) {
|
||||||
|
var pending_request_index = _.indexOf(pending_requests, jqXHR)
|
||||||
|
if (pending_request_index != -1){
|
||||||
|
pending_requests.splice(pending_request_index, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function call(args, idempotent) {
|
function call(args, idempotent) {
|
||||||
// Wrap the error handlers to reload the page if we get a CSRF error
|
// Wrap the error handlers to reload the page if we get a CSRF error
|
||||||
@@ -10,6 +26,8 @@ function call(args, idempotent) {
|
|||||||
orig_error = function () {};
|
orig_error = function () {};
|
||||||
}
|
}
|
||||||
args.error = function wrapped_error(xhr, error_type, xhn) {
|
args.error = function wrapped_error(xhr, error_type, xhn) {
|
||||||
|
remove_pending_request(xhr);
|
||||||
|
|
||||||
if (xhr.status === 403 && $.parseJSON(xhr.responseText).msg.indexOf("CSRF Error:") !== -1) {
|
if (xhr.status === 403 && $.parseJSON(xhr.responseText).msg.indexOf("CSRF Error:") !== -1) {
|
||||||
reload.initiate({immediate: true});
|
reload.initiate({immediate: true});
|
||||||
}
|
}
|
||||||
@@ -20,20 +38,31 @@ function call(args, idempotent) {
|
|||||||
orig_success = function () {};
|
orig_success = function () {};
|
||||||
}
|
}
|
||||||
args.success = function wrapped_success(data, textStatus, jqXHR) {
|
args.success = function wrapped_success(data, textStatus, jqXHR) {
|
||||||
|
remove_pending_request(jqXHR);
|
||||||
|
|
||||||
if (!data && idempotent) {
|
if (!data && idempotent) {
|
||||||
// If idempotent, retry
|
// If idempotent, retry
|
||||||
blueslip.log("Retrying idempotent" + args);
|
blueslip.log("Retrying idempotent" + args);
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
$.ajax(args);
|
var jqXHR = $.ajax(args);
|
||||||
|
add_pending_request(jqXHR);
|
||||||
}, 0);
|
}, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return orig_success(data, textStatus, jqXHR);
|
return orig_success(data, textStatus, jqXHR);
|
||||||
};
|
};
|
||||||
|
|
||||||
return $.ajax(args);
|
var jqXHR = $.ajax(args);
|
||||||
|
add_pending_request(jqXHR);
|
||||||
|
return jqXHR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.abort_all = function () {
|
||||||
|
_.each(pending_requests, function (jqXHR) {
|
||||||
|
jqXHR.abort();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
exports.get = function (options) {
|
exports.get = function (options) {
|
||||||
var args = _.extend({type: "GET", dataType: "json"}, options);
|
var args = _.extend({type: "GET", dataType: "json"}, options);
|
||||||
return call(args, options.idempotent);
|
return call(args, options.idempotent);
|
||||||
|
|||||||
@@ -126,6 +126,9 @@ function do_reload_app(send_after_reload, save_state, message) {
|
|||||||
// Unbind all the jQuery event listeners
|
// Unbind all the jQuery event listeners
|
||||||
$('*').off();
|
$('*').off();
|
||||||
|
|
||||||
|
// Abort all pending ajax requests`
|
||||||
|
channel.abort_all();
|
||||||
|
|
||||||
// Free all of the DOM
|
// Free all of the DOM
|
||||||
$("html").empty();
|
$("html").empty();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user