Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
feat: keep downloads running in background when window is closed (minimize to tray) (#213)
Dhruvil Patel7d535d308aed
diff --git a/e2e/lifecycle.spec.ts b/e2e/lifecycle.spec.tsindex a317783..2b99ce1 100644--- a/e2e/lifecycle.spec.ts+++ b/e2e/lifecycle.spec.ts@@ -232,3 +232,38 @@ test.describe('cancel and remove @smoke', () => {await expect.poll(() => existsSync(join(dirs.userData, 'downloads', id))).toBe(false)})})++test.describe('window close with active downloads @smoke', () => {+ test('closing the window keeps active downloads running and suspends cleanly on quit', async ({+ plexo,+ serve+ }) => {+ test.skip(+ process.platform === 'darwin',+ 'minimize to tray on window close is for Windows and Linux'+ )++ const origin = await serve({ size: SIZE, bytesPerSecond: 256 * 1024 })+ const reached = origin.hold(6 * BLOCK)+ await plexo.start(origin.url(), origin.sha256, { connections: 2 })+ await reached++ const windowState = await plexo.evaluateMain(({ BrowserWindow }) => {+ const win = BrowserWindow.getAllWindows()[0]+ win.close()+ return { isDestroyed: win.isDestroyed(), isVisible: win.isVisible() }+ }, null)++ expect(windowState.isDestroyed, 'window is not destroyed').toBe(false)+ expect(windowState.isVisible, 'window is hidden').toBe(false)+ expect(plexo.alive, 'app is still alive').toBe(true)++ const nextReached = origin.hold(10 * BLOCK)+ await nextReached++ await plexo.relaunch()+ const current = await plexo.currentHttp()+ expect(current?.status).toBe('paused')+ origin.release()+ })+})diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex cd045f5..eb63556 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -391,6 +391,17 @@ export class DownloadManager {await this.configureSettings(settings)}+ /** How many downloads are currently downloading or queued. */+ activeDownloadCount(): number {+ let count = 0+ for (const runtime of this.runtimes.values()) {+ if (runtime.state.status === 'downloading' || runtime.state.status === 'queued') {+ count+++ }+ }+ return count+ }+private async configureSettings(settings: AppSettings): Promise<void> {const previousPreferences = this.networkPreferencesthis.networkPreferences = settings.networkPreferences ?? {}diff --git a/src/main/index.ts b/src/main/index.tsindex 4b642ea..a2955f1 100644--- a/src/main/index.ts+++ b/src/main/index.ts@@ -1,5 +1,14 @@import { electronApp, is, optimizer } from '@electron-toolkit/utils'-import { app, BrowserWindow, Menu, nativeImage, nativeTheme, shell, Tray } from 'electron'+import {+ app,+ BrowserWindow,+ Menu,+ nativeImage,+ nativeTheme,+ Notification,+ shell,+ Tray+} from 'electron'import { join } from 'path'import icon from '../../resources/icon-dark.png?asset'import { registerIpcHandlers } from './ipc/handlers'@@ -26,6 +35,30 @@ let mainWindow: BrowserWindow | null = nulllet downloadManager: DownloadManager | null = nulllet quitAfterSuspending = falselet tray: Tray | null = null+let isQuitting = false+let hasShownTrayNotification = false++function ensureTray(): Tray {+ if (tray && !tray.isDestroyed()) return tray+ tray = new Tray(nativeImage.createFromPath(icon).resize({ width: 16, height: 16 }))+ tray.setToolTip('Plexo')+ tray.on('click', () => bringForward(ensureWindow()))+ tray.on('double-click', () => bringForward(ensureWindow()))+ tray.setContextMenu(+ Menu.buildFromTemplate([+ { label: 'Open Plexo', click: () => bringForward(ensureWindow()) },+ { type: 'separator' },+ {+ label: 'Quit Plexo',+ click: () => {+ isQuitting = true+ app.quit()+ }+ }+ ])+ )+ return tray+}/** Only once the app is ready. */function ensureWindow(): BrowserWindow {@@ -104,6 +137,35 @@ function createWindow(): void {if (!testKnobs.hideWindow) mainWindow?.show()})+ mainWindow.on('session-end', () => {+ isQuitting = true+ })+ mainWindow.on('query-session-end', () => {+ isQuitting = true+ })++ mainWindow.on('close', (event) => {+ if (isQuitting || process.platform === 'darwin') return++ const activeCount = downloadManager?.activeDownloadCount() ?? 0+ const runsForBrowser = keepsRunningForBrowser()++ if (activeCount > 0 || runsForBrowser) {+ event.preventDefault()+ mainWindow?.hide()+ ensureTray()+ if (!hasShownTrayNotification && activeCount > 0) {+ hasShownTrayNotification = true+ if (Notification.isSupported()) {+ new Notification({+ title: 'Plexo is still downloading',+ body: 'Downloads will continue in the background. Open Plexo again, or use its tray icon, to quit.'+ }).show()+ }+ }+ }+ })+mainWindow.on('closed', () => {mainWindow = null})@@ -180,11 +242,18 @@ app.whenReady().then(async () => {startUpdater(() => mainWindow, (await loadSettings()).autoUpdate !== false)app.on('activate', function () {- if (BrowserWindow.getAllWindows().length === 0) createWindow()+ if (mainWindow && !mainWindow.isDestroyed()) {+ if (mainWindow.isMinimized()) mainWindow.restore()+ mainWindow.show()+ mainWindow.focus()+ } else if (BrowserWindow.getAllWindows().length === 0) {+ createWindow()+ }})})app.on('before-quit', (event) => {+ isQuitting = trueif (quitAfterSuspending || !downloadManager) returnevent.preventDefault()@@ -203,17 +272,7 @@ app.on('before-quit', (event) => {app.on('window-all-closed', () => {if (process.platform === 'darwin') return- if (!keepsRunningForBrowser()) return app.quit()- // Kept running in the tray, so the browser extension can still reach it.- if (tray) return- tray = new Tray(nativeImage.createFromPath(icon).resize({ width: 16, height: 16 }))- tray.setToolTip('Plexo')- tray.on('click', () => bringForward(ensureWindow()))- tray.setContextMenu(- Menu.buildFromTemplate([- { label: 'Open Plexo', click: () => bringForward(ensureWindow()) },- { type: 'separator' },- { label: 'Quit Plexo', click: () => app.quit() }- ])- )+ const activeCount = downloadManager?.activeDownloadCount() ?? 0+ if (!keepsRunningForBrowser() && activeCount === 0) return app.quit()+ ensureTray()})