Handle recursive reloads

This commit is contained in:
Pierre-Olivier Latour
2014-04-01 13:49:28 -07:00
parent 25c486cafa
commit 4a3135db81

View File

@ -26,6 +26,8 @@
*/ */
var _path = null; var _path = null;
var _reloading = false;
var _pendingReloads = [];
function formatFileSize(bytes) { function formatFileSize(bytes) {
if (bytes >= 1000000000) { if (bytes >= 1000000000) {
@ -46,6 +48,14 @@ function _showError(message, textStatus, errorThrown) {
} }
function _reload(path) { function _reload(path) {
if (_reloading) {
if ($.inArray(path, _pendingReloads) < 0) {
_pendingReloads.push(path);
}
return;
}
_reloading = true;
$.ajax({ $.ajax({
url: 'list', url: 'list',
type: 'GET', type: 'GET',
@ -53,8 +63,7 @@ function _reload(path) {
dataType: 'json' dataType: 'json'
}).fail(function(jqXHR, textStatus, errorThrown) { }).fail(function(jqXHR, textStatus, errorThrown) {
_showError("Failed retrieving contents of \"" + path + "\"", textStatus, errorThrown); _showError("Failed retrieving contents of \"" + path + "\"", textStatus, errorThrown);
}) }).done(function(data, textStatus, jqXHR) {
.done(function(result) {
if (path != _path) { if (path != _path) {
$("#path").empty(); $("#path").empty();
@ -77,7 +86,7 @@ function _reload(path) {
} }
$("#listing").empty(); $("#listing").empty();
for (var i = 0, file; file = result[i]; ++i) { for (var i = 0, file; file = data[i]; ++i) {
$("#listing").append(tmpl("template-listing", file)); $("#listing").append(tmpl("template-listing", file));
} }
@ -123,11 +132,16 @@ function _reload(path) {
dataType: 'json' dataType: 'json'
}).fail(function(jqXHR, textStatus, errorThrown) { }).fail(function(jqXHR, textStatus, errorThrown) {
_showError("Failed deleting \"" + path + "\"", textStatus, errorThrown); _showError("Failed deleting \"" + path + "\"", textStatus, errorThrown);
}).always(function(result) { }).always(function() {
_reload(_path); _reload(_path);
}); });
}); });
}).always(function() {
_reloading = false;
if (_pendingReloads.length > 0) {
_reload(_pendingReloads.shift());
}
}); });
} }
@ -199,7 +213,7 @@ $(document).ready(function() {
dataType: 'json' dataType: 'json'
}).fail(function(jqXHR, textStatus, errorThrown) { }).fail(function(jqXHR, textStatus, errorThrown) {
_showError("Failed creating folder \"" + name + "\" in \"" + _path + "\"", textStatus, errorThrown); _showError("Failed creating folder \"" + name + "\" in \"" + _path + "\"", textStatus, errorThrown);
}).always(function(result) { }).always(function() {
_reload(_path); _reload(_path);
}); });
} }