Skip to content

Commit 30861d9

Browse files
committed
changed the way I get list of playlists. Instead of relying on a do-while and awaiting each response, I await only the first one, then make the rest in a volley. Means ~200-300ms between the first and second, and only 100ms between each of the rest. Won't make a difference for most users, but in case someone has 1000 playlists, their load time will go from maybe 5 seconds down to about 2 seconds.
1 parent 8f37f60 commit 30861d9

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2022 Pavel Komarov
3+
Copyright (c) 2024 Pavel Komarov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

exportify.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ class PlaylistTable extends React.Component {
5959
tracks: {total: library.total, href: "https://api.spotify.com/v1/me/tracks"}}
6060
let playlists = [[liked_songs]] // double list so .flat() flattens everything right later
6161

62-
// Retrieve the list of all the user's playlists by querying the playlists endpoint.
62+
// Compose a list of all the user's playlists by querying the playlists endpoint. Their total number of playlists
63+
// needs to be garnered from a response, so await the first response, then send a volley of requests to get the rest.
6364
// https://developer.spotify.com/documentation/web-api/reference/get-list-users-playlists
64-
let offset = 0, response = null
65-
do {
66-
response = await utils.apiCall("https://api.spotify.com/v1/me/playlists?limit=50&offset=" + offset,
67-
this.props.access_token) // no need for a delay, because I'm awaiting each response, which builds in transit-time delay
68-
playlists.push(response.items)
69-
offset += 50 // playlists can be grabbed up to 50 at a time
70-
} while (offset < response.total) // Go again if we haven't gotten them all yet.
65+
let response = await utils.apiCall("https://api.spotify.com/v1/me/playlists?limit=50&offset=0", this.props.access_token)
66+
playlists.push(response.items)
67+
let requests = []
68+
for (let offset = 50; offset < response.total; offset += 50) {
69+
requests.push(utils.apiCall("https://api.spotify.com/v1/me/playlists?limit=50&offset=" + offset, this.props.access_token, 100*offset))
70+
}
71+
await Promise.all(requests).then(responses => responses.map(response => playlists.push(response.items)))
7172

7273
//add info to this Component's state. Use setState() so render() gets called again.
7374
this.setState({ playlists: playlists.flat() }) // flatten list of lists into just a list
74-
7575
subtitle.textContent = this.state.playlists.length + ' playlists\n'; // directly reference an HTML element by id
7676
}
7777

0 commit comments

Comments
 (0)