debounce update log writer

This commit is contained in:
tophf 2017-04-27 02:06:16 +03:00
parent a22874a898
commit f0dc13cd2e

View File

@ -128,17 +128,27 @@ var updater = {
updater.schedule(); updater.schedule();
}, },
log(text) { log: (() => {
let queue = [];
let lastWriteTime = 0;
return text => {
queue.push(text);
debounce(flushQueue, 1e3);
};
function flushQueue() {
chromeLocal.getValue('updateLog').then((lines = []) => { chromeLocal.getValue('updateLog').then((lines = []) => {
const time = text && performance.now() - (updater.log.lastWriteTime || 0) > 10e3 // our XHR timeout is 10 seconds
const time = performance.now() - lastWriteTime > 11e3
? new Date().toLocaleString() + '\t' ? new Date().toLocaleString() + '\t'
: ''; : '';
lines.splice(0, lines.length - 1000); lines.splice(0, lines.length - 1000);
lines.push(time + text); lines.push(...queue.map(item => item ? time + item : ''));
chromeLocal.setValue('updateLog', lines); chromeLocal.setValue('updateLog', lines);
updater.log.lastWriteTime = performance.now(); lastWriteTime = performance.now();
queue = [];
}); });
}, }
})(),
}; };
updater.schedule(); updater.schedule();