[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 =
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<string, ActionHandler[]> = {};
@ -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;

View File

@ -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) {