Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

perf: keep progress updates from re-rendering what didn't change (#151)

Arpit Khandelwale65fc4c52809
diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx
index f1d55f2..8d29d13 100644
--- a/src/renderer/src/App.tsx
+++ b/src/renderer/src/App.tsx
@@ -38,29 +38,33 @@ function renderDownload(download: DownloadState | FinishedDownload): React.JSX.E
}
}
+/** The download shown, or the list. Only this re-renders on a progress update — App, the title
+ * bar, the status bar and the dialogs around it don't. */
+function CurrentScreen(): React.JSX.Element {
+ // Just the one shown: an update to any other download leaves this selector's result alone.
+ const shown = useAppStore((store) => {
+ const { view } = store
+ if (view.name !== 'download') return null
+ // A download that's gone (removed, deleted) leaves the list showing.
+ return store.downloads[view.id] ?? store.history.find((entry) => entry.id === view.id) ?? null
+ })
+ const cancelled = shown && !('unitsWritten' in shown) && shown.status === 'cancelled'
+ return shown && !cancelled ? renderDownload(shown) : <DownloadsScreen />
+}
+
function App(): React.JSX.Element {
useDownloadEvents()
useOpenedLinks()
useNewDownloadShortcuts()
useNetworkEvents()
- const downloads = useAppStore((store) => store.downloads)
- const history = useAppStore((store) => store.history)
- const view = useAppStore((store) => store.view)
- // A download that's gone (removed, deleted) leaves the list showing.
- const shown =
- view.name === 'download'
- ? (downloads[view.id] ?? history.find((entry) => entry.id === view.id) ?? null)
- : null
- const cancelled = shown && !('unitsWritten' in shown) && shown.status === 'cancelled'
-
return (
<TooltipProvider>
<DownloadActionsProvider>
<div className="flex h-full flex-col">
<TitleBar />
<div className="min-h-0 flex-1">
- {shown && !cancelled ? renderDownload(shown) : <DownloadsScreen />}
+ <CurrentScreen />
</div>
<StatusBar />
<NewDownloadDialog />
diff --git a/src/renderer/src/hooks/useNetworkVisuals.ts b/src/renderer/src/hooks/useNetworkVisuals.ts
index 0b81a5c..75a177b 100644
--- a/src/renderer/src/hooks/useNetworkVisuals.ts
+++ b/src/renderer/src/hooks/useNetworkVisuals.ts
@@ -1,4 +1,5 @@
import type { NetworkInterfaceKind } from '@shared/types'
+import { useMemo } from 'react'
import { useAppStore } from '../store/useAppStore'
import { assignNetworkColors, resolveNetworkVisual, type NetworkVisual } from '../theme'
@@ -8,23 +9,36 @@ export type ResolveNetworkVisual = (
osName: string
) => NetworkVisual
-/** Colors are assigned across every known network at once (detected interfaces plus any a
- * download, running or finished, used), so each screen agrees on which network is which color. */
-export function useNetworkVisuals(): ResolveNetworkVisual {
- const interfaces = useAppStore((store) => store.interfaces)
- const downloads = useAppStore((store) => store.downloads)
- const history = useAppStore((store) => store.history)
- const preferences = useAppStore((store) => store.networkPreferences)
-
+/** Every known network and its kind, as JSON: a string, so a progress update that brings no
+ * new network leaves it equal and doesn't re-render whoever asks. */
+function knownNetworks(store: ReturnType<typeof useAppStore.getState>): string {
const networks = new Map<string, NetworkInterfaceKind>()
- for (const iface of interfaces) networks.set(iface.id, iface.kind)
- for (const download of [...Object.values(downloads), ...history]) {
+ for (const iface of store.interfaces) networks.set(iface.id, iface.kind)
+ for (const download of Object.values(store.downloads)) {
for (const network of download.networks) {
if (!networks.has(network.id)) networks.set(network.id, network.kind)
}
}
- const colors = assignNetworkColors(networks, preferences)
+ for (const download of store.history) {
+ for (const network of download.networks) {
+ if (!networks.has(network.id)) networks.set(network.id, network.kind)
+ }
+ }
+ return JSON.stringify([...networks])
+}
+
+/** Colors are assigned across every known network at once (detected interfaces plus any a
+ * download, running or finished, used), so each screen agrees on which network is which color.
+ * The function returned stays the same until a network or a preference changes, so it can be
+ * handed to memoized rows. */
+export function useNetworkVisuals(): ResolveNetworkVisual {
+ const known = useAppStore(knownNetworks)
+ const preferences = useAppStore((store) => store.networkPreferences)
- return (id, kind, osName) =>
- resolveNetworkVisual(kind, osName, preferences[id], colors.get(id) ?? 'teal')
+ return useMemo(() => {
+ const networks = new Map<string, NetworkInterfaceKind>(JSON.parse(known))
+ const colors = assignNetworkColors(networks, preferences)
+ return (id, kind, osName) =>
+ resolveNetworkVisual(kind, osName, preferences[id], colors.get(id) ?? 'teal')
+ }, [known, preferences])
}
diff --git a/src/renderer/src/screens/DownloadingScreen.tsx b/src/renderer/src/screens/DownloadingScreen.tsx
index 7768ea4..e4224ae 100644
--- a/src/renderer/src/screens/DownloadingScreen.tsx
+++ b/src/renderer/src/screens/DownloadingScreen.tsx
@@ -100,8 +100,8 @@ function waitingFor(download: DownloadState): string | null {
: 'Waiting for a network. Reconnect one or switch one on.'
}
-// Memoized: App re-renders on every download's progress, and this one's grid and tables are
-// the heaviest thing on screen. Its download keeps its object until its own update arrives.
+// Memoized: its grid and tables are the heaviest thing on screen, so nothing but its own
+// download's update (which brings a new object) should re-render it.
export const DownloadingScreen = memo(function DownloadingScreen({
download
}: {
diff --git a/src/renderer/src/screens/DownloadsScreen.tsx b/src/renderer/src/screens/DownloadsScreen.tsx
index b7ed72b..e5d942b 100644
--- a/src/renderer/src/screens/DownloadsScreen.tsx
+++ b/src/renderer/src/screens/DownloadsScreen.tsx
@@ -320,24 +320,20 @@ export function DownloadsScreen(): React.JSX.Element {
)}
</div>
{group.items.map((item) => (
- <DownloadContextMenu
+ <ListedDownload
key={item.id}
item={item}
+ now={now}
selected={selected.has(item.id)}
- chosen={chosen}
- >
- <DownloadRow
- item={item}
- now={now}
- selected={selected.has(item.id)}
- networkVisual={
- isFinished(item) || item.status === 'completed' ? undefined : networkVisual
- }
- onSelect={selectRow}
- onOpen={openRow}
- onPerform={perform}
- />
- </DownloadContextMenu>
+ // Only a selected row's menu acts on the selection.
+ chosen={selected.has(item.id) ? chosen : NOT_CHOSEN}
+ networkVisual={
+ isFinished(item) || item.status === 'completed' ? undefined : networkVisual
+ }
+ onSelect={selectRow}
+ onOpen={openRow}
+ onPerform={perform}
+ />
))}
</section>
)
@@ -413,6 +409,21 @@ function EmptyState(): React.JSX.Element {
)
}
+const NOT_CHOSEN: Item[] = []
+
+/** A row with its right-click menu, memoized as one: a progress update to one download leaves
+ * every other row, menu included, as it was. */
+const ListedDownload = memo(function ListedDownload({
+ chosen,
+ ...row
+}: React.ComponentProps<typeof DownloadRow> & { chosen: Item[] }): React.JSX.Element {
+ return (
+ <DownloadContextMenu item={row.item} selected={row.selected} chosen={chosen}>
+ <DownloadRow {...row} />
+ </DownloadContextMenu>
+ )
+})
+
const DownloadRow = memo(function DownloadRow({
item,
now,