Add a watchdog for your laptop waking up that wakes up polling more promptly.

Without this, if you suspend and resume you wait up to 90 seconds for
long polling to retry.

(imported from commit 5e964c357f395d30107af5b2c934949058c0d3a8)
This commit is contained in:
Jessica McKellar 2012-10-10 14:21:22 -04:00
parent 48cda57d84
commit 19823bdec8
1 changed files with 19 additions and 3 deletions

View File

@ -445,8 +445,10 @@ function add_messages(data) {
update_autocomplete(); update_autocomplete();
} }
var get_updates_xhr;
var get_updates_timeout;
function get_updates() { function get_updates() {
$.ajax({ get_updates_xhr = $.ajax({
type: 'POST', type: 'POST',
url: 'get_updates', url: 'get_updates',
data: received, data: received,
@ -457,7 +459,7 @@ function get_updates() {
$('#connection-error').hide(); $('#connection-error').hide();
add_messages(data); add_messages(data);
setTimeout(get_updates, 0); get_updates_timeout = setTimeout(get_updates, 0);
}, },
error: function (xhr, error_type, exn) { error: function (xhr, error_type, exn) {
if (error_type === 'timeout') { if (error_type === 'timeout') {
@ -475,13 +477,27 @@ function get_updates() {
} }
var retry_sec = Math.min(90, Math.exp(received.failures/2)); var retry_sec = Math.min(90, Math.exp(received.failures/2));
setTimeout(get_updates, retry_sec*1000); get_updates_timeout = setTimeout(get_updates, retry_sec*1000);
} }
}); });
} }
$(get_updates); $(get_updates);
var watchdog_time = $.now();
setInterval(function() {
var new_time = $.now();
if ((new_time - watchdog_time) > 20000) { // 20 seconds.
// Our app's JS wasn't running (the machine was probably
// asleep). Now that we're running again, immediately poll for
// new updates.
get_updates_xhr.abort();
clearTimeout(get_updates_timeout);
get_updates();
}
watchdog_time = new_time;
}, 5000);
function at_top_of_viewport() { function at_top_of_viewport() {
return ($(window).scrollTop() === 0); return ($(window).scrollTop() === 0);
} }