From 4fed4eff8f4c5a5a75d23d7a2b165b03aa423b8b Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Sun, 20 Oct 2024 18:53:23 +0200 Subject: [PATCH] [dev] Fix outdated global check (#5051) --- src/lib/teact/teactn.tsx | 20 ++++++++------------ src/util/multitab.ts | 4 ++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/lib/teact/teactn.tsx b/src/lib/teact/teactn.tsx index f40247cfa..7352637cb 100644 --- a/src/lib/teact/teactn.tsx +++ b/src/lib/teact/teactn.tsx @@ -25,7 +25,7 @@ interface Container { type GlobalState = AnyLiteral - & { DEBUG_capturedId?: number }; + & { DEBUG_randomId?: number }; type ActionNames = string; type ActionPayload = any; @@ -55,10 +55,10 @@ let currentGlobal = { } as GlobalState; // eslint-disable-next-line @typescript-eslint/naming-convention -let DEBUG_currentCapturedId: number | undefined; +let DEBUG_currentRandomId: number | undefined; // eslint-disable-next-line @typescript-eslint/naming-convention -const DEBUG_releaseCapturedIdThrottled = throttleWithTickEnd(() => { - DEBUG_currentCapturedId = undefined; +const DEBUG_invalidateGlobalOnTickEnd = throttleWithTickEnd(() => { + DEBUG_currentRandomId = Math.random(); }); const actionHandlers: Record = {}; @@ -86,20 +86,16 @@ export function setGlobal(newGlobal?: GlobalState, options?: ActionOptions) { if (DEBUG) { if ( !options?.forceOutdated - && newGlobal.DEBUG_capturedId && newGlobal.DEBUG_capturedId !== DEBUG_currentCapturedId + && newGlobal.DEBUG_randomId && newGlobal.DEBUG_randomId !== DEBUG_currentRandomId ) { throw new Error('[TeactN.setGlobal] Attempt to set an outdated global'); } - DEBUG_currentCapturedId = undefined; + DEBUG_currentRandomId = Math.random(); } currentGlobal = newGlobal; - if (DEBUG) { - DEBUG_currentCapturedId = Math.random(); - } - if (options?.forceSyncOnIOs) { forceOnHeavyAnimation = true; runCallbacks(); @@ -117,9 +113,9 @@ export function getGlobal() { if (DEBUG) { currentGlobal = { ...currentGlobal, - DEBUG_capturedId: DEBUG_currentCapturedId, + DEBUG_randomId: DEBUG_currentRandomId, }; - DEBUG_releaseCapturedIdThrottled(); + DEBUG_invalidateGlobalOnTickEnd(); } return currentGlobal; diff --git a/src/util/multitab.ts b/src/util/multitab.ts index 11a9328de..98821d9cd 100644 --- a/src/util/multitab.ts +++ b/src/util/multitab.ts @@ -229,7 +229,7 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) { const global = deepMerge(oldGlobal, diff); // @ts-ignore - global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId; + global.DEBUG_randomId = oldGlobal.DEBUG_randomId; currentGlobal = global; setGlobal(global); @@ -240,7 +240,7 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) { if (isFirstGlobalResolved) return; const oldGlobal = getGlobal(); // @ts-ignore - data.global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId; + data.global.DEBUG_randomId = oldGlobal.DEBUG_randomId; currentGlobal = data.global; setGlobal(data.global); if (resolveGlobalPromise) {