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;
// Workaround for iOS gesture history navigation
forceSyncOnIOs?: boolean;
noUpdate?: boolean;
}
type Actions = Record<ActionNames, (payload?: ActionPayload, options?: ActionOptions) => void>;
@ -50,7 +49,6 @@ const DEBUG_releaseCapturedIdThrottled = throttleWithTickEnd(() => {
const actionHandlers: Record<string, ActionHandler[]> = {};
const callbacks: Function[] = [updateContainers];
const immediateCallbacks: Function[] = [];
const actions = {} as Actions;
const containers = new Map<string, {
mapStateToProps: MapStateToProps<any>;
@ -66,10 +64,6 @@ const runCallbacksThrottled = throttleWithTickEnd(runCallbacks);
let forceOnHeavyAnimation = true;
function runImmediateCallbacks() {
immediateCallbacks.forEach((cb) => cb(currentGlobal));
}
function runCallbacks() {
if (forceOnHeavyAnimation) {
forceOnHeavyAnimation = false;
@ -93,8 +87,6 @@ export function setGlobal(newGlobal?: GlobalState, options?: ActionOptions) {
currentGlobal = newGlobal;
if (!options?.noUpdate) runImmediateCallbacks();
if (options?.forceSyncOnIOs) {
forceOnHeavyAnimation = true;
runCallbacks();
@ -225,14 +217,14 @@ export function addActionHandler(name: ActionNames, handler: ActionHandler) {
actionHandlers[name].push(handler);
}
export function addCallback(cb: Function, isImmediate = false) {
(isImmediate ? immediateCallbacks : callbacks).push(cb);
export function addCallback(cb: Function) {
callbacks.push(cb);
}
export function removeCallback(cb: Function, isImmediate = false) {
const index = (isImmediate ? immediateCallbacks : callbacks).indexOf(cb);
export function removeCallback(cb: Function) {
const index = callbacks.indexOf(cb);
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 { 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 {
callApiLocal,
@ -157,6 +157,10 @@ export function subscribeToMultitabBroadcastChannel() {
return;
}
if (prevGlobal === global) {
return;
}
if (!prevGlobal) {
prevGlobal = global;
channel.postMessage({
@ -165,6 +169,7 @@ export function subscribeToMultitabBroadcastChannel() {
});
return;
}
const diff = deepDiff(prevGlobal, global);
if (typeof diff !== 'symbol') {
@ -175,7 +180,7 @@ export function subscribeToMultitabBroadcastChannel() {
}
prevGlobal = global;
}, true);
});
channel.addEventListener('message', handleMessage);
}
@ -198,11 +203,12 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) {
const { diff } = data;
const oldGlobal = getGlobal();
const global = deepMerge(oldGlobal, diff);
// @ts-ignore
global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId;
setGlobal(global, { noUpdate: true });
prevGlobal = global;
setGlobal(global);
break;
}
@ -211,8 +217,8 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) {
const oldGlobal = getGlobal();
// @ts-ignore
data.global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId;
setGlobal(data.global, { noUpdate: true });
prevGlobal = data.global;
setGlobal(data.global);
if (resolveGlobalPromise) {
resolveGlobalPromise();
resolveGlobalPromise = undefined;