()
| 12 | const RECOMMENDATION_LIMIT = 5; |
| 13 | |
| 14 | export const initDiscoveryService = () => { |
| 15 | return eventBus.on('trackStarted', async () => { |
| 16 | const isEnabled = useSettingsStore |
| 17 | .getState() |
| 18 | .getValue('core.playback.discovery'); |
| 19 | const activeDiscoveryId = providersHost.getActive('discovery'); |
| 20 | if (!isEnabled || !providersHost.get(activeDiscoveryId, 'discovery')) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | const { items, currentIndex } = useQueueStore.getState(); |
| 25 | const isLastTrack = currentIndex >= items.length - 1; |
| 26 | if (!isLastTrack) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | const variety = |
| 31 | (useSettingsStore |
| 32 | .getState() |
| 33 | .getValue('core.playback.discoveryVariety') as number) ?? 0.5; |
| 34 | |
| 35 | const contextTracks: Track[] = items |
| 36 | .slice(-CONTEXT_SIZE) |
| 37 | .map((item) => item.track); |
| 38 | |
| 39 | try { |
| 40 | const recommendations = await discoveryHost.getRecommendations( |
| 41 | contextTracks, |
| 42 | { variety, limit: RECOMMENDATION_LIMIT }, |
| 43 | ); |
| 44 | |
| 45 | if (recommendations.length > 0) { |
| 46 | useQueueStore.getState().addToQueue(recommendations); |
| 47 | } |
| 48 | } catch (error) { |
| 49 | reportError('discovery', { |
| 50 | userMessage: i18n.t('discovery:recommendationError'), |
| 51 | error, |
| 52 | }); |
| 53 | } |
| 54 | }); |
| 55 | }; |
no test coverage detected