code cosmetics: simplify debounce()

This commit is contained in:
tophf 2017-04-22 21:02:49 +03:00
parent fdc15d24d9
commit f9e90f9cd0
2 changed files with 14 additions and 13 deletions

View File

@ -204,19 +204,20 @@ function tryJSONparse(jsonString) {
} }
function debounce(fn, delay, ...args) { const debounce = Object.assign((fn, delay, ...args) => {
const timers = debounce.timers = debounce.timers || new Map(); clearTimeout(debounce.timers.get(fn));
debounce.run = debounce.run || ((fn, ...args) => { debounce.timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));
timers.delete(fn); }, {
timers: new Map(),
run(fn, ...args) {
debounce.timers.delete(fn);
fn(...args); fn(...args);
}); },
debounce.unregister = debounce.unregister || (fn => { unregister(fn) {
clearTimeout(timers.get(fn)); clearTimeout(debounce.timers.get(fn));
timers.delete(fn); debounce.timers.delete(fn);
}); },
clearTimeout(timers.get(fn)); });
timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));
}
function deepCopy(obj) { function deepCopy(obj) {

View File

@ -66,7 +66,7 @@ var updater = {
if (interval) { if (interval) {
const elapsed = Math.max(0, Date.now() - updater.lastUpdateTime); const elapsed = Math.max(0, Date.now() - updater.lastUpdateTime);
debounce(updater.checkAllStyles, Math.max(10e3, interval - elapsed)); debounce(updater.checkAllStyles, Math.max(10e3, interval - elapsed));
} else if (debounce.timers) { } else {
debounce.unregister(updater.checkAllStyles); debounce.unregister(updater.checkAllStyles);
} }
}, },