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 timers = debounce.timers = debounce.timers || new Map();
debounce.run = debounce.run || ((fn, ...args) => {
timers.delete(fn);
const debounce = Object.assign((fn, delay, ...args) => {
clearTimeout(debounce.timers.get(fn));
debounce.timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));
}, {
timers: new Map(),
run(fn, ...args) {
debounce.timers.delete(fn);
fn(...args);
},
unregister(fn) {
clearTimeout(debounce.timers.get(fn));
debounce.timers.delete(fn);
},
});
debounce.unregister = debounce.unregister || (fn => {
clearTimeout(timers.get(fn));
timers.delete(fn);
});
clearTimeout(timers.get(fn));
timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));
}
function deepCopy(obj) {

View File

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