[Perf] Use throttleWithFullyIdle for user status updates

This commit is contained in:
Alexander Zinchuk 2024-09-19 20:43:42 +02:00
parent af9d8695f4
commit 7634094883
2 changed files with 16 additions and 16 deletions

View File

@ -1,29 +1,24 @@
import { throttleWithFullyIdle } from '../../../lib/teact/heavyAnimation';
import type { ApiUserStatus } from '../../../api/types';
import type { ActionReturnType, RequiredGlobalState } from '../../types';
import { throttle } from '../../../util/schedulers';
import { addActionHandler, getGlobal, setGlobal } from '../../index';
import {
deleteContact, replaceUserStatuses, updatePeerStoriesHidden, updateUser, updateUserFullInfo,
deleteContact,
replaceUserStatuses,
updatePeerStoriesHidden,
updateUser,
updateUserFullInfo,
} from '../../reducers';
import {
selectIsChatWithSelf,
selectIsCurrentUserPremium,
selectUser,
selectUserFullInfo,
selectIsChatWithSelf, selectIsCurrentUserPremium, selectUser, selectUserFullInfo,
} from '../../selectors';
const STATUS_UPDATE_THROTTLE = 3000;
const flushStatusUpdatesThrottled = throttle(flushStatusUpdates, STATUS_UPDATE_THROTTLE, true);
const updateStatusesOnFullyIdle = throttleWithFullyIdle(flushStatusUpdates);
let pendingStatusUpdates: Record<string, ApiUserStatus> = {};
function scheduleStatusUpdate(userId: string, statusUpdate: ApiUserStatus) {
pendingStatusUpdates[userId] = statusUpdate;
flushStatusUpdatesThrottled();
}
function flushStatusUpdates() {
// eslint-disable-next-line eslint-multitab-tt/no-immediate-global
let global = getGlobal() as RequiredGlobalState;
@ -85,7 +80,8 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
case 'updateUserStatus': {
// Status updates come very often so we throttle them
scheduleStatusUpdate(update.userId, update.status);
pendingStatusUpdates[update.userId] = update.status;
updateStatusesOnFullyIdle();
return undefined;
}

View File

@ -1,4 +1,4 @@
import { onIdle } from '../../util/schedulers';
import { onIdle, throttleWith } from '../../util/schedulers';
import { createSignal } from '../../util/signals';
import { requestMeasure } from '../fasterdom/fasterdom';
@ -48,3 +48,7 @@ export function onFullyIdle(cb: NoneToVoidFunction) {
}
});
}
export function throttleWithFullyIdle<F extends AnyToVoidFunction>(fn: F) {
return throttleWith(onFullyIdle, fn);
}