[dev] Fix outdated global check (#5051)

This commit is contained in:
zubiden 2024-10-20 18:53:23 +02:00 committed by Alexander Zinchuk
parent dcdad5152e
commit 4fed4eff8f
2 changed files with 10 additions and 14 deletions

View File

@ -25,7 +25,7 @@ interface Container {
type GlobalState = type GlobalState =
AnyLiteral AnyLiteral
& { DEBUG_capturedId?: number }; & { DEBUG_randomId?: number };
type ActionNames = string; type ActionNames = string;
type ActionPayload = any; type ActionPayload = any;
@ -55,10 +55,10 @@ let currentGlobal = {
} as GlobalState; } as GlobalState;
// eslint-disable-next-line @typescript-eslint/naming-convention // 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 // eslint-disable-next-line @typescript-eslint/naming-convention
const DEBUG_releaseCapturedIdThrottled = throttleWithTickEnd(() => { const DEBUG_invalidateGlobalOnTickEnd = throttleWithTickEnd(() => {
DEBUG_currentCapturedId = undefined; DEBUG_currentRandomId = Math.random();
}); });
const actionHandlers: Record<string, ActionHandler[]> = {}; const actionHandlers: Record<string, ActionHandler[]> = {};
@ -86,20 +86,16 @@ export function setGlobal(newGlobal?: GlobalState, options?: ActionOptions) {
if (DEBUG) { if (DEBUG) {
if ( if (
!options?.forceOutdated !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'); throw new Error('[TeactN.setGlobal] Attempt to set an outdated global');
} }
DEBUG_currentCapturedId = undefined; DEBUG_currentRandomId = Math.random();
} }
currentGlobal = newGlobal; currentGlobal = newGlobal;
if (DEBUG) {
DEBUG_currentCapturedId = Math.random();
}
if (options?.forceSyncOnIOs) { if (options?.forceSyncOnIOs) {
forceOnHeavyAnimation = true; forceOnHeavyAnimation = true;
runCallbacks(); runCallbacks();
@ -117,9 +113,9 @@ export function getGlobal() {
if (DEBUG) { if (DEBUG) {
currentGlobal = { currentGlobal = {
...currentGlobal, ...currentGlobal,
DEBUG_capturedId: DEBUG_currentCapturedId, DEBUG_randomId: DEBUG_currentRandomId,
}; };
DEBUG_releaseCapturedIdThrottled(); DEBUG_invalidateGlobalOnTickEnd();
} }
return currentGlobal; return currentGlobal;

View File

@ -229,7 +229,7 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) {
const global = deepMerge(oldGlobal, diff); const global = deepMerge(oldGlobal, diff);
// @ts-ignore // @ts-ignore
global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId; global.DEBUG_randomId = oldGlobal.DEBUG_randomId;
currentGlobal = global; currentGlobal = global;
setGlobal(global); setGlobal(global);
@ -240,7 +240,7 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) {
if (isFirstGlobalResolved) return; if (isFirstGlobalResolved) return;
const oldGlobal = getGlobal(); const oldGlobal = getGlobal();
// @ts-ignore // @ts-ignore
data.global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId; data.global.DEBUG_randomId = oldGlobal.DEBUG_randomId;
currentGlobal = data.global; currentGlobal = data.global;
setGlobal(data.global); setGlobal(data.global);
if (resolveGlobalPromise) { if (resolveGlobalPromise) {