code cosmetics: simplify debounce()
This commit is contained in:
parent
fdc15d24d9
commit
f9e90f9cd0
25
messaging.js
25
messaging.js
|
@ -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) {
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user