From f0dc13cd2ee5577a40f83dcea5ecd877f76d4eaa Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 27 Apr 2017 02:06:16 +0300 Subject: [PATCH] debounce update log writer --- update.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/update.js b/update.js index 46fb0230..ec7b9641 100644 --- a/update.js +++ b/update.js @@ -128,17 +128,27 @@ var updater = { updater.schedule(); }, - log(text) { - chromeLocal.getValue('updateLog').then((lines = []) => { - const time = text && performance.now() - (updater.log.lastWriteTime || 0) > 10e3 - ? new Date().toLocaleString() + '\t' - : ''; - lines.splice(0, lines.length - 1000); - lines.push(time + text); - chromeLocal.setValue('updateLog', lines); - updater.log.lastWriteTime = performance.now(); - }); - }, + log: (() => { + let queue = []; + let lastWriteTime = 0; + return text => { + queue.push(text); + debounce(flushQueue, 1e3); + }; + function flushQueue() { + chromeLocal.getValue('updateLog').then((lines = []) => { + // our XHR timeout is 10 seconds + const time = performance.now() - lastWriteTime > 11e3 + ? new Date().toLocaleString() + '\t' + : ''; + lines.splice(0, lines.length - 1000); + lines.push(...queue.map(item => item ? time + item : '')); + chromeLocal.setValue('updateLog', lines); + lastWriteTime = performance.now(); + queue = []; + }); + } + })(), }; updater.schedule();