Multitab: Fix global diff sync
This commit is contained in:
parent
57ad78cd45
commit
5b9675b074
@ -113,24 +113,26 @@ type BroadcastChannelMessage = (
|
|||||||
|
|
||||||
let resolveGlobalPromise: VoidFunction | undefined;
|
let resolveGlobalPromise: VoidFunction | undefined;
|
||||||
let isFirstGlobalResolved = false;
|
let isFirstGlobalResolved = false;
|
||||||
let prevGlobal: GlobalState | undefined;
|
let currentGlobal: GlobalState | undefined;
|
||||||
let isDisabled = false;
|
let isDisabled = false;
|
||||||
|
|
||||||
const channel = IS_MULTITAB_SUPPORTED
|
const channel = IS_MULTITAB_SUPPORTED
|
||||||
? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel
|
? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
let globalToDiffWith: GlobalState | undefined;
|
let isBroadcastDiffScheduled = false;
|
||||||
|
let lastBroadcastDiffGlobal: GlobalState | undefined;
|
||||||
|
|
||||||
function broadcastDiffOnIdle(_globalToDiffWith: GlobalState) {
|
function broadcastDiffOnIdle() {
|
||||||
globalToDiffWith = _globalToDiffWith;
|
if (isBroadcastDiffScheduled) return;
|
||||||
|
|
||||||
|
isBroadcastDiffScheduled = true;
|
||||||
|
lastBroadcastDiffGlobal = currentGlobal;
|
||||||
|
|
||||||
onFullyIdle(() => {
|
onFullyIdle(() => {
|
||||||
if (!channel || !globalToDiffWith) return;
|
if (!channel) return;
|
||||||
|
|
||||||
const diff = deepDiff(globalToDiffWith, prevGlobal);
|
const diff = deepDiff(lastBroadcastDiffGlobal, currentGlobal);
|
||||||
|
|
||||||
globalToDiffWith = undefined;
|
|
||||||
|
|
||||||
if (typeof diff !== 'symbol') {
|
if (typeof diff !== 'symbol') {
|
||||||
channel.postMessage({
|
channel.postMessage({
|
||||||
@ -138,6 +140,8 @@ function broadcastDiffOnIdle(_globalToDiffWith: GlobalState) {
|
|||||||
diff,
|
diff,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isBroadcastDiffScheduled = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,16 +185,16 @@ export function subscribeToMultitabBroadcastChannel() {
|
|||||||
|
|
||||||
addCallback((global: GlobalState) => {
|
addCallback((global: GlobalState) => {
|
||||||
if (!isFirstGlobalResolved || isDisabled) {
|
if (!isFirstGlobalResolved || isDisabled) {
|
||||||
prevGlobal = global;
|
currentGlobal = global;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevGlobal === global) {
|
if (currentGlobal === global) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!prevGlobal) {
|
if (!currentGlobal) {
|
||||||
prevGlobal = global;
|
currentGlobal = global;
|
||||||
channel.postMessage({
|
channel.postMessage({
|
||||||
type: 'globalUpdate',
|
type: 'globalUpdate',
|
||||||
global,
|
global,
|
||||||
@ -198,9 +202,9 @@ export function subscribeToMultitabBroadcastChannel() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcastDiffOnIdle(prevGlobal);
|
broadcastDiffOnIdle();
|
||||||
|
|
||||||
prevGlobal = global;
|
currentGlobal = global;
|
||||||
});
|
});
|
||||||
|
|
||||||
channel.addEventListener('message', handleMessage);
|
channel.addEventListener('message', handleMessage);
|
||||||
@ -228,7 +232,7 @@ export function handleMessage({ data }: { data: BroadcastChannelMessage }) {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId;
|
global.DEBUG_capturedId = oldGlobal.DEBUG_capturedId;
|
||||||
|
|
||||||
prevGlobal = global;
|
currentGlobal = global;
|
||||||
setGlobal(global);
|
setGlobal(global);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -238,7 +242,7 @@ 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;
|
||||||
prevGlobal = data.global;
|
currentGlobal = data.global;
|
||||||
setGlobal(data.global);
|
setGlobal(data.global);
|
||||||
if (resolveGlobalPromise) {
|
if (resolveGlobalPromise) {
|
||||||
resolveGlobalPromise();
|
resolveGlobalPromise();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user