Separating track loading from track adding for spotify playlists

This commit is contained in:
James Barnsley
2017-09-08 08:37:16 +12:00
parent f24399c897
commit 56e4c022b3
4 changed files with 73 additions and 21 deletions

View File

@ -45,7 +45,7 @@
// Identify whether we're in test mode, and need to fetch non-minified assets
var ui = JSON.parse( localStorage.getItem('ui') );
if (typeof(ui) === 'undefined' || typeof(ui.test_mode) === 'undefined' || !ui.test_mode){
if (typeof(ui) === 'undefined' || !ui || typeof(ui.test_mode) === 'undefined' || !ui.test_mode){
js.src = '/iris/app.min.js?v='+build_name;
css.href = '/iris/app.min.css?v='+build_name;
} else {

View File

@ -282,14 +282,14 @@ const MopidyMiddleware = (function(){
// make sure we didn't get this playlist from Mopidy-Spotify
// if we did, we'd have a cached version on server so no need to fetch
if (!store.getState().core.playlists[action.uri].is_mopidy){
store.dispatch(spotifyActions.getAllPlaylistTracks(action.uri))
store.dispatch(spotifyActions.getPlaylistTracksForPlaying(action.uri))
break
}
// it's a spotify playlist that we haven't loaded
// we need to fetch via HTTP API to avoid timeout
} else if (helpers.uriSource(action.uri) == 'spotify' && store.getState().spotify.enabled){
store.dispatch(spotifyActions.getAllPlaylistTracks(action.uri))
store.dispatch(spotifyActions.getPlaylistTracksForPlaying(action.uri))
break
// Not in index, and Spotify HTTP not enabled, so just play it as-is
@ -348,10 +348,17 @@ const MopidyMiddleware = (function(){
))
break
case 'MOPIDY_ENQUEUE_URIS_PROCESSOR':
case 'MOPIDY_ENQUEUE_URIS_PROCESSOR':
var last_run = store.getState().ui.processes.MOPIDY_ENQUEUE_URIS_PROCESSOR
// Cancelling
if (last_run && last_run.status == 'cancelling'){
store.dispatch(uiActions.processCancelled('MOPIDY_ENQUEUE_URIS_PROCESSOR'))
return
// make sure we have some uris in the queue
if (action.data.batches && action.data.batches.length > 0){
} else if (action.data.batches && action.data.batches.length > 0){
var batches = Object.assign([],action.data.batches)
var batch = batches[0]

View File

@ -1236,27 +1236,68 @@ export function getPlaylist(uri){
*
* Recursively get .next until we have all tracks
**/
function loadNextPlaylistTracksBatch(dispatch, getState, uri, tracks, lastResponse){
if( lastResponse.next ){
sendRequest(dispatch, getState, lastResponse.next)
.then( response => {
tracks = [...tracks, ...response.items]
loadNextPlaylistTracksBatch(dispatch, getState, uri, tracks, response)
});
}else{
dispatch({
type: 'SPOTIFY_ALL_PLAYLIST_TRACKS_LOADED_FOR_PLAYING',
uri: uri,
tracks: tracks
});
export function getPlaylistTracksForPlaying(uri){
return (dispatch, getState) => {
dispatch(uiActions.startProcess(
'SPOTIFY_GET_PLAYLIST_TRACKS_FOR_PLAYING_PROCESSOR',
'Loading playlist tracks',
{
next: 'users/'+ helpers.getFromUri('userid',uri) +'/playlists/'+ helpers.getFromUri('playlistid',uri) +'/tracks?market='+getState().core.country
}
))
}
}
export function getAllPlaylistTracks(uri){
export function getPlaylistTracksForPlayingProcessor(data){
return (dispatch, getState) => {
sendRequest(dispatch, getState, 'users/'+ helpers.getFromUri('userid',uri) +'/playlists/'+ helpers.getFromUri('playlistid',uri) +'/tracks?market='+getState().core.country)
sendRequest(dispatch, getState, data.next)
.then( response => {
loadNextPlaylistTracksBatch(dispatch, getState, uri, response.items, response)
// Check to see if we've been cancelled
if (getState().ui.processes['SPOTIFY_GET_PLAYLIST_TRACKS_FOR_PLAYING_PROCESSOR'] !== undefined){
var processor = getState().ui.processes['SPOTIFY_GET_PLAYLIST_TRACKS_FOR_PLAYING_PROCESSOR']
if (processor.status == 'cancelling'){
dispatch(uiActions.processCancelled('SPOTIFY_GET_PLAYLIST_TRACKS_FOR_PLAYING_PROCESSOR'))
return false
}
}
// Add on our new batch of loaded tracks
var uris = []
var new_uris = []
for (var i = 0; i < response.items.length; i++){
new_uris.push(response.items[i].track.uri)
}
if (data.uris){
uris = [...data.uris, ...new_uris];
} else {
uris = new_uris;
}
// We got a next link, so we've got more work to be done
if (response.next){
dispatch(uiActions.updateProcess(
'SPOTIFY_GET_PLAYLIST_TRACKS_FOR_PLAYING_PROCESSOR',
'Loading '+(response.total-uris.length)+' playlist tracks',
{
next: response.next,
total: response.total,
remaining: response.total - uris.length
}
))
dispatch(uiActions.runProcess(
'SPOTIFY_GET_PLAYLIST_TRACKS_FOR_PLAYING_PROCESSOR',
{
next: response.next,
uris: uris
}
))
} else {
dispatch(mopidyActions.playURIs(uris))
dispatch(uiActions.processFinished('SPOTIFY_GET_PLAYLIST_TRACKS_FOR_PLAYING_PROCESSOR'))
}
});
}
}

View File

@ -245,6 +245,10 @@ const SpotifyMiddleware = (function(){
store.dispatch(spotifyActions.getLibraryAlbumsProcessor(action.data))
break
case 'SPOTIFY_GET_PLAYLIST_TRACKS_FOR_PLAYING_PROCESSOR':
store.dispatch(spotifyActions.getPlaylistTracksForPlayingProcessor(action.data))
break
case 'SPOTIFY_LIBRARY_ALBUMS_LOADED':
var albums = []
for (var i = 0; i < action.albums.length; i++){