Skip to content

Split any YouTube song into stems

TypeScript

fix: live-update library thumbnails once cache warms

Daniel Ravina678e5a9c8621
diff --git a/src/main/thumbs.ts b/src/main/thumbs.ts
index 2bbe407..7decf7f 100644
--- a/src/main/thumbs.ts
+++ b/src/main/thumbs.ts
@@ -1,6 +1,6 @@
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
import { join } from 'path'
-import { net } from 'electron'
+import { net, BrowserWindow } from 'electron'
import { userDataDir } from './env'
import { loadSettings } from './settings'
@@ -40,6 +40,11 @@ export async function cacheThumbnail(videoId: string, url?: string): Promise<voi
mkdirSync(thumbsDir(), { recursive: true })
writeFileSync(file, buf)
memo.delete(videoId)
+ // the renderer may have already resolved (and memoized) null for this id
+ // before the cache was warm — tell it to look again
+ for (const win of BrowserWindow.getAllWindows()) {
+ win.webContents.send('thumb:cached', videoId)
+ }
} catch {}
}
diff --git a/src/preload/index.ts b/src/preload/index.ts
index b1a36ad..4a44fcd 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -28,6 +28,7 @@ const api: StemKitApi = {
setSettings: (patch) => ipcRenderer.invoke('settings:set', patch),
trackEvent: (name, params) => ipcRenderer.send('analytics:track', name, params),
getThumb: (videoId) => ipcRenderer.invoke('thumb:get', videoId),
+ onThumbCached: (cb) => subscribe<string>('thumb:cached', cb),
enginesStatus: () => ipcRenderer.invoke('engines:status'),
fetchEngine: (which) => ipcRenderer.invoke('engines:fetch', which),
onUpdateEvent: (cb) => subscribe<UpdateEvent>('update:event', cb),
diff --git a/src/renderer/src/lib/thumbs.tsx b/src/renderer/src/lib/thumbs.tsx
index d9f6034..161b326 100644
--- a/src/renderer/src/lib/thumbs.tsx
+++ b/src/renderer/src/lib/thumbs.tsx
@@ -14,22 +14,48 @@ function getThumbCached(videoId: string): Promise<string | null> {
return p
}
+// one IPC subscription per channel shared by every Thumb on the page —
+// per-component subscriptions blow past Electron's 10-listener cap once
+// the sidebar holds a handful of songs. An empty videoId means "re-check
+// everything" (settings flip); otherwise only that video's thumb changed
+type RecheckListener = (videoId: string) => void
+const listeners = new Set<RecheckListener>()
+let subscribed = false
+
+function ensureSubscribed(): void {
+ if (subscribed) return
+ subscribed = true
+ window.stemkit.onSettingsChange(() => {
+ memo.clear()
+ listeners.forEach((l) => l(''))
+ })
+ // optional so a stale preload (renderer hot-reloaded, window not) can't
+ // take the whole app down with a TypeError
+ window.stemkit.onThumbCached?.((id) => {
+ memo.delete(id)
+ listeners.forEach((l) => l(id))
+ })
+}
+
export function useThumb(videoId: string): string | null {
const [src, setSrc] = useState<string | null>(null)
useEffect(() => {
let alive = true
- void getThumbCached(videoId).then((url) => {
- if (alive) setSrc(url)
- })
- const off = window.stemkit.onSettingsChange(() => {
- memo.clear()
+ const resolve = (): void => {
void getThumbCached(videoId).then((url) => {
if (alive) setSrc(url)
})
- })
+ }
+ resolve()
+ ensureSubscribed()
+ const listener: RecheckListener = (id) => {
+ if (id && id !== videoId) return
+ resolve()
+ }
+ listeners.add(listener)
return () => {
alive = false
- off()
+ listeners.delete(listener)
}
}, [videoId])
return src
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 56ad6cd..fda6824 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -124,6 +124,7 @@ export interface StemKitApi {
setSettings(patch: Partial<AppSettings>): Promise<AppSettings>
trackEvent(name: string, params?: Record<string, string | number | boolean>): void
getThumb(videoId: string): Promise<string | null>
+ onThumbCached(cb: (videoId: string) => void): () => void
enginesStatus(): Promise<EngineStatus>
fetchEngine(which: 'vocals' | 'ft' | 'gpu'): Promise<void>
onUpdateEvent(cb: (ev: UpdateEvent) => void): () => void