11// @ts -nocheck
2- import { writable } from "svelte/store" ;
2+ import { writable , get } from "svelte/store" ;
33import { storePlaylists , storePlaylistSongs , getPlaylistsStore , getPlaylistSongsStore } from "./storage.js" ;
44
55const baseApiUrl = import . meta. env . VITE_BASE_API_URL ;
66
7- let playlistsArray = new Array ( ) ;
8- let playlistsSongsMap = new Map ( ) ;
7+ const playlistsArray = new Array ( ) ;
8+ const playlistsSongsMap = new Map ( ) ;
99
1010export const playlistsStore = writable ( [ ] ) ;
1111
12- export async function updateStores ( ) {
12+ export const writablePlaylistsStore = [ ] ;
13+
14+ export async function initStores ( ) {
1315 // check localStorage for playlists and songs
1416 // if not found, fetch from API
15- let cachedPlaylists = getPlaylistsStore ( ) ;
17+ const cachedPlaylists = getPlaylistsStore ( ) ;
1618
1719 if ( cachedPlaylists . length > 0 ) {
1820 for ( const playlist of cachedPlaylists ) {
19- playlistsArray . push ( playlist ) ;
21+ playlistsArray . push ( playlist ) ;
2022 let songs = getPlaylistSongsStore ( playlist . id ) ;
2123 playlistsSongsMap . set ( playlist . id , songs ) ;
24+
25+ writablePlaylistsStore [ playlist . id ] = writable ( songs ) ;
2226 }
2327
2428 playlistsStore . set ( playlistsArray ) ;
2529 console . log ( "Loaded playlists from localStorage" ) ;
2630 } else {
27- let playlists = await fetch ( `${ baseApiUrl } /playlist` )
31+ console . log ( "Fetching playlists from API" ) ;
32+ const playlists = await fetch ( `${ baseApiUrl } /playlist` )
2833 . then ( ( response ) => response . json ( ) )
2934 . then ( ( data ) => data . Data )
3035 . catch ( ( error ) => console . error ( "Error fetching playlists:" , error ) ) ;
3136
32- playlistsArray = [ ] ;
37+ playlistsArray . length = 0 ; ;
3338 playlistsSongsMap . clear ( ) ;
3439
3540 for ( const playlist of playlists ) {
3641 playlistsArray . push ( playlist ) ;
37- let songs = await fetchPlaylistSongs ( playlist . id ) ;
42+ const songs = await fetchPlaylistSongs ( playlist . id ) ;
3843 playlistsSongsMap . set ( playlist . id , songs ) ;
3944
4045 storePlaylistSongs ( playlist . id , songs ) ;
46+
47+ writablePlaylistsStore [ playlist . id ] = writable ( songs ) ;
4148 }
4249
4350 playlistsStore . set ( playlistsArray ) ;
4451 storePlaylists ( playlistsArray ) ;
4552 }
4653}
4754
55+ export async function updateStores ( ) {
56+ // Update plaists
57+ const cachedPlaylists = getPlaylistsStore ( ) ;
58+ const lastKnowPlaylistId = cachedPlaylists . at ( - 1 ) . id ;
59+ const playlists = await fetchPlaylists ( lastKnowPlaylistId ) ;
60+
61+ for ( const playlist of playlists ) {
62+ playlistsArray . push ( playlist ) ;
63+ }
64+
65+ playlistsStore . set ( playlistsArray ) ;
66+ storePlaylists ( playlistsArray ) ;
67+
68+ // Update songs for each playlist
69+ console . log ( "starting to update songs for each playlist" ) ;
70+ for ( const playlist of playlistsArray ) {
71+
72+ let lastKnowSongPosition = playlistsSongsMap . get ( playlist . id ) . length ;
73+
74+ if ( ! lastKnowSongPosition ) {
75+ lastKnowSongPosition = 0 ; // Default to 0 if no songs are found
76+ }
77+
78+ const songs = await fetchPlaylistSongs ( playlist . id , lastKnowSongPosition ) ;
79+
80+ if ( songs . length > 0 ) {
81+ // Add local notification for new songs?
82+ console . log ( `Found (${ songs . length } ) new songs for playlist ID: ${ playlist . id } with last known song position: ${ lastKnowSongPosition } ` ) ;
83+ playlistsSongsMap . set ( playlist . id , [ ...playlistsSongsMap . get ( playlist . id ) , ...songs ] ) ;
84+ storePlaylistSongs ( playlist . id , playlistsSongsMap . get ( playlist . id ) ) ;
85+
86+ const songList = get ( writablePlaylistsStore [ playlist . id ] ) ;
87+
88+ for ( const song of songs ) {
89+ songList . push ( song ) ;
90+ }
91+
92+ writablePlaylistsStore [ playlist . id ] . set ( songList ) ;
93+ }
94+ }
95+ console . log ( "Finished updating songs for each playlist" ) ;
96+ }
97+
4898export function getImageUrl ( imagePath ) {
4999 if ( ! imagePath ) return null ;
50100 return `${ baseApiUrl } /images/${ imagePath } ` ;
@@ -69,8 +119,16 @@ export function getPlaylistById(playlistId) {
69119 }
70120}
71121
72- async function fetchPlaylistSongs ( playlistId ) {
73- let songs = await fetch ( `${ baseApiUrl } /playlist/${ playlistId } ` )
122+ async function fetchPlaylists ( lastKnowPlaylistId ) {
123+ const playlists = await fetch ( `${ baseApiUrl } /playlist?lastKnowPlaylistId=${ lastKnowPlaylistId } ` )
124+ . then ( ( response ) => response . json ( ) )
125+ . then ( ( data ) => data . Data )
126+ . catch ( ( error ) => console . error ( "Error fetching playlists:" , error ) ) ;
127+ return playlists ;
128+ }
129+
130+ async function fetchPlaylistSongs ( playlistId , lastKnowSongId = 0 ) {
131+ const songs = await fetch ( `${ baseApiUrl } /playlist/${ playlistId } ?lastKnowSongPosition=${ lastKnowSongId } ` )
74132 . then ( ( response ) => response . json ( ) )
75133 . then ( ( data ) => data . Data )
76134 . catch ( ( error ) => console . error ( "Error fetching playlist songs:" , error ) ) ;
0 commit comments