Skip to content

Commit ddc5154

Browse files
committed
Improved UX by caching certain states which save on unneccessary requests to Spotify API
1 parent 230c2df commit ddc5154

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

assets/js/main.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
let SPOTIFY_API;
2-
let RECENT_SPOTIFY_PLAYBACK_DEVICE_ID;
3-
let SHUFFLE_MAX_BATCH_SAMPLE_SIZE = 50;
4-
let RECENT_SPOTIFY_SHUFFLED_TRACKS = [];
1+
let SPOTIFY_API; // Globally stores the Spotify API instance
2+
let SPOTIFY_USER_IS_PREMIUM; // Caches whether the Spotify user is a Premium user or not
3+
let SHUFFLE_MAX_BATCH_SAMPLE_SIZE = 50; // The sample size for batch shuffling of tracks
4+
let RECENT_SPOTIFY_PLAYBACK_DEVICE_ID; // Caches the device id of the Spotify player that most recently played shuffled tracks
5+
let RECENT_SPOTIFY_SHUFFLED_TRACKS = []; // Caches the most recently shuffled tracks from Spotify
56

6-
let TEMPORARY_SHUFFLED_PLAYLIST;
7-
const TEMPORARY_SHUFFLED_PLAYLIST_NAME = 'True Shuffle Playlist';
8-
const TEMPORARY_SHUFFLED_PLAYLIST_DESCRIPTION = `An Automatically Generated Playlist By True Shuffle from ${location.origin}`;
7+
let TEMPORARY_SHUFFLED_PLAYLIST; // Caches the temporary shuffled playlist object to store free user shuffled tracks
8+
const TEMPORARY_SHUFFLED_PLAYLIST_NAME = 'True Shuffle Playlist'; // Default name for the temporary shuffled playlist
9+
const TEMPORARY_SHUFFLED_PLAYLIST_DESCRIPTION = `An Automatically Generated Playlist By True Shuffle from ${location.origin}`; // Default description for the temporary shuffled playlist
910

1011
/**
1112
* Begins the process of shuffling and playing music based on UI selected playlist/device.
@@ -45,16 +46,21 @@ async function shuffle_and_play() {
4546
RECENT_SPOTIFY_SHUFFLED_TRACKS = results;
4647

4748
// Safely determine if the user is a Premium user by disabling shuffle
48-
let is_premium;
49-
try {
50-
ui_render_play_button('Preparing Player...', false);
51-
is_premium = await SPOTIFY_API.set_playback_shuffle(device_id, false);
52-
} catch (error) {
53-
log('ERROR', 'Failed to set playback shuffle to disabled.');
54-
alert('Failed to disable playback shuffle in Spotify player.');
55-
return console.log(error);
49+
let is_premium = SPOTIFY_USER_IS_PREMIUM;
50+
if (is_premium === undefined) {
51+
try {
52+
ui_render_play_button('Preparing Player...', false);
53+
is_premium = await SPOTIFY_API.set_playback_shuffle(device_id, false);
54+
} catch (error) {
55+
log('ERROR', 'Failed to set playback shuffle to disabled.');
56+
alert('Failed to disable playback shuffle in Spotify player.');
57+
return console.log(error);
58+
}
5659
}
5760

61+
// Cache the user's premium status for later use
62+
SPOTIFY_USER_IS_PREMIUM = is_premium;
63+
5864
// Spotify Premium User: Start playback with our shuffled results song uris
5965
if (is_premium) {
6066
try {
@@ -164,6 +170,9 @@ function bind_playables_listeners() {
164170
alert('Failed to play tracks from the selected track in selected device Spotify player.');
165171
return console.log(error);
166172
}
173+
174+
// Enable the UI button to allow the user to reshuffle and play music
175+
ui_render_play_button('Reshuffle & Play', true);
167176
};
168177
playable.addEventListener('click', listener);
169178

0 commit comments

Comments
 (0)