[Perf] Multitab: Broadcast diff with onFullyIdle

This commit is contained in:
Alexander Zinchuk 2024-09-06 15:42:29 +02:00
parent 496c6c697d
commit 35948a8bcb

View File

@ -24,6 +24,8 @@ import { getCurrentTabId, signalPasscodeHash, subscribeToTokenDied } from './est
import { omit } from './iteratees'; import { omit } from './iteratees';
import { IS_MULTITAB_SUPPORTED } from './windowEnvironment'; import { IS_MULTITAB_SUPPORTED } from './windowEnvironment';
import { onFullyIdle } from '../hooks/useHeavyAnimationCheck';
type BroadcastChannelRefreshLangpack = { type BroadcastChannelRefreshLangpack = {
type: 'langpackRefresh'; type: 'langpackRefresh';
langCode: string; langCode: string;
@ -118,6 +120,27 @@ const channel = IS_MULTITAB_SUPPORTED
? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel ? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel
: undefined; : undefined;
let globalToDiffWith: GlobalState | undefined;
function broadcastDiffOnIdle(_globalToDiffWith: GlobalState) {
globalToDiffWith = _globalToDiffWith;
onFullyIdle(() => {
if (!channel || !globalToDiffWith) return;
const diff = deepDiff(globalToDiffWith, prevGlobal);
globalToDiffWith = undefined;
if (typeof diff !== 'symbol') {
channel.postMessage({
type: 'globalDiffUpdate',
diff,
});
}
});
}
export function unsubcribeFromMultitabBroadcastChannel() { export function unsubcribeFromMultitabBroadcastChannel() {
if (channel) { if (channel) {
channel.removeEventListener('message', handleMessage); channel.removeEventListener('message', handleMessage);
@ -175,14 +198,7 @@ export function subscribeToMultitabBroadcastChannel() {
return; return;
} }
const diff = deepDiff(prevGlobal, global); broadcastDiffOnIdle(prevGlobal);
if (typeof diff !== 'symbol') {
channel.postMessage({
type: 'globalDiffUpdate',
diff,
});
}
prevGlobal = global; prevGlobal = global;
}); });