This repository has been archived on 2025-03-19. You can view files and clone it, but cannot push or open issues or pull requests.
feishin/src/main/features/core/lyrics/index.ts
Kendall Garner 007a099951 Lyrics Improvements
- Make the settings text actually consistent with behavior
- Add metadata (artist/track name) for fetched tracks
- Add ability to remove incorrectly fetched lyric
- Add lyric fetch cache; save the last 10 fetches
- Add ability to change offset in full screen, add more comments
2023-06-09 17:24:34 -07:00

59 lines
1.7 KiB
TypeScript

import { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
import { query as queryGenius } from './genius';
import { query as queryNetease } from './netease';
import { LyricSource } from '../../../../renderer/types';
import { ipcMain } from 'electron';
import { getMainWindow } from '../../../main';
import { store } from '../settings/index';
type SongFetcher = (song: QueueSong) => Promise<InternetProviderLyricResponse | null>;
type CachedLyrics = Record<LyricSource, InternetProviderLyricResponse>;
const FETCHERS: Record<LyricSource, SongFetcher> = {
[LyricSource.GENIUS]: queryGenius,
[LyricSource.NETEASE]: queryNetease,
};
const MAX_CACHED_ITEMS = 10;
const lyricCache = new Map<string, CachedLyrics>();
ipcMain.on('lyric-fetch', async (_event, song: QueueSong) => {
const sources = store.get('lyrics', []) as LyricSource[];
const cached = lyricCache.get(song.id);
if (cached) {
for (const source of sources) {
const data = cached[source];
if (data) {
getMainWindow()?.webContents.send('lyric-get', song.name, source, data);
return;
}
}
}
for (const source of sources) {
const lyric = await FETCHERS[source](song);
if (lyric) {
const newResult = cached
? {
...cached,
[source]: lyric,
}
: ({ [source]: lyric } as CachedLyrics);
if (lyricCache.size === MAX_CACHED_ITEMS && cached === undefined) {
const toRemove = lyricCache.keys().next().value;
lyricCache.delete(toRemove);
}
lyricCache.set(song.id, newResult);
getMainWindow()?.webContents.send('lyric-get', song.name, source, lyric);
break;
}
}
});