mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
fix: avoid retaining desktop updater downloads (#1491)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes # #### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
This commit is contained in:
2 files changed
+92
-47
No files matched your search
@@ -4,7 +4,6 @@ import { ShareTargetFeature } from '$features/share-target/ShareTargetFeature';
|
||||
import { useIncomingCallSignaling } from '$features/call/useCallSignaling';
|
||||
import { MatrixRTCSessionProvider } from '$hooks/useMatrixRTCSession';
|
||||
import { BackgroundNotifications } from './BackgroundNotifications';
|
||||
import { DesktopUpdater } from './DesktopUpdater';
|
||||
import { WebUpdater } from './WebUpdater';
|
||||
import { NotificationTransportRuntimeFeature } from '$features/settings/notifications/NotificationTransportRuntimeFeature';
|
||||
import {
|
||||
@@ -55,7 +54,6 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
|
||||
<NativeNotificationClickRouting />
|
||||
<NativeNotificationActionRouting />
|
||||
<BackgroundNotifications />
|
||||
<DesktopUpdater />
|
||||
<WebUpdater />
|
||||
<NotificationTransportRuntimeFeature />
|
||||
<SyncNotificationSettingsWithServiceWorker />
|
||||
|
||||
@@ -27,11 +27,21 @@ export function DesktopUpdater() {
|
||||
const setLastChecked = useSetAtom(desktopUpdateLastCheckedAtom);
|
||||
const [updateInfo, setUpdateInfo] = useState<Update | null>(null);
|
||||
const [isDownloaded, setIsDownloaded] = useState(false);
|
||||
const [isDownloading, setIsDownloading] = useState(false);
|
||||
const [isInstalled, setIsInstalled] = useState(false);
|
||||
const [isInstalling, setIsInstalling] = useState(false);
|
||||
const [dismissed, setDismissed] = useState(false);
|
||||
const [useCustomTitleBar] = useDesktopSetting('useCustomTitleBar');
|
||||
const hasUpdateRef = useRef(false);
|
||||
const runningRef = useRef(false);
|
||||
const downloadedRef = useRef(false);
|
||||
const pendingUpdateRef = useRef<Update | null>(null);
|
||||
const installStartedRef = useRef(false);
|
||||
|
||||
const closePendingUpdate = useCallback((update: Update | null) => {
|
||||
if (!update || fakeDesktopUpdate()) return;
|
||||
void update.close().catch((err) => log.error('Failed to release desktop update', err));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isDesktopTauri()) return undefined;
|
||||
@@ -39,22 +49,18 @@ export function DesktopUpdater() {
|
||||
|
||||
let mounted = true;
|
||||
|
||||
setPhase({ type: 'checking' });
|
||||
if (!downloadedRef.current) {
|
||||
setPhase({ type: 'checking' });
|
||||
}
|
||||
|
||||
async function run() {
|
||||
if (runningRef.current || downloadedRef.current || installStartedRef.current) return;
|
||||
runningRef.current = true;
|
||||
try {
|
||||
if (fakeDesktopUpdate()) {
|
||||
log.log('Fake update: simulating download');
|
||||
log.log('Fake update: simulating available update');
|
||||
setUpdateInfo({ version: '9.9.9', body: 'Fake changelog for testing.' } as Update);
|
||||
setPhase({ type: 'downloading', progress: 0 });
|
||||
for (let pct = 0; pct <= 100; pct += 2) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await new Promise((r) => setTimeout(r, 40));
|
||||
if (!mounted) return;
|
||||
setPhase({ type: 'downloading', progress: pct });
|
||||
}
|
||||
if (!mounted) return;
|
||||
setIsDownloaded(true);
|
||||
setIsDownloaded(false);
|
||||
setPhase({ type: 'ready', version: '9.9.9' });
|
||||
setLastChecked(new Date().toISOString());
|
||||
if (!hasCustomDesktopTitlebar(useCustomTitleBar)) setBannerVisible(true);
|
||||
@@ -63,38 +69,30 @@ export function DesktopUpdater() {
|
||||
|
||||
const { check } = await import('@tauri-apps/plugin-updater');
|
||||
const update = await check();
|
||||
if (!mounted) return;
|
||||
if (!mounted) {
|
||||
closePendingUpdate(update);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!update) {
|
||||
if (!hasUpdateRef.current) {
|
||||
setPhase({ type: 'idle' });
|
||||
}
|
||||
closePendingUpdate(pendingUpdateRef.current);
|
||||
pendingUpdateRef.current = null;
|
||||
setUpdateInfo(null);
|
||||
setIsDownloaded(false);
|
||||
hasUpdateRef.current = false;
|
||||
setPhase({ type: 'idle' });
|
||||
setLastChecked(new Date().toISOString());
|
||||
return;
|
||||
}
|
||||
|
||||
log.log(`Desktop update ${update.version} available, downloading...`);
|
||||
closePendingUpdate(pendingUpdateRef.current);
|
||||
pendingUpdateRef.current = update;
|
||||
log.log(`Desktop update ${update.version} available`);
|
||||
setUpdateInfo(update);
|
||||
setIsDownloaded(false);
|
||||
setIsInstalled(false);
|
||||
setDismissed(false);
|
||||
hasUpdateRef.current = true;
|
||||
setPhase({ type: 'downloading', progress: 0 });
|
||||
|
||||
let downloadedBytes = 0;
|
||||
let contentLength = 0;
|
||||
await update.download((event) => {
|
||||
if (event.event === 'Started') {
|
||||
contentLength = event.data.contentLength ?? 0;
|
||||
} else if (event.event === 'Progress') {
|
||||
downloadedBytes += event.data.chunkLength;
|
||||
const pct = contentLength > 0 ? Math.round((downloadedBytes / contentLength) * 100) : 0;
|
||||
setPhase({ type: 'downloading', progress: Math.min(pct, 100) });
|
||||
}
|
||||
});
|
||||
|
||||
if (!mounted) return;
|
||||
log.log(`Update ${update.version} downloaded`);
|
||||
setIsDownloaded(true);
|
||||
setPhase({ type: 'ready', version: update.version });
|
||||
setLastChecked(new Date().toISOString());
|
||||
if (!hasCustomDesktopTitlebar(useCustomTitleBar)) setBannerVisible(true);
|
||||
@@ -106,6 +104,8 @@ export function DesktopUpdater() {
|
||||
}
|
||||
setLastChecked(new Date().toISOString());
|
||||
}
|
||||
} finally {
|
||||
runningRef.current = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,11 +116,19 @@ export function DesktopUpdater() {
|
||||
return () => {
|
||||
mounted = false;
|
||||
clearInterval(interval);
|
||||
if (!installStartedRef.current) {
|
||||
closePendingUpdate(pendingUpdateRef.current);
|
||||
pendingUpdateRef.current = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return () => {
|
||||
mounted = false;
|
||||
if (!installStartedRef.current) {
|
||||
closePendingUpdate(pendingUpdateRef.current);
|
||||
pendingUpdateRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [
|
||||
autoUpdateCheck,
|
||||
@@ -129,6 +137,7 @@ export function DesktopUpdater() {
|
||||
setBannerVisible,
|
||||
setLastChecked,
|
||||
useCustomTitleBar,
|
||||
closePendingUpdate,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -138,25 +147,52 @@ export function DesktopUpdater() {
|
||||
}, [bannerVisible, dismissed]);
|
||||
|
||||
const handleInstall = useCallback(async () => {
|
||||
if (!updateInfo) return;
|
||||
if (!updateInfo || installStartedRef.current) return;
|
||||
installStartedRef.current = true;
|
||||
try {
|
||||
setIsInstalling(true);
|
||||
setPhase({ type: 'installing' });
|
||||
setIsDownloading(!isDownloaded);
|
||||
if (!isDownloaded) setPhase({ type: 'downloading', progress: 0 });
|
||||
|
||||
if (fakeDesktopUpdate()) {
|
||||
await new Promise((r) => setTimeout(r, 1500));
|
||||
} else {
|
||||
await updateInfo.install();
|
||||
for (let pct = 0; pct <= 100; pct += 2) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await new Promise((r) => setTimeout(r, 40));
|
||||
setPhase({ type: 'downloading', progress: pct });
|
||||
}
|
||||
} else if (!isDownloaded) {
|
||||
let downloadedBytes = 0;
|
||||
let contentLength = 0;
|
||||
await updateInfo.download((event) => {
|
||||
if (event.event === 'Started') {
|
||||
contentLength = event.data.contentLength ?? 0;
|
||||
} else if (event.event === 'Progress') {
|
||||
downloadedBytes += event.data.chunkLength;
|
||||
const pct = contentLength > 0 ? Math.round((downloadedBytes / contentLength) * 100) : 0;
|
||||
setPhase({ type: 'downloading', progress: Math.min(pct, 100) });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setIsDownloading(false);
|
||||
setIsDownloaded(true);
|
||||
downloadedRef.current = true;
|
||||
setIsInstalling(true);
|
||||
setPhase({ type: 'installing' });
|
||||
if (fakeDesktopUpdate()) await new Promise((r) => setTimeout(r, 1500));
|
||||
else await updateInfo.install();
|
||||
|
||||
setPhase({ type: 'ready', version: updateInfo.version });
|
||||
setIsInstalling(false);
|
||||
setIsInstalled(true);
|
||||
pendingUpdateRef.current = null;
|
||||
closePendingUpdate(updateInfo);
|
||||
} catch (err) {
|
||||
log.error('Failed to install update', err);
|
||||
setIsDownloading(false);
|
||||
setIsInstalling(false);
|
||||
installStartedRef.current = false;
|
||||
}
|
||||
}, [updateInfo, setPhase]);
|
||||
}, [closePendingUpdate, isDownloaded, updateInfo, setPhase]);
|
||||
|
||||
const handleRestart = useCallback(async () => {
|
||||
try {
|
||||
@@ -170,7 +206,13 @@ export function DesktopUpdater() {
|
||||
const handleDismiss = useCallback(() => {
|
||||
setDismissed(true);
|
||||
setBannerVisible(false);
|
||||
}, [setBannerVisible]);
|
||||
if (!installStartedRef.current) {
|
||||
closePendingUpdate(pendingUpdateRef.current);
|
||||
pendingUpdateRef.current = null;
|
||||
setUpdateInfo(null);
|
||||
setIsDownloaded(false);
|
||||
}
|
||||
}, [closePendingUpdate, setBannerVisible]);
|
||||
|
||||
const bannerData = useMemo<GlobalBanner | null>(() => {
|
||||
if (!bannerVisible || !updateInfo || dismissed) return null;
|
||||
@@ -195,16 +237,20 @@ export function DesktopUpdater() {
|
||||
};
|
||||
}
|
||||
|
||||
if (!isDownloaded) return null;
|
||||
|
||||
return {
|
||||
id: 'desktop-update-ready',
|
||||
priority: 200,
|
||||
icon: ArrowUp,
|
||||
title: 'Update Available',
|
||||
description: `Sable ${updateInfo.version} is ready to install.${updateInfo.body ? `\n${updateInfo.body}` : ''}`,
|
||||
description: `Sable ${updateInfo.version} is available.${updateInfo.body ? `\n${updateInfo.body}` : ''}`,
|
||||
primaryAction: {
|
||||
label: isInstalling ? 'Installing...' : 'Install & Update',
|
||||
label: isDownloading
|
||||
? 'Downloading...'
|
||||
: isInstalling
|
||||
? 'Installing...'
|
||||
: isDownloaded
|
||||
? 'Install & Update'
|
||||
: 'Download & Install',
|
||||
variant: 'Primary',
|
||||
onClick: handleInstall,
|
||||
},
|
||||
@@ -219,6 +265,7 @@ export function DesktopUpdater() {
|
||||
updateInfo,
|
||||
dismissed,
|
||||
isDownloaded,
|
||||
isDownloading,
|
||||
isInstalled,
|
||||
isInstalling,
|
||||
handleInstall,
|
||||
|
||||
Reference in new issue
Block a user