Get rid of immediate callbacks in multitab

This commit is contained in:
Alexander Zinchuk 2023-10-27 12:49:55 +02:00
parent 6ebd258fb1
commit 8c0c871023
2 changed files with 15 additions and 17 deletions

View File

@ -25,7 +25,6 @@ export interface ActionOptions {
forceOnHeavyAnimation?: boolean; forceOnHeavyAnimation?: boolean;
// Workaround for iOS gesture history navigation // Workaround for iOS gesture history navigation
forceSyncOnIOs?: boolean; forceSyncOnIOs?: boolean;
noUpdate?: boolean;
} }
type Actions = Record<ActionNames, (payload?: ActionPayload, options?: ActionOptions) => void>; type Actions = Record<ActionNames, (payload?: ActionPayload, options?: ActionOptions) => void>;
@ -50,7 +49,6 @@ const DEBUG_releaseCapturedIdThrottled = throttleWithTickEnd(() => {
const actionHandlers: Record<string, ActionHandler[]> = {}; const actionHandlers: Record<string, ActionHandler[]> = {};
const callbacks: Function[] = [updateContainers]; const callbacks: Function[] = [updateContainers];
const immediateCallbacks: Function[] = [];
const actions = {} as Actions; const actions = {} as Actions;
const containers = new Map<string, { const containers = new Map<string, {
mapStateToProps: MapStateToProps<any>; mapStateToProps: MapStateToProps<any>;
@ -66,10 +64,6 @@ const runCallbacksThrottled = throttleWithTickEnd(runCallbacks);
let forceOnHeavyAnimation = true; let forceOnHeavyAnimation = true;
function runImmediateCallbacks() {
immediateCallbacks.forEach((cb) => cb(currentGlobal));
}
function runCallbacks() { function runCallbacks() {
if (forceOnHeavyAnimation) { if (forceOnHeavyAnimation) {
forceOnHeavyAnimation = false; forceOnHeavyAnimation = false;
@ -93,8 +87,6 @@ export function setGlobal(newGlobal?: GlobalState, options?: ActionOptions) {
currentGlobal = newGlobal; currentGlobal = newGlobal;
if (!options?.noUpdate) runImmediateCallbacks();
if (options?.forceSyncOnIOs) { if (options?.forceSyncOnIOs) {
forceOnHeavyAnimation = true; forceOnHeavyAnimation = true;
runCallbacks(); runCallbacks();
@ -225,14 +217,14 @@ export function addActionHandler(name: ActionNames, handler: ActionHandler) {
actionHandlers[name].push(handler); actionHandlers[name].push(handler);
} }
export function addCallback(cb: Function, isImmediate = false) { export function addCallback(cb: Function) {
(isImmediate ? immediateCallbacks : callbacks).push(cb); callbacks.push(cb);
} }
export function removeCallback(cb: Function, isImmediate = false) { export function removeCallback(cb: Function) {
const index = (isImmediate ? immediateCallbacks : callbacks).indexOf(cb); const index = callbacks.indexOf(cb);
if (index !== -1) { if (index !== -1) {
(isImmediate ? immediateCallbacks : callbacks).splice(index, 1); callbacks.splice(index, 1);
} }
} }

View File

@ -7,7 +7,7 @@ import type { MethodArgs, Methods } from '../api/gramjs/methods/types';
import type { ApiInitialArgs } from '../api/types'; import type { ApiInitialArgs } from '../api/types';
import type { GlobalState } from '../global/types'; import type { GlobalState } from '../global/types';
import { DATA_BROADCAST_CHANNEL_NAME, MULTITAB_LOCALSTORAGE_KEY } from '../config'; import { DATA_BROADCAST_CHANNEL_NAME, DEBUG, MULTITAB_LOCALSTORAGE_KEY } from '../config';
import { selectTabState } from '../global/selectors'; import { selectTabState } from '../global/selectors';
import { import {
callApiLocal, callApiLocal,
@ -157,6 +157,10 @@ export function subscribeToMultitabBroadcastChannel() {
return; return;
} }
if (prevGlobal === global) {
return;
}
if (!prevGlobal) { if (!prevGlobal) {
prevGlobal = global; prevGlobal = global;
channel.postMessage({ channel.postMessage({
@ -165,6 +169,7 @@ export function subscribeToMultitabBroadcastChannel() {
}); });
return; return;
} }
const diff = deepDiff(prevGlobal, global); const diff = deepDiff(prevGlobal, global);
if (typeof diff !== 'symbol') { if (typeof diff !== 'symbol') {
@ -175,7 +180,7 @@ export function subscribeToMultitabBroadcastChannel() {
} }
prevGlobal = global; prevGlobal = global;
}, true); });
channel.addEventListener('message', handleMessage); channel.addEventListener('message', handleMessage);
} }
@ -198,11 +203,12 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) {
const { diff } = data; const { diff } = data;
const oldGlobal = getGlobal(); const oldGlobal = getGlobal();
const global = deepMerge(oldGlobal, diff); const global = deepMerge(oldGlobal, diff);
// @ts-ignore // @ts-ignore
global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId; global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId;
setGlobal(global, { noUpdate: true });
prevGlobal = global; prevGlobal = global;
setGlobal(global);
break; break;
} }
@ -211,8 +217,8 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) {
const oldGlobal = getGlobal(); const oldGlobal = getGlobal();
// @ts-ignore // @ts-ignore
data.global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId; data.global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId;
setGlobal(data.global, { noUpdate: true });
prevGlobal = data.global; prevGlobal = data.global;
setGlobal(data.global);
if (resolveGlobalPromise) { if (resolveGlobalPromise) {
resolveGlobalPromise(); resolveGlobalPromise();
resolveGlobalPromise = undefined; resolveGlobalPromise = undefined;