Projet

Général

Profil

0001-do-not-refresh-table-when-tab-is-invisible-or-user-i.patch

Benjamin Dauvergne, 26 février 2019 17:42

Télécharger (2,6 ko)

Voir les différences:

Subject: [PATCH] do not refresh table when tab is invisible or user idle
 (#26804)

* refresh timer is cleared on tab invisible event
* refresh timer is cleared after 5 minutes of inactivity
* table is refreshed immediately when tab become visible and user has been inactive
more than 5 minutes
 wcs/qommon/static/js/wcs.listing.js | 42 +++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
wcs/qommon/static/js/wcs.listing.js
1
/**
2
 * vis() -> bool : is tab visible ?
3
 * vis(aFunction) -> call aFunction on visibility change.
4
 */
5
var vis = (function(){
6
    var stateKey, eventKey, keys = {
7
        hidden: "visibilitychange",
8
        webkitHidden: "webkitvisibilitychange",
9
        mozHidden: "mozvisibilitychange",
10
        msHidden: "msvisibilitychange"
11
    };
12
    for (stateKey in keys) {
13
        if (stateKey in document) {
14
            eventKey = keys[stateKey];
15
            break;
16
        }
17
    }
18
    return function(c) {
19
        if (c) document.addEventListener(eventKey, c);
20
        return !document[stateKey];
21
    }
22
})();
23

  
1 24
function prepare_page_links() {
2 25
  $('#page-links a').click(function() {
3 26
    $('form#listing-settings input[name="offset"]').val($(this).data('offset'));
......
190 213

  
191 214
  if ($('#statistics').length == 0) {
192 215
    var idle_id = null;
216
    var longidle_id = null;
217
    vis(function () {
218
      if (! vis()) {
219
        /* stop refreshing in invisible tabs */
220
        if (idle_id) idle_id = window.clearInterval(idle_id);
221
      } else {
222
        /* if tab invisible for more than 5 minutes, refresh now */
223
        if (! longidle_id) autorefresh_table();
224
      }
225
    }
193 226
    $(window).on('mousemove keydown mousedown touchstart', function() {
227
      /* if refresh timer exists, clear it */
194 228
      if (idle_id) window.clearInterval(idle_id);
229
      /* if stop refresh timer exists, clear it */
230
      if (longidle_id) window.clearTimeout(longidle_id);
231
      /* launch timer to refresh every 30 seconds */
195 232
      idle_id = setInterval(autorefresh_table, 30000);
233
      /* launch timer to stop refresh after 5 minutes idle */
234
      longidle_id = setTimeout(function () {
235
          if (idle_id) idle_id = window.clearInterval(idle_id);
236
          longidle_id = undefined;
237
      }, 300 * 1000);
196 238
    });
197 239
  }
198 240

  
199
-