fix(sync): only pause background sync on mobile

This commit is contained in:
Erwan Leboucher committed 2026-08-07 16:35:47 +02:00
1 parent 39e0ef2cb4
commit 1f9a9c8cee
4 files changed
+53 -12

No files matched your search

+33 -10
View File
@@ -1,16 +1,25 @@
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { renderHook } from '@testing-library/react';
const { pause, resume, getSlidingSyncManager, listen, listenOff, mockIsTauri, mockCallEmbed } =
vi.hoisted(() => ({
pause: vi.fn<() => void>(),
resume: vi.fn<() => void>(),
getSlidingSyncManager: vi.fn<() => unknown>(),
listen: vi.fn<(_event: string, _cb: () => void) => Promise<() => void>>(),
listenOff: vi.fn<() => void>(),
mockIsTauri: { value: false },
mockCallEmbed: { value: undefined as unknown },
}));
const {
pause,
resume,
getSlidingSyncManager,
listen,
listenOff,
mockIsTauri,
mockIsMobileTauri,
mockCallEmbed,
} = vi.hoisted(() => ({
pause: vi.fn<() => void>(),
resume: vi.fn<() => void>(),
getSlidingSyncManager: vi.fn<() => unknown>(),
listen: vi.fn<(_event: string, _cb: () => void) => Promise<() => void>>(),
listenOff: vi.fn<() => void>(),
mockIsTauri: { value: false },
mockIsMobileTauri: { value: true },
mockCallEmbed: { value: undefined as unknown },
}));
vi.mock('$client/initMatrix', () => ({ getSlidingSyncManager }));
@@ -28,6 +37,8 @@ vi.mock('@tauri-apps/api/event', () => ({
vi.mock('@tauri-apps/api/core', () => ({ isTauri: () => mockIsTauri.value }));
vi.mock('$utils/platform', () => ({ isMobileTauri: () => mockIsMobileTauri.value }));
import { useBackgroundSyncPause } from './useBackgroundSyncPause';
const setVisibility = (state: 'visible' | 'hidden') =>
@@ -42,6 +53,7 @@ describe('useBackgroundSyncPause', () => {
listen.mockReset().mockResolvedValue(listenOff);
getSlidingSyncManager.mockReset().mockReturnValue({ pause, resume });
mockIsTauri.value = false;
mockIsMobileTauri.value = true;
mockCallEmbed.value = undefined;
});
@@ -88,6 +100,17 @@ describe('useBackgroundSyncPause', () => {
expect(resume).toHaveBeenCalled();
});
it('keeps polling when a browser tab is hidden', () => {
mockIsMobileTauri.value = false;
setVisibility('visible');
renderHook(() => useBackgroundSyncPause({ clientRunning: true } as never));
setVisibility('hidden');
document.dispatchEvent(new Event('visibilitychange'));
expect(pause).not.toHaveBeenCalled();
});
it('does nothing without a client', () => {
renderHook(() => useBackgroundSyncPause(undefined));
+2 -1
View File
@@ -4,6 +4,7 @@ import { TauriEvent, listen } from '@tauri-apps/api/event';
import { isTauri } from '@tauri-apps/api/core';
import type { MatrixClient } from '$types/matrix-sdk';
import { getSlidingSyncManager } from '$client/initMatrix';
import { isMobileTauri } from '$utils/platform';
import { callEmbedAtom } from '../state/callEmbed';
/**
@@ -17,7 +18,7 @@ export const useBackgroundSyncPause = (mx: MatrixClient | undefined): void => {
const callActive = callEmbed !== undefined;
useEffect(() => {
if (!mx) return undefined;
if (!mx || !isMobileTauri()) return undefined;
const resume = () => getSlidingSyncManager(mx)?.resume();
const applyVisibility = () => {
+17
View File
@@ -1445,6 +1445,23 @@ describe('SlidingSyncManager pause/resume', () => {
}
});
it('keeps the watchdog silent when the aborted poll emits a lifecycle event', () => {
vi.useFakeTimers();
try {
const manager = makeManager(makeMockMx());
manager.attach();
manager.pause();
mocks.slidingSyncInstance.resend.mockClear();
fireLifecycle(SlidingSyncState.RequestFinished, {});
vi.advanceTimersByTime(DEFAULT_DEADLINE_MS * 3);
expect(mocks.slidingSyncInstance.resend).not.toHaveBeenCalled();
} finally {
vi.useRealTimers();
}
});
it('re-arms the poll watchdog on resume', () => {
vi.useFakeTimers();
try {
+1 -1
View File
@@ -706,7 +706,7 @@ export class SlidingSyncManager {
* `continue`s without emitting a lifecycle event.
*/
private armPollWatchdog(): void {
if (this.disposed) return;
if (this.disposed || this.paused) return;
globalThis.clearTimeout(this.pollWatchdogTimer);
this.pollWatchdogTimer = globalThis.setTimeout(() => {
this.pollWatchdogTimer = undefined;