Only reload the page when the user is idle

Also, clean up reload-related function names a bit

(imported from commit 4c2e17c06d7567e71b92f6e1b04a44aeff419bff)
This commit is contained in:
Zev Benjamin 2012-10-19 14:04:20 -04:00
parent ae08f512dd
commit 88e09c6166
3 changed files with 49 additions and 17 deletions

View File

@ -30,6 +30,7 @@
<script type="text/javascript" src="{{ static_third }}spin/spin.min.js"></script> <script type="text/javascript" src="{{ static_third }}spin/spin.min.js"></script>
<script type="text/javascript" src="{{ static_third }}jquery-mousewheel/jquery.mousewheel.js"></script> <script type="text/javascript" src="{{ static_third }}jquery-mousewheel/jquery.mousewheel.js"></script>
<script type="text/javascript" src="{{ static_third }}jquery-throttle-debounce/jquery.ba-throttle-debounce.min.js"></script> <script type="text/javascript" src="{{ static_third }}jquery-throttle-debounce/jquery.ba-throttle-debounce.min.js"></script>
<script type="text/javascript" src="{{ static_third }}jquery.idle/jquery.idle.js"></script>
<script type="text/javascript" src="{{ static_hidden }}js/setup.js"></script> <script type="text/javascript" src="{{ static_hidden }}js/setup.js"></script>
<script type="text/javascript" src="{{ static_hidden }}js/rows.js"></script> <script type="text/javascript" src="{{ static_hidden }}js/rows.js"></script>
<script type="text/javascript" src="{{ static_hidden }}js/narrow.js"></script> <script type="text/javascript" src="{{ static_hidden }}js/narrow.js"></script>

View File

@ -42,7 +42,7 @@ var globals =
+ ' select_message select_message_by_id' + ' select_message select_message_by_id'
+ ' scroll_to_selected select_and_show_by_id' + ' scroll_to_selected select_and_show_by_id'
+ ' selected_message selected_message_id' + ' selected_message selected_message_id'
+ ' reload_app reloading_app reload_app_preserving_compose' + ' start_reload_app reloading_app do_reload_app_preserving_compose'
+ ' at_top_of_viewport at_bottom_of_viewport' + ' at_top_of_viewport at_bottom_of_viewport'
+ ' viewport' + ' viewport'
; ;

View File

@ -30,15 +30,12 @@ $(function () {
send_status.hide(); send_status.hide();
compose.hide(); compose.hide();
buttons.removeAttr('disabled'); buttons.removeAttr('disabled');
if (get_updates_params.reload_pending) { start_reload_app();
reload_app();
return;
}
}, },
error: function (xhr, error_type) { error: function (xhr, error_type) {
if (error_type !== 'timeout' && get_updates_params.reload_pending) { if (error_type !== 'timeout' && get_updates_params.reload_pending) {
// The error might be due to the server changing // The error might be due to the server changing
reload_app_preserving_compose(true); do_reload_app_preserving_compose(true);
return; return;
} }
var response = "Error sending message"; var response = "Error sending message";
@ -521,19 +518,54 @@ function do_reload_app() {
window.location.reload(true); window.location.reload(true);
} }
function reload_app() { function start_reload_app() {
// If we can, reload the page immediately if (get_updates_params.reload_pending) {
if (! compose.composing()) { return;
do_reload_app();
} }
get_updates_params.reload_pending = 1;
// If the user is composing a message, wait until he's done or // Always reload after 5 minutes
// until a timeout expires setTimeout(function () { do_reload_app_preserving_compose(false); },
setTimeout(function () { reload_app_preserving_compose(false); }, 1000 * 60 * 5);
1000 * 60 * 5); // 5 minutes
// If the user is composing a message, reload if they become
// idle while composing. If they finish composing, the
// submit code will reload the app. If they cancel the
// compose, wait until they're idle again
// If the user is not composing, reload if the user becomes idle.
// If they start composing, postpone reloading
var idle_control;
var composing_timeout = 1000*30;
var home_timeout = 1000*10;
var compose_canceled_handler, compose_started_handler;
compose_canceled_handler = function () {
idle_control.cancel();
idle_control = $(document).idle({'idle': home_timeout,
'onIdle': do_reload_app});
$(document).one('compose_started.zephyr', compose_started_handler);
};
compose_started_handler = function () {
idle_control.cancel();
idle_control = $(document).idle({'idle': composing_timeout,
'onIdle': do_reload_app_preserving_compose});
$(document).one('compose_canceled.zephyr', compose_canceled_handler);
};
if (compose.composing()) {
idle_control = $(document).idle({'idle': composing_timeout,
'onIdle': do_reload_app_preserving_compose});
$(document).one('compose_canceled.zephyr', compose_canceled_handler);
} else {
idle_control = $(document).idle({'idle': home_timeout,
'onIdle': do_reload_app});
$(document).one('compose_started.zephyr', compose_started_handler);
}
} }
function reload_app_preserving_compose(send_after_reload) { function do_reload_app_preserving_compose(send_after_reload) {
var url = "#reload:send_after_reload=" + Number(send_after_reload); var url = "#reload:send_after_reload=" + Number(send_after_reload);
if (compose.composing() === 'stream') { if (compose.composing() === 'stream') {
url += "+msg_type=stream"; url += "+msg_type=stream";
@ -596,8 +628,7 @@ function get_updates() {
if (get_updates_params.server_generation === -1) { if (get_updates_params.server_generation === -1) {
get_updates_params.server_generation = data.server_generation; get_updates_params.server_generation = data.server_generation;
} else if (data.server_generation !== get_updates_params.server_generation) { } else if (data.server_generation !== get_updates_params.server_generation) {
get_updates_params.reload_pending = 1; start_reload_app();
reload_app();
} }
add_messages(data); add_messages(data);