|
1 | | -import { createStore } from 'solid-js/store'; |
| 1 | +import * as z from 'zod'; |
2 | 2 |
|
3 | | -import { createMemo } from 'solid-js'; |
| 3 | +import type { LyricResult } from '../types'; |
4 | 4 |
|
5 | | -import { LRCLib } from './LRCLib'; |
6 | | -import { LyricsGenius } from './LyricsGenius'; |
7 | | -import { MusixMatch } from './MusixMatch'; |
8 | | -import { YTMusic } from './YTMusic'; |
9 | | - |
10 | | -import { getSongInfo } from '@/providers/song-info-front'; |
11 | | - |
12 | | -import type { LyricProvider, LyricResult } from '../types'; |
13 | | -import type { SongInfo } from '@/providers/song-info'; |
14 | | - |
15 | | -export const providers = { |
16 | | - YTMusic: new YTMusic(), |
17 | | - LRCLib: new LRCLib(), |
18 | | - MusixMatch: new MusixMatch(), |
19 | | - LyricsGenius: new LyricsGenius(), |
20 | | - // Megalobiz: new Megalobiz(), // Disabled because it is too unstable and slow |
21 | | -} as const; |
| 5 | +export enum ProviderNames { |
| 6 | + YTMusic = 'YTMusic', |
| 7 | + LRCLib = 'LRCLib', |
| 8 | + MusixMatch = 'MusixMatch', |
| 9 | + LyricsGenius = 'LyricsGenius', |
| 10 | + // Megalobiz = 'Megalobiz', |
| 11 | +} |
22 | 12 |
|
23 | | -export type ProviderName = keyof typeof providers; |
24 | | -export const providerNames = Object.keys(providers) as ProviderName[]; |
| 13 | +export const ProviderNameSchema = z.enum(ProviderNames); |
| 14 | +export type ProviderName = z.infer<typeof ProviderNameSchema>; |
| 15 | +export const providerNames = ProviderNameSchema.options; |
25 | 16 |
|
26 | 17 | export type ProviderState = { |
27 | 18 | state: 'fetching' | 'done' | 'error'; |
28 | 19 | data: LyricResult | null; |
29 | 20 | error: Error | null; |
30 | 21 | }; |
31 | | - |
32 | | -type LyricsStore = { |
33 | | - provider: ProviderName; |
34 | | - current: ProviderState; |
35 | | - lyrics: Record<ProviderName, ProviderState>; |
36 | | -}; |
37 | | - |
38 | | -const initialData = () => |
39 | | - providerNames.reduce( |
40 | | - (acc, name) => { |
41 | | - acc[name] = { state: 'fetching', data: null, error: null }; |
42 | | - return acc; |
43 | | - }, |
44 | | - {} as LyricsStore['lyrics'], |
45 | | - ); |
46 | | - |
47 | | -export const [lyricsStore, setLyricsStore] = createStore<LyricsStore>({ |
48 | | - provider: providerNames[0], |
49 | | - lyrics: initialData(), |
50 | | - get current(): ProviderState { |
51 | | - return this.lyrics[this.provider]; |
52 | | - }, |
53 | | -}); |
54 | | - |
55 | | -export const currentLyrics = createMemo(() => { |
56 | | - const provider = lyricsStore.provider; |
57 | | - return lyricsStore.lyrics[provider]; |
58 | | -}); |
59 | | - |
60 | | -type VideoId = string; |
61 | | - |
62 | | -type SearchCacheData = Record<ProviderName, ProviderState>; |
63 | | -interface SearchCache { |
64 | | - state: 'loading' | 'done'; |
65 | | - data: SearchCacheData; |
66 | | -} |
67 | | - |
68 | | -// TODO: Maybe use localStorage for the cache. |
69 | | -const searchCache = new Map<VideoId, SearchCache>(); |
70 | | -export const fetchLyrics = (info: SongInfo) => { |
71 | | - if (searchCache.has(info.videoId)) { |
72 | | - const cache = searchCache.get(info.videoId)!; |
73 | | - |
74 | | - if (cache.state === 'loading') { |
75 | | - setTimeout(() => { |
76 | | - fetchLyrics(info); |
77 | | - }); |
78 | | - return; |
79 | | - } |
80 | | - |
81 | | - if (getSongInfo().videoId === info.videoId) { |
82 | | - setLyricsStore('lyrics', () => { |
83 | | - // weird bug with solid-js |
84 | | - return JSON.parse(JSON.stringify(cache.data)) as typeof cache.data; |
85 | | - }); |
86 | | - } |
87 | | - |
88 | | - return; |
89 | | - } |
90 | | - |
91 | | - const cache: SearchCache = { |
92 | | - state: 'loading', |
93 | | - data: initialData(), |
94 | | - }; |
95 | | - |
96 | | - searchCache.set(info.videoId, cache); |
97 | | - if (getSongInfo().videoId === info.videoId) { |
98 | | - setLyricsStore('lyrics', () => { |
99 | | - // weird bug with solid-js |
100 | | - return JSON.parse(JSON.stringify(cache.data)) as typeof cache.data; |
101 | | - }); |
102 | | - } |
103 | | - |
104 | | - const tasks: Promise<void>[] = []; |
105 | | - |
106 | | - // prettier-ignore |
107 | | - for ( |
108 | | - const [providerName, provider] of Object.entries(providers) as [ |
109 | | - ProviderName, |
110 | | - LyricProvider, |
111 | | - ][] |
112 | | - ) { |
113 | | - const pCache = cache.data[providerName]; |
114 | | - |
115 | | - tasks.push( |
116 | | - provider |
117 | | - .search(info) |
118 | | - .then((res) => { |
119 | | - pCache.state = 'done'; |
120 | | - pCache.data = res; |
121 | | - |
122 | | - if (getSongInfo().videoId === info.videoId) { |
123 | | - setLyricsStore('lyrics', (old) => { |
124 | | - return { |
125 | | - ...old, |
126 | | - [providerName]: { |
127 | | - state: 'done', |
128 | | - data: res ? { ...res } : null, |
129 | | - error: null, |
130 | | - }, |
131 | | - }; |
132 | | - }); |
133 | | - } |
134 | | - }) |
135 | | - .catch((error: Error) => { |
136 | | - pCache.state = 'error'; |
137 | | - pCache.error = error; |
138 | | - |
139 | | - console.error(error); |
140 | | - |
141 | | - if (getSongInfo().videoId === info.videoId) { |
142 | | - setLyricsStore('lyrics', (old) => { |
143 | | - return { |
144 | | - ...old, |
145 | | - [providerName]: { state: 'error', error, data: null }, |
146 | | - }; |
147 | | - }); |
148 | | - } |
149 | | - }), |
150 | | - ); |
151 | | - } |
152 | | - |
153 | | - Promise.allSettled(tasks).then(() => { |
154 | | - cache.state = 'done'; |
155 | | - searchCache.set(info.videoId, cache); |
156 | | - }); |
157 | | -}; |
158 | | - |
159 | | -export const retrySearch = (provider: ProviderName, info: SongInfo) => { |
160 | | - setLyricsStore('lyrics', (old) => { |
161 | | - const pCache = { |
162 | | - state: 'fetching', |
163 | | - data: null, |
164 | | - error: null, |
165 | | - }; |
166 | | - |
167 | | - return { |
168 | | - ...old, |
169 | | - [provider]: pCache, |
170 | | - }; |
171 | | - }); |
172 | | - |
173 | | - providers[provider] |
174 | | - .search(info) |
175 | | - .then((res) => { |
176 | | - setLyricsStore('lyrics', (old) => { |
177 | | - return { |
178 | | - ...old, |
179 | | - [provider]: { state: 'done', data: res, error: null }, |
180 | | - }; |
181 | | - }); |
182 | | - }) |
183 | | - .catch((error) => { |
184 | | - setLyricsStore('lyrics', (old) => { |
185 | | - return { |
186 | | - ...old, |
187 | | - [provider]: { state: 'error', data: null, error }, |
188 | | - }; |
189 | | - }); |
190 | | - }); |
191 | | -}; |
0 commit comments