From 19823bdec8643f56b4a9162b2abc7db487b35d9c Mon Sep 17 00:00:00 2001 From: Jessica McKellar Date: Wed, 10 Oct 2012 14:21:22 -0400 Subject: [PATCH] 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) --- zephyr/static/js/zephyr.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/zephyr/static/js/zephyr.js b/zephyr/static/js/zephyr.js index 020ac73708..4bf4d06759 100644 --- a/zephyr/static/js/zephyr.js +++ b/zephyr/static/js/zephyr.js @@ -445,8 +445,10 @@ function add_messages(data) { update_autocomplete(); } +var get_updates_xhr; +var get_updates_timeout; function get_updates() { - $.ajax({ + get_updates_xhr = $.ajax({ type: 'POST', url: 'get_updates', data: received, @@ -457,7 +459,7 @@ function get_updates() { $('#connection-error').hide(); add_messages(data); - setTimeout(get_updates, 0); + get_updates_timeout = setTimeout(get_updates, 0); }, error: function (xhr, error_type, exn) { if (error_type === 'timeout') { @@ -475,13 +477,27 @@ function get_updates() { } 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); +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() { return ($(window).scrollTop() === 0); }