Replace main lyrics listener with promise handler
This commit is contained in:
parent
d38c846e80
commit
98fa47348c
2 changed files with 18 additions and 13 deletions
|
@ -3,7 +3,6 @@ import { query as queryGenius } from './genius';
|
||||||
import { query as queryNetease } from './netease';
|
import { query as queryNetease } from './netease';
|
||||||
import { LyricSource } from '../../../../renderer/types';
|
import { LyricSource } from '../../../../renderer/types';
|
||||||
import { ipcMain } from 'electron';
|
import { ipcMain } from 'electron';
|
||||||
import { getMainWindow } from '../../../main';
|
|
||||||
import { store } from '../settings/index';
|
import { store } from '../settings/index';
|
||||||
|
|
||||||
type SongFetcher = (song: QueueSong) => Promise<InternetProviderLyricResponse | null>;
|
type SongFetcher = (song: QueueSong) => Promise<InternetProviderLyricResponse | null>;
|
||||||
|
@ -19,7 +18,7 @@ const MAX_CACHED_ITEMS = 10;
|
||||||
|
|
||||||
const lyricCache = new Map<string, CachedLyrics>();
|
const lyricCache = new Map<string, CachedLyrics>();
|
||||||
|
|
||||||
ipcMain.on('lyric-fetch', async (_event, song: QueueSong) => {
|
const getRemoteLyrics = async (song: QueueSong) => {
|
||||||
const sources = store.get('lyrics', []) as LyricSource[];
|
const sources = store.get('lyrics', []) as LyricSource[];
|
||||||
|
|
||||||
const cached = lyricCache.get(song.id);
|
const cached = lyricCache.get(song.id);
|
||||||
|
@ -27,23 +26,21 @@ ipcMain.on('lyric-fetch', async (_event, song: QueueSong) => {
|
||||||
if (cached) {
|
if (cached) {
|
||||||
for (const source of sources) {
|
for (const source of sources) {
|
||||||
const data = cached[source];
|
const data = cached[source];
|
||||||
|
if (data) return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (data) {
|
let lyricsFromSource = null;
|
||||||
getMainWindow()?.webContents.send('lyric-get', song.name, source, data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const source of sources) {
|
for (const source of sources) {
|
||||||
const lyric = await FETCHERS[source](song);
|
const response = await FETCHERS[source](song);
|
||||||
if (lyric) {
|
if (response) {
|
||||||
const newResult = cached
|
const newResult = cached
|
||||||
? {
|
? {
|
||||||
...cached,
|
...cached,
|
||||||
[source]: lyric,
|
[source]: response,
|
||||||
}
|
}
|
||||||
: ({ [source]: lyric } as CachedLyrics);
|
: ({ [source]: response } as CachedLyrics);
|
||||||
|
|
||||||
if (lyricCache.size === MAX_CACHED_ITEMS && cached === undefined) {
|
if (lyricCache.size === MAX_CACHED_ITEMS && cached === undefined) {
|
||||||
const toRemove = lyricCache.keys().next().value;
|
const toRemove = lyricCache.keys().next().value;
|
||||||
|
@ -52,8 +49,15 @@ ipcMain.on('lyric-fetch', async (_event, song: QueueSong) => {
|
||||||
|
|
||||||
lyricCache.set(song.id, newResult);
|
lyricCache.set(song.id, newResult);
|
||||||
|
|
||||||
getMainWindow()?.webContents.send('lyric-get', song.name, source, lyric);
|
lyricsFromSource = response;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return lyricsFromSource;
|
||||||
|
};
|
||||||
|
|
||||||
|
ipcMain.handle('lyric-fetch-manual', async (_event, song: QueueSong) => {
|
||||||
|
const lyric = await getRemoteLyrics(song);
|
||||||
|
return lyric;
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,7 +2,8 @@ import { IpcRendererEvent, ipcRenderer } from 'electron';
|
||||||
import { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
|
import { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
|
||||||
|
|
||||||
const fetchRemoteLyrics = (song: QueueSong) => {
|
const fetchRemoteLyrics = (song: QueueSong) => {
|
||||||
ipcRenderer.send('lyric-fetch', song);
|
const result = ipcRenderer.invoke('lyric-fetch-manual', song);
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
const remoteLyricsListener = (
|
const remoteLyricsListener = (
|
||||||
|
|
Reference in a new issue