Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
perf: send the window only changed blocks, and save progress compactly
Anmol Kapil5851ec0c9177
diff --git a/e2e/download.spec.ts b/e2e/download.spec.tsindex 665fe20..eba0438 100644--- a/e2e/download.spec.ts+++ b/e2e/download.spec.ts@@ -109,6 +109,16 @@ test.describe('happy paths @smoke', () => {expect(updates.length, 'progress kept coming while it downloaded').toBeGreaterThan(5)})+ test('after the first, updates carry only the blocks that changed', async ({ plexo, serve }) => {+ const origin = await serve({ size: 64 * BLOCK, bytesPerSecond: 500_000 })+ const id = await plexo.start(origin.url(), origin.sha256)+ await plexo.waitForStatus('completed')+ const sent = plexo.updates.at(-1)!.filter((update) => update.state.id === id)+ expect(sent[0].blocks).toHaveLength(64)+ // Two streams move a few blocks between one update and the next, not all 64.+ expect(Math.max(...sent.slice(1).map((update) => update.blocks.length))).toBeLessThan(16)+ })+test('server without range support: one stream, whole file', async ({ plexo, serve }) => {const origin = await serve({ size: 10 * BLOCK, ranges: false })await plexo.start(origin.url(), origin.sha256)diff --git a/e2e/downloadUpdate.spec.ts b/e2e/downloadUpdate.spec.tsnew file mode 100644index 0000000..27ca49e--- /dev/null+++ b/e2e/downloadUpdate.spec.ts@@ -0,0 +1,67 @@+import { expect, test } from '@playwright/test'+import { applyDownloadUpdate } from '../src/shared/downloadUpdate'+import type { BlockState, DownloadUpdate } from '../src/shared/types'++// O. How the window puts a download back together from the updates the main process sends: a+// snapshot, then only the blocks that changed.++const block = (index: number, bytesDownloaded = 0): BlockState => ({+ index,+ rangeStart: index * 100,+ rangeEnd: index * 100 + 99,+ status: bytesDownloaded === 100 ? 'completed' : bytesDownloaded ? 'downloading' : 'pending',+ bytesDownloaded,+ bytesByInterface: bytesDownloaded ? { a: bytesDownloaded } : {}+})++const update = (seq: number, blocks: BlockState[], id = 'd1'): DownloadUpdate => ({+ seq,+ state: {+ id,+ url: 'http://example.test/file',+ fileName: 'file',+ destinationPath: '/tmp/file',+ totalBytes: 400,+ bytesDownloaded: blocks.reduce((sum, entry) => sum + entry.bytesDownloaded, 0),+ speedBytesPerSec: 0,+ status: 'downloading',+ chunks: [],+ totalBlocks: 4,+ startedAt: 0+ },+ blocks+})++const full = (seq: number, id = 'd1'): DownloadUpdate =>+ update(seq, [block(0), block(1), block(2), block(3)], id)++test.describe('download updates', () => {+ test('changed blocks land on the ones already there', () => {+ const start = applyDownloadUpdate(null, full(1))+ const next = applyDownloadUpdate(start, update(2, [block(1, 50)]))!+ expect(next.blocks!.map((entry) => entry.bytesDownloaded)).toEqual([0, 50, 0, 0])+ expect(next.seq).toBe(2)+ // What was there is left untouched.+ expect(start!.blocks![1].bytesDownloaded).toBe(0)+ })++ test('an update sent before a snapshot was taken, arriving after it, is ignored', () => {+ const snapshot = applyDownloadUpdate(+ null,+ update(5, [block(0, 100), block(1, 80), block(2), block(3)])+ )+ expect(applyDownloadUpdate(snapshot, update(4, [block(1, 40)]))).toBe(snapshot)+ // And a snapshot older than what is already there changes nothing.+ const newer = applyDownloadUpdate(snapshot, update(6, [block(1, 100)]))+ expect(applyDownloadUpdate(newer, full(5))).toBe(newer)+ })++ test('a download with no whole picture yet waits for one', () => {+ // A window that has just loaded can be sent a partial update before its snapshot comes back.+ expect(applyDownloadUpdate(null, update(3, [block(2, 10)]))).toBeNull()+ const previous = applyDownloadUpdate(null, full(1, 'old'))+ expect(applyDownloadUpdate(previous, update(3, [block(2, 10)], 'new'))).toBe(previous)+ // A new download's first update has every block, and stands on its own.+ expect(applyDownloadUpdate(previous, full(1, 'new'))?.id).toBe('new')+ })+})diff --git a/e2e/fixtures.ts b/e2e/fixtures.tsindex b6e26e6..a22589d 100644--- a/e2e/fixtures.ts+++ b/e2e/fixtures.ts@@ -13,7 +13,8 @@ import {type Page} from '@playwright/test'import type { IpcContract } from '../src/shared/ipc-contract'-import type { DownloadState, DownloadStatus } from '../src/shared/types'+import { applyDownloadUpdate } from '../src/shared/downloadUpdate'+import type { DownloadState, DownloadStatus, DownloadUpdate } from '../src/shared/types'import { Origin, sha256, type OriginOptions } from './origin'export { expect }@@ -70,6 +71,8 @@ export class PlexoApp {page!: Page/** Every downloadUpdated state, one array per app launch (a relaunch starts a new one). */readonly sessions: DownloadState[][] = []+ /** The same, as the window was sent them: only the blocks that changed. */+ readonly updates: DownloadUpdate[][] = []readonly tracked = new Map<string, Tracked>()readonly output: string[] = []alive = false@@ -118,11 +121,18 @@ export class PlexoApp {this.page = await this.electronApp.firstWindow()await this.page.waitForLoadState('domcontentloaded')const session: DownloadState[] = []+ const updates: DownloadUpdate[] = []this.sessions.push(session)- await this.page.exposeFunction('__plexoRecord', (state: DownloadState) => session.push(state))+ this.updates.push(updates)+ // Kept whole, the way the window puts them together.+ await this.page.exposeFunction('__plexoRecord', (update: DownloadUpdate) => {+ updates.push(update)+ const state = applyDownloadUpdate(session.at(-1) ?? null, update)+ if (state && state !== session.at(-1)) session.push(state)+ })await this.page.evaluate(() => {const w = window as unknown as { __plexoRecord: (s: unknown) => void }- window.plexo.onDownloadUpdated((state) => w.__plexoRecord(state))+ window.plexo.onDownloadUpdated((update) => w.__plexoRecord(update))})return this}@@ -213,7 +223,8 @@ export class PlexoApp {nextDownload: Tracked | null = nullasync current(): Promise<DownloadState | null> {- return this.api.getCurrentDownload()+ const snapshot = await this.api.getCurrentDownload()+ return snapshot && applyDownloadUpdate(null, snapshot)}async waitForStatus(@@ -271,6 +282,11 @@ export function checkEvents(sessions: DownloadState[][]): void {state.totalBytes)}+ expect(+ state.blocks?.every((block, index) => block?.index === index),+ `${label}: every block is there, in order`+ ).toBe(true)+ expect(state.blocks?.length, `${label}: as many blocks as planned`).toBe(state.totalBlocks)for (const block of state.blocks ?? []) {const attributed = Object.values(block.bytesByInterface).reduce((a, b) => a + b, 0)expect(attributed, `${label}: block ${block.index} attribution sums to its bytes`).toBe(diff --git a/e2e/plan.spec.ts b/e2e/plan.spec.tsindex 5f153cd..a760843 100644--- a/e2e/plan.spec.ts+++ b/e2e/plan.spec.ts@@ -3,9 +3,9 @@ import fc from 'fast-check'import {DEFAULT_MAX_BLOCK_BYTES,interleave,- MAX_BLOCKS,MAX_STREAMS_PER_NETWORK,MIN_BLOCK_BYTES,+ planBlocks,planDownload,START_STREAMS_PER_NETWORK} from '../src/main/download/plan'@@ -30,11 +30,9 @@ test.describe('download plan', () => {const { blockSizeBytes, blockCount } = planexpect(blockSizeBytes * blockCount).toBeGreaterThanOrEqual(request.totalBytes)expect(blockSizeBytes * (blockCount - 1)).toBeLessThan(request.totalBytes)- expect(blockCount).toBeLessThanOrEqual(MAX_BLOCKS)expect(blockSizeBytes).toBeGreaterThanOrEqual(Math.min(MIN_BLOCK_BYTES, request.totalBytes))- expect(blockSizeBytes).toBeLessThanOrEqual(- Math.max(DEFAULT_MAX_BLOCK_BYTES, Math.ceil(request.totalBytes / MAX_BLOCKS))- )+ // However big the file: a bigger file gets more blocks, never bigger ones.+ expect(blockSizeBytes).toBeLessThanOrEqual(DEFAULT_MAX_BLOCK_BYTES)}))})@@ -139,6 +137,27 @@ test.describe('download plan', () => {}})+ test('the planned blocks tile the file exactly, in order', () => {+ fc.assert(+ fc.property(+ fc.integer({ min: 1, max: 2 ** 30 }),+ fc.integer({ min: 1, max: 2 ** 24 }),+ (totalBytes, blockSizeBytes) => {+ fc.pre(totalBytes / blockSizeBytes <= 100_000)+ const blocks = planBlocks(totalBytes, blockSizeBytes)+ let next = 0+ blocks.forEach((block, index) => {+ expect(block.index).toBe(index)+ expect(block.rangeStart).toBe(next)+ next = block.rangeEnd! + 1+ })+ expect(next).toBe(totalBytes)+ }+ )+ )+ expect(planBlocks(0, 0)).toMatchObject([{ rangeStart: 0, rangeEnd: null }])+ })+test('interleave keeps each group in order', () => {fc.assert(fc.property(fc.array(fc.tuple(fc.integer({ min: 0, max: 3 }), fc.nat())), (items) => {diff --git a/e2e/recovery.spec.ts b/e2e/recovery.spec.tsindex a8636f3..cf7e425 100644--- a/e2e/recovery.spec.ts+++ b/e2e/recovery.spec.ts@@ -147,6 +147,40 @@ test.describe('persisted state on disk @smoke', () => {expect((await plexo.current())?.error).toMatch(/partial download file is missing/)})+ test('a download saved by the previous version resumes where it was', async ({+ plexo,+ serve,+ dirs+ }) => {+ const { id, origin } = await pausedDownload(plexo, serve)+ const paused = (await plexo.current())!+ await plexo.quit()++ // What version 4 wrote: every block whole, inside the state.+ const manifestPath = join(dirs.userData, 'downloads', id, 'manifest.json')+ const manifest = JSON.parse(await readFile(manifestPath, 'utf-8'))+ delete manifest.progress+ await writeFile(+ manifestPath,+ JSON.stringify({+ ...manifest,+ version: 4,+ state: { ...manifest.state, blocks: paused.blocks }+ })+ )++ await plexo.launch()+ expect((await plexo.current())?.bytesDownloaded).toBe(paused.bytesDownloaded)+ const before = origin.chunkRequests().length+ await plexo.api.resumeDownload(id)+ await plexo.waitForStatus('completed')+ const refetched = origin+ .chunkRequests()+ .slice(before)+ .some((request) => request.range!.start < paused.bytesDownloaded / 2)+ expect(refetched, 'what was already there is not fetched again').toBe(false)+ })+test('a staging file cut short while the app was closed is fetched again', async ({plexo,servediff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex 9a6c85b..6c6c28a 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -9,6 +9,7 @@ import type {ChunkState,DownloadState,DownloadStatus,+ DownloadUpdate,NetworkInterfaceInfo,StartDownloadRequest} from '../../shared/types'@@ -19,7 +20,7 @@ import { downloadChunk, fetchRange, RemoteChangedError } from './chunkDownloaderimport { DownloadFile } from './downloadFile'import { compareVersion, type FileVersion } from './fileVersion'import { ensureDirectory, reserveDestinationPath } from './paths'-import { interleave, MAX_STREAMS_PER_NETWORK, planDownload } from './plan'+import { interleave, MAX_STREAMS_PER_NETWORK, planBlocks, planDownload } from './plan'import { pickWork, type SchedulerPolicy, type Work } from './scheduler'import {compatibleInterfaces,@@ -132,12 +133,14 @@ interface DownloadRuntime {/** Everything each network's streams have received, by network id: what the stream count is* judged by (see concurrency.ts). */receivedByNetwork: Map<string, number>+ /** Updates sent to the window so far (see DownloadUpdate). */+ sentUpdates: number+ /** Each block as the window was last sent it, by index — what tells a changed block apart. */+ sentBlocks: Pick<BlockState, 'status' | 'interfaceId' | 'bytesDownloaded'>[]}-interface PersistedDownload {- version: 4+interface PersistedDownloadBase {savedAt: number- state: DownloadStatepartialPath: stringpublicationPath?: stringpublicationIdentity?: { dev: number; ino: number }@@ -145,6 +148,53 @@ interface PersistedDownload {activeInterfaces: NetworkInterfaceInfo[]}+type PersistedDownload = PersistedDownloadBase &+ (+ | {+ version: 5+ state: Omit<DownloadState, 'blocks'>+ /** Per network id, the bytes it delivered of each block, by block index. Everything else+ * about a block follows from the plan: its range from `blockSizeBytes`, and that it is+ * complete from those bytes adding up to its length. */+ progress: Record<string, number[]>+ }+ /** Written before version 5, with every block saved whole; still read, so an update doesn't+ * lose the progress of a download it finds paused. */+ | { version: 4; state: DownloadState }+ )++/** Per network, the bytes it delivered of each block (see PersistedDownload). */+function progressByNetwork(blocks: readonly BlockState[]): Record<string, number[]> {+ const progress: Record<string, number[]> = {}+ for (const block of blocks) {+ for (const [networkId, bytes] of Object.entries(block.bytesByInterface)) {+ ;(progress[networkId] ??= new Array<number>(blocks.length).fill(0))[block.index] = bytes+ }+ }+ return progress+}++/** The blocks a version 5 manifest describes. */+function restoreBlocks(+ state: Omit<DownloadState, 'blocks'>,+ progress: Record<string, number[]>+): BlockState[] {+ const blocks = planBlocks(state.totalBytes, state.blockSizeBytes ?? state.totalBytes)+ for (const [networkId, column] of Object.entries(progress)) {+ column.forEach((bytes, index) => {+ const block = blocks[index]+ if (!block || !(bytes > 0)) return+ block.bytesByInterface[networkId] = bytes+ block.bytesDownloaded += bytes+ })+ }+ for (const block of blocks) {+ const length = block.rangeEnd === null ? null : block.rangeEnd - block.rangeStart + 1+ if (state.status === 'completed' || block.bytesDownloaded === length) block.status = 'completed'+ }+ return blocks+}+// Set PLEXO_DEBUG=1 to log every request's outcome, how long the server took to answer and// whether it reused a warm connection — what it takes to tell one slow connection from a slow path.const debug: (...args: unknown[]) => void = process.env['PLEXO_DEBUG']@@ -348,12 +398,15 @@ export class DownloadManager {const persisted = JSON.parse(await readFile(this.manifestPath(id), 'utf-8')) as PersistedDownload- if (persisted.version !== 4 || persisted.state.id !== id || !persisted.state.blocks)- return-- const state = persisted.state- const blocks = state.blocks+ if (persisted.state.id !== id) return+ const blocks =+ persisted.version === 5+ ? restoreBlocks(persisted.state, persisted.progress)+ : persisted.version === 4+ ? persisted.state.blocks+ : undefinedif (!blocks) return+ const state: DownloadState = { ...persisted.state, blocks }if (state.status === 'downloading') {state.status = 'paused'state.pausedAt = persisted.savedAt || Date.now()@@ -443,7 +496,9 @@ export class DownloadManager {avoidNetworkByBlock: new Map(),attempts: new Map(),hedgesByBlock: new Map(),- receivedByNetwork: new Map()+ receivedByNetwork: new Map(),+ sentUpdates: 0,+ sentBlocks: []})} catch {// Ignore incomplete or corrupt manifests; other downloads can still be restored.@@ -480,12 +535,15 @@ export class DownloadManager {}}- async getCurrentDownload(): Promise<DownloadState | null> {+ /** A snapshot of the current download: an update with every block in it. */+ async getCurrentDownload(): Promise<DownloadUpdate | null> {await this.initializationconst latest = [...this.runtimes.values()].sort((a, b) => b.state.startedAt - a.state.startedAt)[0]- return latest ? structuredClone(latest.state) : null+ if (!latest) return null+ const { blocks, ...state } = latest.state+ return structuredClone({ seq: latest.sentUpdates, state, blocks: blocks ?? latest.blocks })}/** Plexo shows one download at a time (see useAppStore's currentDownload) — starting a second@@ -552,34 +610,7 @@ export class DownloadManager {// The UI caps how many cells it renders separately (see BlockGrid), by bucketing these// blocks rather than by shrinking their count here.- const blocks: BlockState[] = []- if (requestPayload.totalBytes > 0) {- const { blockSizeBytes } = plan- for (- let rangeStart = 0;- rangeStart < requestPayload.totalBytes;- rangeStart += blockSizeBytes- ) {- blocks.push({- index: blocks.length,- rangeStart,- rangeEnd: Math.min(rangeStart + blockSizeBytes, requestPayload.totalBytes) - 1,- status: 'pending',- bytesDownloaded: 0,- bytesByInterface: {}- })- }- } else {- // Size unknown: one open-ended block, to end of file.- blocks.push({- index: 0,- rangeStart: 0,- rangeEnd: null,- status: 'pending',- bytesDownloaded: 0,- bytesByInterface: {}- })- }+ const blocks = planBlocks(requestPayload.totalBytes, plan.blockSizeBytes)const chunks = plan.streamNetworks.map((networkIndex, id) =>newStream(id, interfaces[networkIndex])@@ -620,7 +651,9 @@ export class DownloadManager {avoidNetworkByBlock: new Map(),attempts: new Map(),hedgesByBlock: new Map(),- receivedByNetwork: new Map()+ receivedByNetwork: new Map(),+ sentUpdates: 0,+ sentBlocks: []}this.runtimes.set(id, runtime)await this.persistNow(runtime)@@ -1662,7 +1695,32 @@ export class DownloadManager {if (runtime.state.status === 'paused' || runtime.state.status === 'cancelled') {runtime.state.speedBytesPerSec = 0}- window.webContents.send(IpcChannels.downloadUpdated, structuredClone(runtime.state))+ window.webContents.send(IpcChannels.downloadUpdated, this.takeUpdate(runtime))+ }++ /** What the window hasn't been sent yet: the download's state, and the blocks that moved. */+ private takeUpdate(runtime: DownloadRuntime): DownloadUpdate {+ const { blocks: all, ...state } = runtime.state+ const sent = runtime.sentBlocks+ const blocks: BlockState[] = []+ for (const block of all ?? runtime.blocks) {+ const last = sent[block.index]+ // bytesByInterface only ever changes along with bytesDownloaded (see blockProgress.ts).+ if (+ last?.status === block.status &&+ last.interfaceId === block.interfaceId &&+ last.bytesDownloaded === block.bytesDownloaded+ ) {+ continue+ }+ sent[block.index] = {+ status: block.status,+ interfaceId: block.interfaceId,+ bytesDownloaded: block.bytesDownloaded+ }+ blocks.push(block)+ }+ return structuredClone({ seq: ++runtime.sentUpdates, state, blocks })}private schedulePersistence(runtime: DownloadRuntime): void {@@ -1687,10 +1745,12 @@ export class DownloadManager {const dir = this.downloadDir(runtime.state.id)const path = this.manifestPath(runtime.state.id)const temporaryPath = `${path}.tmp`+ const { blocks, ...state } = runtime.stateconst persisted: PersistedDownload = {- version: 4,+ version: 5,savedAt: Date.now(),- state: structuredClone(runtime.state),+ state: structuredClone(state),+ progress: progressByNetwork(blocks ?? runtime.blocks),partialPath: runtime.file.path,publicationPath: runtime.publicationPath,publicationIdentity: runtime.publicationIdentity,diff --git a/src/main/download/plan.ts b/src/main/download/plan.tsindex 5227c94..72f4531 100644--- a/src/main/download/plan.ts+++ b/src/main/download/plan.ts@@ -1,20 +1,20 @@+import type { BlockState } from '../../shared/types'+// How a download is cut into blocks and how many streams start on them. Pure, so every rule here// can be checked against exact situations. How many streams a network ends up with is decided// while the download runs (see concurrency.ts); the plan only has to leave them enough blocks.const MIB = 1024 * 1024-/** The largest block, for any file the block-count cap doesn't force bigger. */+/** The largest block. However big the file, a block stays small enough for one stream to finish+ * soon: a racing second attempt re-fetches a block rather than splitting it (see scheduler.ts),+ * so the last block's size is how long the slowest connection can hold the download up. */export const DEFAULT_MAX_BLOCK_BYTES = 8 * MIB/** No block is planned smaller than this: each block is its own request, and below about a* megabyte the round trips cost more than splitting the file saves. */export const MIN_BLOCK_BYTES = MIB-/** Only a safety valve for multi-terabyte files, so the block list stays a sane size. Past it,- * blocks grow instead of multiplying. */-export const MAX_BLOCKS = 4096-/** Streams each network starts with. Servers are built for browsers, which open about six* connections to a host, so four troubles none of them. */export const START_STREAMS_PER_NETWORK = 4@@ -87,13 +87,10 @@ export function planDownload(request: PlanRequest): DownloadPlan {)const targetBlocks = networkCount * MAX_STREAMS_PER_NETWORK * BLOCKS_PER_STREAM- const blockSizeBytes = Math.max(- clamp(- Math.ceil(totalBytes / targetBlocks),- Math.min(MIN_BLOCK_BYTES, maxBlockBytes),- maxBlockBytes- ),- Math.ceil(totalBytes / MAX_BLOCKS)+ const blockSizeBytes = clamp(+ Math.ceil(totalBytes / targetBlocks),+ Math.min(MIN_BLOCK_BYTES, maxBlockBytes),+ maxBlockBytes)const blockCount = Math.ceil(totalBytes / blockSizeBytes)@@ -107,3 +104,21 @@ export function planDownload(request: PlanRequest): DownloadPlan {return { blockSizeBytes, blockCount, streamNetworks }}++/** The blocks a file of `totalBytes` is cut into, none of them started. An unknown size is one+ * block, open-ended to the end of the file. */+export function planBlocks(totalBytes: number, blockSizeBytes: number): BlockState[] {+ const fresh = (index: number, rangeStart: number, rangeEnd: number | null): BlockState => ({+ index,+ rangeStart,+ rangeEnd,+ status: 'pending',+ bytesDownloaded: 0,+ bytesByInterface: {}+ })+ if (totalBytes <= 0) return [fresh(0, 0, null)]+ return Array.from({ length: Math.ceil(totalBytes / blockSizeBytes) }, (_, index) => {+ const rangeStart = index * blockSizeBytes+ return fresh(index, rangeStart, Math.min(rangeStart + blockSizeBytes, totalBytes) - 1)+ })+}diff --git a/src/preload/index.ts b/src/preload/index.tsindex e17d440..155cb40 100644--- a/src/preload/index.ts+++ b/src/preload/index.ts@@ -1,7 +1,7 @@import { contextBridge, ipcRenderer, type IpcRendererEvent } from 'electron'import { IpcChannels } from '../shared/ipc-channels'import type { IpcContract } from '../shared/ipc-contract'-import type { AppSettings, DownloadState, InitialState } from '../shared/types'+import type { AppSettings, DownloadUpdate, InitialState } from '../shared/types'/** Typed wrapper around ipcRenderer.invoke — the channel name picks its args/result shape out of* IpcContract, so a call here that doesn't match what registerIpcHandlers (main) actually handles@@ -37,8 +37,8 @@ const plexoApi = {removeDownload: (downloadId: string) => invoke('removeDownload', downloadId),checkForUpdate: () => invoke('checkForUpdate'),- onDownloadUpdated: (callback: (state: DownloadState) => void): (() => void) => {- const listener = (_event: IpcRendererEvent, state: DownloadState): void => callback(state)+ onDownloadUpdated: (callback: (update: DownloadUpdate) => void): (() => void) => {+ const listener = (_event: IpcRendererEvent, update: DownloadUpdate): void => callback(update)ipcRenderer.on(IpcChannels.downloadUpdated, listener)return () => ipcRenderer.removeListener(IpcChannels.downloadUpdated, listener)}diff --git a/src/renderer/src/components/BlockGrid.tsx b/src/renderer/src/components/BlockGrid.tsxindex f182966..a7ccc20 100644--- a/src/renderer/src/components/BlockGrid.tsx+++ b/src/renderer/src/components/BlockGrid.tsx@@ -267,16 +267,13 @@ export function BlockGrid({let border = '0.5px solid var(--border-strong)'let boxShadow = 'none'let opacity = 1- let fillColor = visual?.solid || UNATTRIBUTED_SOLID+ const fillColor = visual?.solid || UNATTRIBUTED_SOLIDif (cell.status === 'downloading') {background = visual?.bg || UNATTRIBUTED_BGborder = `1px solid ${visual?.solid || UNATTRIBUTED_SOLID}`boxShadow = isPaused ? 'none' : `0 0 7px ${visual?.solid || UNATTRIBUTED_SOLID}`opacity = isPaused ? 0.6 : 1- } else if (cell.status === 'error') {- fillColor = 'var(--color-danger)'- border = 'none'} else if (cell.status === 'completed') {border = 'none'opacity = 0.92diff --git a/src/renderer/src/hooks/useDownloadEvents.ts b/src/renderer/src/hooks/useDownloadEvents.tsindex 1d3e4bf..7a0c904 100644--- a/src/renderer/src/hooks/useDownloadEvents.ts+++ b/src/renderer/src/hooks/useDownloadEvents.ts@@ -3,16 +3,18 @@ import { useAppStore } from '../store/useAppStore'/** Subscribes once to main-process download progress pushes for the lifetime of the app. */export function useDownloadEvents(): void {- const setCurrentDownload = useAppStore((store) => store.setCurrentDownload)+ const receiveDownloadUpdate = useAppStore((store) => store.receiveDownloadUpdate)useEffect(() => {let disposed = false- const unsubscribe = window.plexo.onDownloadUpdated(setCurrentDownload)+ // Subscribed before the snapshot is asked for, so nothing sent in between is missed; each+ // update carries a count, so whichever arrives late can't undo the other.+ const unsubscribe = window.plexo.onDownloadUpdated(receiveDownloadUpdate)void window.plexo.getCurrentDownload()- .then((download) => {- if (!disposed && download) setCurrentDownload(download)+ .then((snapshot) => {+ if (!disposed && snapshot) receiveDownloadUpdate(snapshot)}).catch(() => {})@@ -20,5 +22,5 @@ export function useDownloadEvents(): void {disposed = trueunsubscribe()}- }, [setCurrentDownload])+ }, [receiveDownloadUpdate])}diff --git a/src/renderer/src/store/useAppStore.ts b/src/renderer/src/store/useAppStore.tsindex d501c55..53fbe43 100644--- a/src/renderer/src/store/useAppStore.ts+++ b/src/renderer/src/store/useAppStore.ts@@ -1,6 +1,8 @@+import { applyDownloadUpdate } from '@shared/downloadUpdate'import type {AppSettings,DownloadState,+ DownloadUpdate,NetworkInterfaceInfo,NetworkPreference,NetworkPreferences,@@ -58,7 +60,8 @@ interface AppStore {setThemeSource: (source: ThemeSource) => voidcheckForUpdate: () => Promise<void>dismissUpdate: () => void- setCurrentDownload: (state: DownloadState) => void+ /** A snapshot or an update of the current download, from the main process. */+ receiveDownloadUpdate: (update: DownloadUpdate) => voidclearCurrentDownload: () => voidsetDraftUrl: (url: string) => voidsetDestinationDir: (dir: string) => void@@ -152,8 +155,10 @@ export const useAppStore = create<AppStore>((set, get) => ({persist({ dismissedUpdateVersion: update.version })},- setCurrentDownload: (download) => {+ receiveDownloadUpdate: (update) => {const previous = get().currentDownload+ const download = applyDownloadUpdate(previous, update)+ if (!download || download === previous) returnconst isNewDownload = !previous || previous.id !== download.idlet speedHistory = isNewDownload ? [] : get().speedHistorydiff --git a/src/shared/downloadUpdate.ts b/src/shared/downloadUpdate.tsnew file mode 100644index 0000000..b7a63d5--- /dev/null+++ b/src/shared/downloadUpdate.ts@@ -0,0 +1,20 @@+import type { DownloadState, DownloadUpdate } from './types'++/** The download as `current` plus `update`: what anything watching a download keeps. */+export function applyDownloadUpdate(+ current: DownloadState | null,+ update: DownloadUpdate+): DownloadState | null {+ const { state, blocks: changed, seq } = update+ if (current?.id === state.id) {+ // A snapshot taken after this was sent already has it.+ if ((current.seq ?? 0) >= seq) return current+ const blocks = [...(current.blocks ?? [])]+ for (const block of changed) blocks[block.index] = block+ return { ...state, blocks, seq }+ }+ // Another download: only an update with every block in it stands on its own — the first one+ // sent for a download, or a snapshot. Until one comes there is nothing whole to show.+ if (changed.length < (state.totalBlocks ?? 0)) return current+ return { ...state, blocks: changed, seq }+}diff --git a/src/shared/ipc-contract.ts b/src/shared/ipc-contract.tsindex dc2fc4e..8f25c88 100644--- a/src/shared/ipc-contract.ts+++ b/src/shared/ipc-contract.ts@@ -1,6 +1,6 @@import type {AppSettings,- DownloadState,+ DownloadUpdate,NetworkInterfaceInfo,ProbeResult,StartDownloadRequest,@@ -23,7 +23,7 @@ export interface IpcContract {readClipboardText: { args: []; result: string }revealInFolder: { args: [filePath: string]; result: void }startDownload: { args: [request: StartDownloadRequest]; result: string }- getCurrentDownload: { args: []; result: DownloadState | null }+ getCurrentDownload: { args: []; result: DownloadUpdate | null }pauseDownload: { args: [id: string]; result: void }resumeDownload: { args: [id: string]; result: void }cancelDownload: { args: [id: string]; result: void }diff --git a/src/shared/types.ts b/src/shared/types.tsindex f3853c5..baf379a 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -64,7 +64,7 @@ export interface ChunkState {hedge?: boolean}-export type BlockStatus = 'pending' | 'downloading' | 'completed' | 'error'+export type BlockStatus = 'pending' | 'downloading' | 'completed'export interface BlockState {index: number@@ -102,6 +102,20 @@ export interface DownloadState {pausedAt?: numbertotalPausedMs?: numbercompletedAt?: number+ /** The update this state is as of (see DownloadUpdate). */+ seq?: number+}++/** What the main process sends as a download changes: everything but its blocks, and only the+ * blocks that changed since it last sent. A download can have tens of thousands of blocks, and+ * copying every one several times a second would cost the process that carries every byte. A+ * snapshot is the same with every block in it. */+export interface DownloadUpdate {+ /** Counts what the main process has sent for the download. A snapshot has the count it was+ * taken at. */+ seq: number+ state: Omit<DownloadState, 'blocks'>+ blocks: BlockState[]}/** User customization for one physical network, keyed by NetworkInterfaceInfo.id — lets a