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