Skip to content

Commit 0e2b55b

Browse files
committed
Preloading coverarts
1 parent e64ea39 commit 0e2b55b

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/lib/util/dom.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
export function preloadImage(src: string, timeout: number = 10000): Promise<boolean> {
3+
return new Promise((resolve) => {
4+
const img = document.createElement("img");
5+
let complete = false;
6+
7+
img.style.position = "absolute";
8+
img.style.top = "10000%";
9+
img.src = src;
10+
11+
function done(result: boolean) {
12+
if(complete)
13+
return;
14+
15+
complete = true;
16+
img.remove();
17+
resolve(result);
18+
}
19+
20+
img.onload = () => done(true);
21+
img.onerror = () => done(false);
22+
23+
setTimeout(() => done(false), timeout);
24+
});
25+
}

src/routes/+page.svelte

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { SongAPI } from "$lib/api/api";
88
import ProgressBar from "$lib/comp/ProgressBar.svelte";
99
import { wait } from "$lib/util";
10+
import { preloadImage } from "$lib/util/dom";
1011
1112
const api = new SongAPI();
1213
@@ -27,15 +28,21 @@
2728
2829
if(stats) {
2930
const songs = stats.data.songs.slice(0, config.songRankCount);
30-
progressMax = songs.length * 2;
31+
progressMax = songs.length * 3;
3132
3233
for(let i = 0; i < songs.length; i++) {
33-
const song = songs[i];
34+
const songStat = songs[i];
3435
35-
const artist = await api.queryArtistByName(song.artist);
36+
const artist = await api.queryArtistByName(songStat.artist);
3637
progress++;
3738
38-
await api.querySongByName(song.title, artist?.id);
39+
const song = await api.querySongByName(songStat.title, artist?.id);
40+
progress++;
41+
42+
if(song?.coverArt) {
43+
await preloadImage(song.coverArt, 2000);
44+
}
45+
3946
progress++;
4047
4148
// Account for rate limit

0 commit comments

Comments
 (0)