From b5b25458db19836ecbd50c29af65a37fd847f616 Mon Sep 17 00:00:00 2001 From: Guillaume Taquet Gasperini Date: Mon, 20 May 2019 21:42:42 +0200 Subject: [PATCH] turn off livestats update when the tab isn't active Fixes #330: Turning off the updating when the tab isn't active When the tab is not visible from the user, the livestats refresh are stopped. As soon as its visibility changes (from visibilitychange API), the livestats are refreshed as usual. This will prevent unnecessary calls to the different services when the tab is hidden from the user. --- public/js/app.js | 2 +- resources/assets/js/app.js | 52 ++++++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/public/js/app.js b/public/js/app.js index 338c0fb0..3cc22f06 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1 +1 @@ -!function(t,e){"function"==typeof define&&define.amd?define("ev-emitter/ev-emitter",e):"object"==typeof module&&module.exports?module.exports=e():t.EvEmitter=e()}("undefined"!=typeof window?window:this,function(){function t(){}var e=t.prototype;return e.on=function(t,e){if(t&&e){var n=this._events=this._events||{},i=n[t]=n[t]||[];return-1==i.indexOf(e)&&i.push(e),this}},e.once=function(t,e){if(t&&e){this.on(t,e);var n=this._onceEvents=this._onceEvents||{};return(n[t]=n[t]||{})[e]=!0,this}},e.off=function(t,e){var n=this._events&&this._events[t];if(n&&n.length){var i=n.indexOf(e);return-1!=i&&n.splice(i,1),this}},e.emitEvent=function(t,e){var n=this._events&&this._events[t];if(n&&n.length){var i=0,o=n[i];e=e||[];for(var s=this._onceEvents&&this._onceEvents[t];o;){var r=s&&s[o];r&&(this.off(t,o),delete s[o]),o.apply(this,e),i+=r?0:1,o=n[i]}return this}},t}),function(t,e){"function"==typeof define&&define.amd?define("unipointer/unipointer",["ev-emitter/ev-emitter"],function(n){return e(t,n)}):"object"==typeof module&&module.exports?module.exports=e(t,require("ev-emitter")):t.Unipointer=e(t,t.EvEmitter)}(window,function(t,e){function n(){}function i(){}var o=i.prototype=Object.create(e.prototype);o.bindStartEvent=function(t){this._bindStartEvent(t,!0)},o.unbindStartEvent=function(t){this._bindStartEvent(t,!1)},o._bindStartEvent=function(e,n){n=void 0===n||!!n;var i=n?"addEventListener":"removeEventListener";t.navigator.pointerEnabled?e[i]("pointerdown",this):t.navigator.msPointerEnabled?e[i]("MSPointerDown",this):(e[i]("mousedown",this),e[i]("touchstart",this))},o.handleEvent=function(t){var e="on"+t.type;this[e]&&this[e](t)},o.getTouch=function(t){for(var e=0;e.5;var o=this.colorGrid[e.toUpperCase()];this.updateCursor(o),this.setTexts(),this.setBackgrounds(),n||this.emitEvent("change",[e,t.hue,t.sat,t.lum])}},p.setTexts=function(){if(this.setTextElems)for(var t=0;t.5;var o=this.colorGrid[e.toUpperCase()];this.updateCursor(o),this.setTexts(),this.setBackgrounds(),n||this.emitEvent("change",[e,t.hue,t.sat,t.lum])}},h.setTexts=function(){if(this.setTextElems)for(var t=0;t0&&(void 0===document.addEventListener||void 0===t?console.log("This browser does not support visibilityChange"):document.addEventListener(e,function(){document[t]?function(){for(var t of n)window.clearTimeout(t)}():function(){for(var t of i)t()}()},!1),o.each(function(t){var e=$(this).data("id"),o=1==$(this).data("dataonly")?2e4:1e3,s=$(this),r=5e3,a=function i(){$.ajax({url:"/get_stats/"+e,dataType:"json",success:function(t){s.html(t.html),"active"==t.status?r=o:r<3e4&&(r+=2e3)},complete:function(){n[t]=window.setTimeout(i,r)}})};i[t]=a,a()})),$("#upload").change(function(){!function(t){if(t.files&&t.files[0]){var e=new FileReader;e.onload=function(t){$("#appimage img").attr("src",t.target.result)},e.readAsDataURL(t.files[0])}}(this)}),$("#sortable").sortable({stop:function(t,e){var n=$("#sortable").sortable("toArray",{attribute:"data-id"});$.post("/order",{order:n})}}),$("#sortable").sortable("disable"),$("#app").on("click","#config-button",function(t){t.preventDefault();var e=$("#app"),n=e.hasClass("header");e.toggleClass("header"),n?($(".add-item").hide(),$(".item-edit").hide(),$("#app").removeClass("sidebar"),$("#sortable").sortable("disable")):($("#sortable").sortable("enable"),setTimeout(function(){$(".add-item").fadeIn(),$(".item-edit").fadeIn()},350))}).on("click","#add-item, #pin-item",function(t){t.preventDefault();var e=$("#app");e.hasClass("sidebar");e.toggleClass("sidebar")}).on("click",".close-sidenav",function(t){t.preventDefault(),$("#app").removeClass("sidebar")}).on("click","#test_config",function(t){t.preventDefault();var e=$("#create input[name=url]").val(),n=$('#create input[name="config[override_url]"]').val();n.length&&""!=n&&(e=n);var i={};i.url=e,$(".config-item").each(function(t){var e=$(this).data("config");i[e]=$(this).val()}),$.post("/test_config",{data:i},function(t){alert(t)})}),$("#pinlist").on("click","a",function(t){t.preventDefault();var e=$(this),n=e.data("id");$.get("items/pintoggle/"+n+"/true",function(t){var n=$(t).filter("#sortable").html();$("#sortable").html(n),e.toggleClass("active")})})}); diff --git a/resources/assets/js/app.js b/resources/assets/js/app.js index 6deed36a..99590e7a 100644 --- a/resources/assets/js/app.js +++ b/resources/assets/js/app.js @@ -8,15 +8,55 @@ $.when( $.ready ).then(function() { }, 3500); } - if($('.livestats-container').length) { - $('.livestats-container').each(function(index){ + // from https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API + // Set the name of the hidden property and the change event for visibility + var hidden, visibilityChange; + if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support + hidden = "hidden"; + visibilityChange = "visibilitychange"; + } else if (typeof document.msHidden !== "undefined") { + hidden = "msHidden"; + visibilityChange = "msvisibilitychange"; + } else if (typeof document.webkitHidden !== "undefined") { + hidden = "webkitHidden"; + visibilityChange = "webkitvisibilitychange"; + } + + var livestatsRefreshTimeouts = []; + var livestatsFuncs = []; + var livestatsContainers = $('.livestats-container'); + function stopLivestatsRefresh() { + for (var timeoutId of livestatsRefreshTimeouts) { + window.clearTimeout(timeoutId); + } + } + function startLivestatsRefresh() { + for (var fun of livestatsFuncs) { + fun(); + } + } + + if (livestatsContainers.length > 0) { + if (typeof document.addEventListener === "undefined" || hidden === undefined) { + console.log("This browser does not support visibilityChange"); + } else { + document.addEventListener(visibilityChange, function() { + if (document[hidden]) { + stopLivestatsRefresh(); + } else { + startLivestatsRefresh(); + } + }, false); + } + + livestatsContainers.each(function(index){ var id = $(this).data('id'); var dataonly = $(this).data('dataonly'); var increaseby = (dataonly == 1) ? 20000 : 1000; var container = $(this); var max_timer = 30000; var timer = 5000; - (function worker() { + var fun = function worker() { $.ajax({ url: '/get_stats/'+id, dataType: 'json', @@ -29,10 +69,12 @@ $.when( $.ready ).then(function() { }, complete: function() { // Schedule the next request when the current one's complete - setTimeout(worker, timer); + livestatsRefreshTimeouts[index] = window.setTimeout(worker, timer); } }); - })(); + }; + livestatsFuncs[index] = fun; + fun(); }); }