f2e0fd43e9
* Refactor: Make all chat area use graphql * Fix: large space between welcome msg and chat list * Fix: missing file * add pending status and fix system messages * Add: mark messages as seen in chat * Refactor: Move char opening logic to inside of chat panel * Refactor message and mark as seen * Add Recharts to package.json and fix miss data * Implements clear-chat function on graphql * Make system message sticky * Add clear message support and fix user is typing * FIx chat unread and scroll not following the tail * Change: make unread messages be marked by message and fix throttle * Don't show restore welcome message when the welcome message isn't set * Fix: scroll not following the tail properly * Fix: previous page last sender not working * Fix: scroll loading all messages * Fix messaga not marked as read --------- Co-authored-by: Gustavo Trott <gustavo@trott.com.br>
35 lines
941 B
JavaScript
35 lines
941 B
JavaScript
export function throttle(func, delay, options = {}) {
|
|
let lastInvocation = 0;
|
|
let isWaiting = false;
|
|
let timeoutId;
|
|
|
|
const leading = options.leading !== undefined ? options.leading : true;
|
|
const trailing = options.trailing !== undefined ? options.trailing : true;
|
|
|
|
return function throttled(...args) {
|
|
const invokeFunction = () => {
|
|
lastInvocation = Date.now();
|
|
isWaiting = false;
|
|
func.apply(this, args);
|
|
};
|
|
|
|
if (!isWaiting) {
|
|
if (leading) {
|
|
invokeFunction();
|
|
} else {
|
|
isWaiting = true;
|
|
}
|
|
|
|
const currentTime = Date.now();
|
|
const timeSinceLastInvocation = currentTime - lastInvocation;
|
|
|
|
if (timeSinceLastInvocation >= delay) {
|
|
clearTimeout(timeoutId);
|
|
invokeFunction();
|
|
} else if (trailing) {
|
|
clearTimeout(timeoutId);
|
|
timeoutId = setTimeout(invokeFunction, delay - timeSinceLastInvocation);
|
|
}
|
|
}
|
|
};
|
|
} |