Skip to content

Commit d035df2

Browse files
committed
Querying only what's needed
1 parent 3a58171 commit d035df2

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/lib/layout/StatsView.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<script lang="ts">
2-
import type { SongAPI } from "$lib/api/api";
2+
import { SongAPI } from "$lib/api/api";
33
import ArtistList from "$lib/comp/ArtistList.svelte";
44
import PageContainer from "$lib/comp/PageContainer.svelte";
55
import SongList from "$lib/comp/SongList.svelte";
66
import LinkButton from "$lib/LinkButton.svelte";
7-
import type { Stats } from "$lib/stats";
7+
import type { Stats, StatsViewConfig } from "$lib/stats";
88
9+
export let config: StatsViewConfig;
910
export let stats: Stats;
1011
export let api: SongAPI;
1112
@@ -63,12 +64,12 @@
6364

6465
{#snippet pageFavSongsAllTime()}
6566
<h1>Your favorite songs of all time</h1>
66-
<SongList songs={stats.data.songs.slice(0, 4)} api={api} />
67+
<SongList songs={stats.data.songs.slice(0, config.songRankCount)} api={api} />
6768
{/snippet}
6869

6970
{#snippet pageFavArtistsAllTime()}
7071
<h1>Your favorite artists of all time</h1>
71-
<ArtistList artists={stats.getArtists(true).slice(0, 3)} />
72+
<ArtistList artists={stats.getArtists(true).slice(0, config.artistRankCount)} />
7273
{/snippet}
7374

7475
{#snippet pageEnd()}

src/lib/stats.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
export type StatsViewConfig = {
3+
songRankCount: number,
4+
artistRankCount: number
5+
}
6+
27
export type StatsData = {
38
formatVersion: number,
49
songs: SongData[]

src/routes/+page.svelte

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import type { Stats, StatsData } from "$lib/stats";
2+
import type { Stats, StatsData, StatsViewConfig } from "$lib/stats";
33
44
import Home from "$lib/layout/Home.svelte";
55
import Page from "$lib/comp/Page.svelte";
@@ -11,14 +11,19 @@
1111
let currentStats: Stats | null = null;
1212
let statsProcessed = false;
1313
14+
const config: StatsViewConfig = {
15+
songRankCount: 4,
16+
artistRankCount: 3
17+
};
18+
1419
async function setStats(stats: Stats | null) {
1520
currentStats = stats;
1621
statsProcessed = false;
1722
1823
if(stats) {
1924
// Load API data to cache
2025
21-
for(const song of stats.data.songs) {
26+
for(const song of stats.data.songs.slice(config.songRankCount)) {
2227
const artist = await api.queryArtistByName(song.artist);
2328
await api.querySongByName(song.title, artist?.id);
2429
}
@@ -33,6 +38,7 @@
3338
<StatsView
3439
stats={currentStats}
3540
api={api}
41+
config={config}
3642
onClose={() => setStats(null)}
3743
/>
3844
{:else if currentStats && !statsProcessed}

0 commit comments

Comments
 (0)