diff --git a/src/global/actions/apiUpdaters/users.ts b/src/global/actions/apiUpdaters/users.ts index c102bfee3..f1dae5f24 100644 --- a/src/global/actions/apiUpdaters/users.ts +++ b/src/global/actions/apiUpdaters/users.ts @@ -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 = {}; -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; } diff --git a/src/lib/teact/heavyAnimation.ts b/src/lib/teact/heavyAnimation.ts index d95473f44..816376333 100644 --- a/src/lib/teact/heavyAnimation.ts +++ b/src/lib/teact/heavyAnimation.ts @@ -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(fn: F) { + return throttleWith(onFullyIdle, fn); +}