From c7b051e73bf58b2fd4d7b515d0384945266150d9 Mon Sep 17 00:00:00 2001 From: Oliver Gerlich Date: Mon, 16 Jul 2018 22:18:32 +0200 Subject: [PATCH] list perf: re-render list at most once per second This should at least keep the browser somewhat usable even if lots of updates are received from server. --- webroot/js/view/list.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/webroot/js/view/list.js b/webroot/js/view/list.js index 5be1348..9ad589a 100644 --- a/webroot/js/view/list.js +++ b/webroot/js/view/list.js @@ -10,6 +10,8 @@ export class ListView extends View { constructor () { super(); + this.lastRenderTime = null; + this.deferredRenderTimer = null; const table = domlib.newAt(this.el, 'table'), thead = domlib.newAt(table, 'thead'); @@ -386,6 +388,28 @@ export class ListView extends View { if (this.editing && this.tbody) { return; } + + // render at most once every 1000 msec + const nowTime = Date.now(); + if (this.lastRenderTime != null) { + const timeSinceLastRender = nowTime - this.lastRenderTime, + maxRenderDelay = 1000; + + if (timeSinceLastRender < maxRenderDelay) { + console.log("deferring render() impl"); + if (this.deferredRenderTimer == null) { + this.deferredRenderTimer = setTimeout(() => { + console.log("doing deferred render() impl now"); + this.render(); + this.deferredRenderTimer = null; + }, maxRenderDelay - timeSinceLastRender); + } + return; + } + } + this.lastRenderTime = nowTime; + console.log("immediate render()"); + while(this.tbody.hasChildNodes()) { this.tbody.removeChild(this.tbody.firstElementChild); }