feed: less eager rendering
before this change every new incoming text note called a render, that filters, sorts and iterates all known text notes and creates missing dom elements and appends into the right place. this change throttles and debounces (both!) the render function, that less checks have to be performed, especially on page load when potentially 100s of events arrive within a short time. it is important to throttle and debounce, else either the last call is missed or no render is called while events are being received. this change surfaced an error in recommend server that depended on all known text notes already being rendered and inside the dom. this function should probably be handled by render feed itself.pull/67/head
parent
dffcbc6b2b
commit
f4f951469f
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* throttle and debounce given function in regular time interval,
|
||||
* but with the difference that the last call will be debounced and therefore never missed.
|
||||
* @param {*} function to throttle and debounce
|
||||
* @param {*} time desired interval to execute function
|
||||
* @returns callback
|
||||
*/
|
||||
export const bounce = (fn, time) => {
|
||||
let throttle;
|
||||
let debounce;
|
||||
return (/*...args*/) => {
|
||||
if (throttle) {
|
||||
clearTimeout(debounce);
|
||||
debounce = setTimeout(() => fn(/*...args*/), time);
|
||||
return;
|
||||
}
|
||||
fn(/*...args*/);
|
||||
throttle = setTimeout(() => {
|
||||
throttle = false;
|
||||
}, time);
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue