Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
add proper pause state
Anmol Kapil9617525c2efb
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex 6f49955..00f2bca 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -285,6 +285,14 @@ export class DownloadManager {if (!runtime || runtime.state.status !== 'downloading') returnruntime.state.status = 'paused'+ runtime.state.speedBytesPerSec = 0+ runtime.state.pausedAt = Date.now()+ for (const chunk of runtime.state.chunks) {+ if (chunk.status !== 'completed') {+ chunk.status = 'paused'+ }+ chunk.speedBytesPerSec = 0+ }for (const chunkRuntime of runtime.chunkRuntimes.values()) {chunkRuntime.controller.abort()}@@ -318,6 +326,11 @@ export class DownloadManager {}runtime.state.status = 'downloading'+ if (runtime.state.pausedAt) {+ runtime.state.totalPausedMs =+ (runtime.state.totalPausedMs || 0) + (Date.now() - runtime.state.pausedAt)+ runtime.state.pausedAt = undefined+ }const pending = runtime.state.chunks.filter((chunk) => chunk.status !== 'completed')for (const chunk of pending) {runtime.speedSamplesByChunk.delete(chunk.id)@@ -334,6 +347,10 @@ export class DownloadManager {returnruntime.state.status = 'cancelled'+ runtime.state.speedBytesPerSec = 0+ for (const chunk of runtime.state.chunks) {+ chunk.speedBytesPerSec = 0+ }for (const chunkRuntime of runtime.chunkRuntimes.values()) {chunkRuntime.controller.abort()}@@ -528,6 +545,7 @@ export class DownloadManager {continue}chunk.status = runtime.state.status === 'paused' ? 'paused' : 'cancelled'+ chunk.speedBytesPerSec = 0break}@@ -555,6 +573,7 @@ export class DownloadManager {await delay(retryDelayMs(attempt), activeController.signal)if (activeController.signal.aborted) {chunk.status = runtime.state.status === 'paused' ? 'paused' : 'cancelled'+ chunk.speedBytesPerSec = 0break}chunk.status = 'downloading'@@ -608,6 +627,9 @@ export class DownloadManager {private pushUpdate(runtime: DownloadRuntime): void {const window = this.getWindow()if (!window || window.isDestroyed()) return+ if (runtime.state.status === 'paused' || runtime.state.status === 'cancelled') {+ runtime.state.speedBytesPerSec = 0+ }window.webContents.send(IpcChannels.downloadUpdated, structuredClone(runtime.state))}diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsxindex 981c2e8..e08472e 100644--- a/src/renderer/src/App.tsx+++ b/src/renderer/src/App.tsx@@ -42,12 +42,18 @@ function App(): React.JSX.Element {let titleBarStatus: TitleBarStatus = { kind: 'none' }if (currentDownload) {- if (currentDownload.status === 'downloading' || currentDownload.status === 'paused') {+ if (currentDownload.status === 'downloading') {screen = <DownloadingScreen download={currentDownload} />titleBarStatus = {kind: 'merged',networkCount: groupChunksByInterface(currentDownload.chunks).length}+ } else if (currentDownload.status === 'paused') {+ screen = <DownloadingScreen download={currentDownload} />+ titleBarStatus = {+ kind: 'paused',+ networkCount: groupChunksByInterface(currentDownload.chunks).length+ }} else if (currentDownload.status === 'completed') {screen = <CompleteScreen download={currentDownload} onNewDownload={handleNewDownload} />} else {diff --git a/src/renderer/src/components/MergeDiagram.tsx b/src/renderer/src/components/MergeDiagram.tsxindex a73f056..cd93199 100644--- a/src/renderer/src/components/MergeDiagram.tsx+++ b/src/renderer/src/components/MergeDiagram.tsx@@ -20,10 +20,12 @@ export interface MergeDiagramNetwork {* directly to the final total speed on the right. */export function MergeDiagram({networks,- muted = false+ muted = false,+ paused = false}: {networks: MergeDiagramNetwork[]muted?: boolean+ paused?: boolean}): React.JSX.Element {const height = Math.max(78, networks.length * ROW_HEIGHT + 10)const midY = height / 2@@ -43,8 +45,9 @@ export function MergeDiagram({d={`M${CURVE_START_X},${y.toFixed(1)} C${CURVE_START_X + 36},${y.toFixed(1)} ${MERGE_X - 26},${midY.toFixed(1)} ${MERGE_X},${midY.toFixed(1)}`}stroke={color}strokeDasharray="6 8"+ opacity={paused ? 0.55 : 1}style={- muted+ muted || paused? undefined: { animation: `plexo-dash ${1.1 + index * 0.2}s linear infinite` }}@@ -53,13 +56,15 @@ export function MergeDiagram({})}<pathd={`M${MERGE_X},${midY} L${STREAM_END_X},${midY}`}- stroke={muted ? 'var(--icon-muted)' : 'var(--text)'}+ stroke={muted ? 'var(--icon-muted)' : paused ? 'var(--text-secondary)' : 'var(--text)'}strokeWidth={6.5}+ opacity={paused ? 0.6 : 1}/></g><polygonpoints={`${STREAM_END_X - 1},${midY - 4.5} ${STREAM_END_X + 6},${midY} ${STREAM_END_X - 1},${midY + 4.5}`}- fill={muted ? 'var(--icon-muted)' : 'var(--text)'}+ fill={muted ? 'var(--icon-muted)' : paused ? 'var(--text-secondary)' : 'var(--text)'}+ opacity={paused ? 0.6 : 1}/><circlecx={MERGE_X}@@ -120,10 +125,13 @@ export function MergeDiagram({x={STREAM_END_X + 2}y={midY - 11}textAnchor="end"- fill="var(--text-tertiary)"- style={{ font: `500 8.5px ${FONT_MONO}`, letterSpacing: '0.12em' }}+ fill={paused ? 'var(--color-usb)' : 'var(--text-tertiary)'}+ style={{+ font: `${paused ? '600' : '500'} 8.5px ${FONT_MONO}`,+ letterSpacing: '0.12em'+ }}>- MERGED+ {paused ? 'PAUSED' : 'MERGED'}</text>)}</svg>diff --git a/src/renderer/src/components/NetworkRow.tsx b/src/renderer/src/components/NetworkRow.tsxindex d4321e1..67870d1 100644--- a/src/renderer/src/components/NetworkRow.tsx+++ b/src/renderer/src/components/NetworkRow.tsx@@ -117,7 +117,7 @@ export function NetworkRow({const isChunkDone = chunk.status === 'completed'const isChunkActive = chunk.status === 'downloading'const isChunkError = chunk.status === 'error'- const title = `Stream #${index + 1}: ${Math.round(percent)}% · ${formatBytes(chunk.bytesDownloaded)} / ${formatBytes(chunkSize)}${chunk.speedBytesPerSec > 0 ? ` · ${formatSpeed(chunk.speedBytesPerSec)}` : ''}`+ const title = `Stream #${index + 1}: ${Math.round(percent)}% · ${formatBytes(chunk.bytesDownloaded)} / ${formatBytes(chunkSize)}${chunk.speedBytesPerSec > 0 ? ` · ${formatSpeed(chunk.speedBytesPerSec)}` : chunk.status === 'paused' ? ' · Paused' : ''}`return (<div@@ -291,7 +291,9 @@ export function NetworkRow({? 'Done': chunk.status === 'retrying'? 'Retrying…'- : 'Waiting'}+ : chunk.status === 'paused'+ ? 'Paused'+ : 'Waiting'}</div><divdiff --git a/src/renderer/src/components/TitleBar.tsx b/src/renderer/src/components/TitleBar.tsxindex 4068c6c..a81bfd7 100644--- a/src/renderer/src/components/TitleBar.tsx+++ b/src/renderer/src/components/TitleBar.tsx@@ -1,7 +1,10 @@import { FONT_UI, TITLE_BAR_HEIGHT, pillStyle } from '../theme'export type TitleBarStatus =- { kind: 'none' } | { kind: 'merged'; networkCount: number } | { kind: 'offline' }+ | { kind: 'none' }+ | { kind: 'merged'; networkCount: number }+ | { kind: 'paused'; networkCount: number }+ | { kind: 'offline' }const isMac = window.plexo.platform === 'darwin'@@ -59,6 +62,19 @@ export function TitleBar({ status }: { status: TitleBarStatus }): React.JSX.Elem{status.networkCount} {status.networkCount === 1 ? 'network' : 'networks'} merged</div>)}+ {status.kind === 'paused' && (+ <div style={pillStyle('paused')}>+ <div+ style={{+ width: 6,+ height: 6,+ borderRadius: '50%',+ background: 'var(--color-usb)'+ }}+ />+ {status.networkCount} {status.networkCount === 1 ? 'network' : 'networks'} · Paused+ </div>+ )}{status.kind === 'offline' && (<div style={pillStyle('negative')}><divdiff --git a/src/renderer/src/screens/DownloadingScreen.tsx b/src/renderer/src/screens/DownloadingScreen.tsxindex e364625..5d62636 100644--- a/src/renderer/src/screens/DownloadingScreen.tsx+++ b/src/renderer/src/screens/DownloadingScreen.tsx@@ -13,6 +13,7 @@ import {footerStyle,footerTextStyle,networkTableHeaderStyle,+ primaryButtonStyle,resolveNetworkVisual,secondaryButtonStyle,sectionHeaderLabelStyle,@@ -59,25 +60,32 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Reconst speedHistoryByInterface = useAppStore((store) => store.speedHistoryByInterface)const peakSpeedBytesPerSec = useAppStore((store) => store.peakSpeedBytesPerSec)const networkPreferences = useAppStore((store) => store.networkPreferences)+ const isPaused = download.status === 'paused'+ const percent = formatPercent(download.bytesDownloaded, download.totalBytes)+ const knownSize = download.totalBytes > 0const [now, setNow] = useState(() => Date.now())useEffect(() => {+ if (isPaused) returnconst interval = setInterval(() => setNow(Date.now()), 1000)return () => clearInterval(interval)- }, [])-- const percent = formatPercent(download.bytesDownloaded, download.totalBytes)- const knownSize = download.totalBytes > 0- const isPaused = download.status === 'paused'+ }, [isPaused])useEffect(() => {- document.title = knownSize ? `Plexo — ${percent}%` : 'Plexo — downloading'+ if (isPaused) {+ document.title = knownSize ? `Plexo — Paused (${percent}%)` : 'Plexo — Paused'+ } else {+ document.title = knownSize ? `Plexo — ${percent}%` : 'Plexo — downloading'+ }return () => {document.title = 'Plexo'}- }, [percent, knownSize])+ }, [percent, knownSize, isPaused])- const elapsedSeconds = (now - download.startedAt) / 1000+ const totalPausedMs =+ (download.totalPausedMs || 0) ++ (isPaused && download.pausedAt ? Math.max(0, now - download.pausedAt) : 0)+ const elapsedSeconds = Math.max(0, (now - download.startedAt - totalPausedMs) / 1000)const handlePauseResume = (): void => {if (isPaused) void window.plexo.resumeDownload(download.id)@@ -85,7 +93,8 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re}const handleCancel = (): void => void window.plexo.cancelDownload(download.id)- const speed = splitFormattedBytes(download.speedBytesPerSec)+ const effectiveSpeed = isPaused ? 0 : download.speedBytesPerSec+ const speed = splitFormattedBytes(effectiveSpeed)const groups = groupChunksByInterface(download.chunks)const visuals = groups.map((group) =>resolveNetworkVisual(@@ -98,7 +107,7 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Regroup.chunks.some((chunk) => chunk.status === 'downloading'))const weights = groups.map((group) =>- download.speedBytesPerSec > 0 ? group.speedBytesPerSec : group.bytesDownloaded+ effectiveSpeed > 0 ? group.speedBytesPerSec : group.bytesDownloaded)const totalWeight = weights.reduce((sum, weight) => sum + weight, 0) || 1@@ -119,7 +128,7 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Reborder: string}const chipOptions: SpeedChipOption[] = []- if (groups.length > 1) {+ if (groups.length > 1 && !isPaused) {const sortedIndices = groups.map((_, i) => i).filter((i) =>@@ -180,6 +189,7 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Relabel: visuals[index].name,speedBytesPerSec: isPaused ? 0 : group.speedBytesPerSec}))}+ paused={isPaused}/><div@@ -195,25 +205,45 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Restyle={{font: `500 10px/1 ${FONT_MONO}`,letterSpacing: '0.2em',- color: '#8d9196'+ color: '#8d9196',+ display: 'flex',+ alignItems: 'center',+ gap: 6}}>- TOTAL SPEED+ <span>TOTAL SPEED</span>+ {isPaused && (+ <span+ style={{+ font: `600 9px/1 ${FONT_MONO}`,+ letterSpacing: '0.08em',+ color: 'var(--color-usb)',+ background: 'var(--color-usb-bg)',+ border: '0.5px solid var(--color-usb-border)',+ padding: '2px 5px',+ borderRadius: 3+ }}+ >+ PAUSED+ </span>+ )}</div><div style={{ display: 'flex', alignItems: 'baseline', gap: 7 }}><divstyle={{font: `600 38px/0.88 ${FONT_MONO}`,letterSpacing: '-0.03em',- color: '#f5f2ed',+ color: isPaused ? 'var(--text-tertiary)' : '#f5f2ed',fontVariantNumeric: 'tabular-nums'}}>- {speed.value}- </div>- <div style={{ font: `500 12px/1 ${FONT_MONO}`, color: '#8d9196' }}>- {speed.unit}/s+ {isPaused ? '—' : speed.value}</div>+ {!isPaused && (+ <div style={{ font: `500 12px/1 ${FONT_MONO}`, color: '#8d9196' }}>+ {speed.unit}/s+ </div>+ )}</div><divstyle={{@@ -239,31 +269,52 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re</span></span></div>- {activeChipOption && (- <button- type="button"- onClick={() => setChipModeIndex((i) => (i + 1) % chipOptions.length)}- title={`${activeChipOption.tooltip}${chipOptions.length > 1 ? ' (click to toggle)' : ''}`}+ {isPaused ? (+ <divstyle={{- ...accentChipStyle,- display: 'inline-flex',- alignItems: 'center',- gap: 5,- cursor: chipOptions.length > 1 ? 'pointer' : 'default',- userSelect: 'none',- border: `0.5px solid ${activeChipOption.border}`,- background: activeChipOption.bg,- color: activeChipOption.color,- width: 'fit-content'+ font: `500 11px/1.2 ${FONT_UI}`,+ color: 'var(--text-tertiary)',+ marginTop: 2}}>- <span>{activeChipOption.label}</span>- {chipOptions.length > 1 && <span style={{ opacity: 0.55, fontSize: 8.5 }}>⇄</span>}- </button>+ Download paused · Click Resume to continue+ </div>+ ) : (+ activeChipOption && (+ <button+ type="button"+ onClick={() => setChipModeIndex((i) => (i + 1) % chipOptions.length)}+ title={`${activeChipOption.tooltip}${chipOptions.length > 1 ? ' (click to toggle)' : ''}`}+ style={{+ ...accentChipStyle,+ display: 'inline-flex',+ alignItems: 'center',+ gap: 5,+ cursor: chipOptions.length > 1 ? 'pointer' : 'default',+ userSelect: 'none',+ border: `0.5px solid ${activeChipOption.border}`,+ background: activeChipOption.bg,+ color: activeChipOption.color,+ width: 'fit-content'+ }}+ >+ <span>{activeChipOption.label}</span>+ {chipOptions.length > 1 && (+ <span style={{ opacity: 0.55, fontSize: 8.5 }}>⇄</span>+ )}+ </button>+ ))}</div>- <div style={{ flex: 1, minWidth: 0 }}>+ <div+ style={{+ flex: 1,+ minWidth: 0,+ opacity: isPaused ? 0.45 : 1,+ transition: 'opacity 0.2s'+ }}+ ><divstyle={{font: `500 9.5px/1 ${FONT_MONO}`,@@ -271,7 +322,7 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Recolor: '#8d9196'}}>- THROUGHPUT · LAST {speedHistory.length}S+ THROUGHPUT · {isPaused ? 'PAUSED' : `LAST ${speedHistory.length}S`}</div><ThroughputChartorder={groups.map((g, i) => ({@@ -319,15 +370,34 @@ export function DownloadingScreen({ download }: { download: DownloadState }): RemarginTop: 2,font: `11.5px/1 ${FONT_MONO}`,color: 'var(--text-secondary)',- fontVariantNumeric: 'tabular-nums'+ fontVariantNumeric: 'tabular-nums',+ display: 'flex',+ alignItems: 'center',+ gap: 6}}>- {formatBytes(download.bytesDownloaded)}- {knownSize ? ` of ${formatBytes(download.totalBytes)} · ${percent}%` : ''}- {!isPaused && knownSize- ? ` · ${formatEta(remainingBytes, download.speedBytesPerSec)} left`- : ''}- {isPaused ? ' · paused' : ''}+ <span>+ {formatBytes(download.bytesDownloaded)}+ {knownSize ? ` of ${formatBytes(download.totalBytes)} · ${percent}%` : ''}+ {!isPaused && knownSize+ ? ` · ${formatEta(remainingBytes, effectiveSpeed)} left`+ : ''}+ </span>+ {isPaused && (+ <span+ style={{+ font: `600 9px/1 ${FONT_MONO}`,+ letterSpacing: '0.08em',+ color: 'var(--color-usb)',+ background: 'var(--color-usb-bg)',+ border: '0.5px solid var(--color-usb-border)',+ padding: '2px 6px',+ borderRadius: 3+ }}+ >+ PAUSED+ </span>+ )}</div></div></div>@@ -372,11 +442,10 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re{ label: 'Total', value: knownSize ? formatBytes(download.totalBytes) : '—' },{label: 'ETA',- value:- !isPaused && knownSize ? formatEta(remainingBytes, download.speedBytesPerSec) : '—'+ value: !isPaused && knownSize ? formatEta(remainingBytes, effectiveSpeed) : '—'},{ label: 'Elapsed', value: formatDuration(elapsedSeconds) },- { label: 'Chunks', value: `${completedChunks} / ${download.chunks.length}` }+ { label: 'Completed', value: `${completedChunks} / ${download.chunks.length} done` }].map((stat, index) => (<divkey={stat.label}@@ -415,7 +484,8 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re><div style={sectionHeaderLabelStyle}>Networks</div><div style={sectionHeaderMetaStyle}>- {groups.length} merged · {download.chunks.length} chunks · {activeGroups.length} active+ {groups.length} merged · {download.chunks.length} chunks ·{' '}+ {isPaused ? 'paused' : `${activeGroups.length} active`}</div></div><div style={networkTableHeaderStyle}>@@ -444,7 +514,11 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re{toDisplayPath(dirnameOf(download.destinationPath), homeDir)}</div><div style={{ flex: 1 }} />- <button type="button" onClick={handlePauseResume} style={secondaryButtonStyle}>+ <button+ type="button"+ onClick={handlePauseResume}+ style={isPaused ? primaryButtonStyle : secondaryButtonStyle}+ >{isPaused ? 'Resume' : 'Pause'}</button><button type="button" onClick={handleCancel} style={dangerButtonStyle}>diff --git a/src/renderer/src/screens/ErrorScreen.tsx b/src/renderer/src/screens/ErrorScreen.tsxindex db737cf..dfe23b8 100644--- a/src/renderer/src/screens/ErrorScreen.tsx+++ b/src/renderer/src/screens/ErrorScreen.tsx@@ -83,13 +83,36 @@ export function ErrorScreen({>{cancelled ? (<svg viewBox="0 0 24 24" style={{ width: 22, height: 22 }}>- <circle cx="12" cy="12" r="9" fill="none" stroke="var(--color-usb-text)" strokeWidth="2" />- <line x1="8" y1="12" x2="16" y2="12" stroke="var(--color-usb-text)" strokeWidth="2.4" strokeLinecap="round" />+ <circle+ cx="12"+ cy="12"+ r="9"+ fill="none"+ stroke="var(--color-usb-text)"+ strokeWidth="2"+ />+ <line+ x1="8"+ y1="12"+ x2="16"+ y2="12"+ stroke="var(--color-usb-text)"+ strokeWidth="2.4"+ strokeLinecap="round"+ /></svg>) : (<svg viewBox="0 0 24 24" style={{ width: 22, height: 22 }}><circle cx="12" cy="12" r="9" fill="none" stroke={DANGER} strokeWidth="2" />- <line x1="12" y1="8" x2="12" y2="12.5" stroke={DANGER} strokeWidth="2.4" strokeLinecap="round" />+ <line+ x1="12"+ y1="8"+ x2="12"+ y2="12.5"+ stroke={DANGER}+ strokeWidth="2.4"+ strokeLinecap="round"+ /><circle cx="12" cy="15.5" r="1.2" fill={DANGER} /></svg>)}@@ -162,7 +185,9 @@ export function ErrorScreen({{download.bytesDownloaded > 0 ? (<>{formatBytes(download.bytesDownloaded)}- {knownSize ? ` of ${formatBytes(download.totalBytes)} (${percent}%)` : ' transferred'}+ {knownSize+ ? ` of ${formatBytes(download.totalBytes)} (${percent}%)`+ : ' transferred'}</>) : ('No data transferred'@@ -172,7 +197,15 @@ export function ErrorScreen({</div>{/* Action Buttons in Center */}- <div style={{ display: 'flex', gap: 10, width: '100%', justifyContent: 'center', marginTop: 4 }}>+ <div+ style={{+ display: 'flex',+ gap: 10,+ width: '100%',+ justifyContent: 'center',+ marginTop: 4+ }}+ ><buttontype="button"onClick={onDownloadAgain}diff --git a/src/renderer/src/theme.ts b/src/renderer/src/theme.tsindex d8b2a7d..c77c467 100644--- a/src/renderer/src/theme.ts+++ b/src/renderer/src/theme.ts@@ -53,7 +53,10 @@ export const accentChipStyle: React.CSSProperties = {whiteSpace: 'nowrap'}-const PILL_TOKENS: Record<'positive' | 'negative', { bg: string; border: string; text: string }> = {+const PILL_TOKENS: Record<+ 'positive' | 'negative' | 'paused',+ { bg: string; border: string; text: string }+> = {positive: {bg: 'var(--color-wifi-bg)',border: 'var(--color-wifi-border)',@@ -63,10 +66,15 @@ const PILL_TOKENS: Record<'positive' | 'negative', { bg: string; border: string;bg: 'var(--color-danger-bg)',border: 'var(--color-danger-border)',text: 'var(--color-danger)'+ },+ paused: {+ bg: 'var(--color-usb-bg)',+ border: 'var(--color-usb-border)',+ text: 'var(--color-usb-text)'}}-export function pillStyle(kind: 'positive' | 'negative'): React.CSSProperties {+export function pillStyle(kind: 'positive' | 'negative' | 'paused'): React.CSSProperties {const tokens = PILL_TOKENS[kind]return {display: 'flex',diff --git a/src/shared/types.ts b/src/shared/types.tsindex 181f94a..8c8d0f4 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -58,6 +58,8 @@ export interface DownloadState {chunks: ChunkState[]error?: stringstartedAt: number+ pausedAt?: number+ totalPausedMs?: numbercompletedAt?: number}