Resume-able processes for loading library

This commit is contained in:
James Barnsley
2017-08-16 12:40:27 +12:00
parent 5b5e4a2351
commit 8e030cc936
10 changed files with 155 additions and 157 deletions

View File

@ -337,21 +337,22 @@ const MopidyMiddleware = (function(){
next(action)
// start our processor
store.dispatch(uiActions.startProcess('MOPIDY_ENQUEUE_URIS_PROCESSOR', 'Adding '+action.uris.length+' URI(s)'))
store.dispatch(uiActions.startProcess('MOPIDY_ENQUEUE_URIS_PROCESSOR', 'Adding '+action.uris.length+' URI(s)', {batches: batches}))
break
case 'MOPIDY_ENQUEUE_URIS_PROCESSOR':
// make sure we have some uris in the queue
if (store.getState().mopidy.enqueue_uris_batches && store.getState().mopidy.enqueue_uris_batches.length > 0){
if (action.data.batches && action.data.batches.length > 0){
var batches = store.getState().mopidy.enqueue_uris_batches
var batches = Object.assign([],action.data.batches)
var batch = batches[0]
var total_uris = 0
for (var i = 0; i < batches.length; i++){
total_uris += batches[i].uris.length
}
store.dispatch(uiActions.updateProcess('MOPIDY_ENQUEUE_URIS_PROCESSOR', 'Adding '+total_uris+' URI(s)'))
var next_batches = batches.shift()
store.dispatch(uiActions.updateProcess('MOPIDY_ENQUEUE_URIS_PROCESSOR', 'Adding '+total_uris+' URI(s)', {batches: next_batches}))
// no batches means we're done here
} else {
@ -406,8 +407,7 @@ const MopidyMiddleware = (function(){
// server requests before our next batch. It's a little crude but it means the server isn't
// locked until we're completely done.
setTimeout(
function(){
store.dispatch(mopidyActions.enqueueURIsBatchDone())
function(){
store.dispatch(uiActions.runProcess(action.type))
},
100
@ -852,29 +852,36 @@ const MopidyMiddleware = (function(){
**/
case 'MOPIDY_GET_LIBRARY_ALBUMS':
var last_run = store.getState().ui.processes.MOPIDY_LIBRARY_ALBUMS_PROCESSOR
store.dispatch({type: 'MOPIDY_LIBRARY_ALBUMS_CLEAR'})
if (!last_run){
instruct( socket, store, 'library.browse', { uri: 'local:directory?type=album' } )
.then( response => {
if (response.length <= 0) return
instruct( socket, store, 'library.browse', { uri: 'local:directory?type=album' } )
.then( response => {
if (response.length <= 0) return
var uris = helpers.arrayOf('uri',response)
store.dispatch({
type: 'MOPIDY_LIBRARY_ALBUMS_LOADED',
uris: uris
});
var uris = helpers.arrayOf('uri',response)
store.dispatch({
type: 'MOPIDY_LIBRARY_ALBUMS_LOADED',
uris: uris
});
// Start our process to load the full album objects
store.dispatch(uiActions.startProcess('MOPIDY_LIBRARY_ALBUMS_PROCESSOR','Loading '+uris.length+' local albums', {uris: uris}))
})
} else if (last_run.status == 'cancelled'){
store.dispatch(uiActions.resumeProcess('MOPIDY_LIBRARY_ALBUMS_PROCESSOR'))
} else if (last_run.status == 'finished'){
// TODO: do we want to force a refresh?
}
// Start our process to load the full album objects
store.dispatch(uiActions.startProcess('MOPIDY_LIBRARY_ALBUMS_PROCESSOR','Loading '+uris.length+' local albums', {uris: uris}))
})
break;
case 'MOPIDY_LIBRARY_ALBUMS_PROCESSOR':
if (store.getState().ui.processes['MOPIDY_LIBRARY_ALBUMS_PROCESSOR'] !== undefined){
var processor = store.getState().ui.processes['MOPIDY_LIBRARY_ALBUMS_PROCESSOR']
if (processor.cancelling){
if (processor.status == 'cancelling'){
store.dispatch(uiActions.processCancelled('MOPIDY_LIBRARY_ALBUMS_PROCESSOR'))
return false
}
@ -884,7 +891,7 @@ const MopidyMiddleware = (function(){
var uris_to_load = uris.splice(0,50)
if (uris_to_load.length > 0){
store.dispatch(uiActions.updateProcess('MOPIDY_LIBRARY_ALBUMS_PROCESSOR', 'Loading '+uris.length+' local albums'))
store.dispatch(uiActions.updateProcess('MOPIDY_LIBRARY_ALBUMS_PROCESSOR', 'Loading '+uris.length+' local albums', {uris: uris}))
store.dispatch(mopidyActions.getAlbums(uris_to_load, {name: 'MOPIDY_LIBRARY_ALBUMS_PROCESSOR', data: {uris: uris}}))
} else {
store.dispatch(uiActions.processFinished('MOPIDY_LIBRARY_ALBUMS_PROCESSOR'))
@ -1006,30 +1013,38 @@ const MopidyMiddleware = (function(){
**/
case 'MOPIDY_GET_LIBRARY_ARTISTS':
store.dispatch({type: 'MOPIDY_LIBRARY_ARTISTS_CLEAR'})
instruct( socket, store, 'library.browse', { uri: 'local:directory?type=artist' } )
.then( response => {
if (response.length <= 0) return
var last_run = store.getState().ui.processes.MOPIDY_LIBRARY_ARTISTS_PROCESSOR
var uris = helpers.arrayOf('uri',response)
if (!last_run){
instruct( socket, store, 'library.browse', { uri: 'local:directory?type=artist' } )
.then( response => {
if (response.length <= 0) return
store.dispatch({
type: 'MOPIDY_LIBRARY_ARTISTS_LOADED',
uris: uris
});
var uris = helpers.arrayOf('uri',response)
store.dispatch({
type: 'MOPIDY_LIBRARY_ARTISTS_LOADED',
uris: uris
});
// Start our process to load the full album objects
store.dispatch(uiActions.startProcess('MOPIDY_LIBRARY_ARTISTS_PROCESSOR','Loading '+uris.length+' local artists', {uris: uris}))
})
} else if (last_run.status == 'cancelled'){
store.dispatch(uiActions.resumeProcess('MOPIDY_LIBRARY_ARTISTS_PROCESSOR'))
} else if (last_run.status == 'finished'){
// TODO: do we want to force a refresh?
}
// Start our process to load the full album objects
store.dispatch(uiActions.startProcess('MOPIDY_LIBRARY_ARTISTS_PROCESSOR','Loading '+uris.length+' local artists', {uris: uris}))
})
break;
case 'MOPIDY_LIBRARY_ARTISTS_PROCESSOR':
if (store.getState().ui.processes['MOPIDY_LIBRARY_ARTISTS_PROCESSOR'] !== undefined){
var processor = store.getState().ui.processes['MOPIDY_LIBRARY_ARTISTS_PROCESSOR']
if (processor.cancelling){
if (processor.status == 'cancelling'){
store.dispatch(uiActions.processCancelled('MOPIDY_LIBRARY_ARTISTS_PROCESSOR'))
return false
}
@ -1039,7 +1054,7 @@ const MopidyMiddleware = (function(){
var uris_to_load = uris.splice(0,50)
if (uris_to_load.length > 0){
store.dispatch(uiActions.updateProcess('MOPIDY_LIBRARY_ARTISTS_PROCESSOR', 'Loading '+uris.length+' local artists'))
store.dispatch(uiActions.updateProcess('MOPIDY_LIBRARY_ARTISTS_PROCESSOR', 'Loading '+uris.length+' local artists', {uris: uris}))
store.dispatch(mopidyActions.getArtists(uris_to_load, {name: 'MOPIDY_LIBRARY_ARTISTS_PROCESSOR', data: {uris: uris}}))
} else {
store.dispatch(uiActions.processFinished('MOPIDY_LIBRARY_ARTISTS_PROCESSOR'))

View File

@ -1,4 +1,6 @@
import * as helpers from '../../helpers'
export default function reducer(mopidy = {}, action){
switch (action.type) {
@ -94,34 +96,6 @@ export default function reducer(mopidy = {}, action){
directory: action.data
});
case 'MOPIDY_ENQUEUE_URIS':
if (mopidy.enqueue_uris_batches){
var batches = [...mopidy.enqueue_uris_batches, ...action.batches]
} else {
var batches = Object.assign([],action.batches)
}
return Object.assign({}, mopidy, {
enqueue_uris_batches: batches
});
case 'MOPIDY_ENQUEUE_URIS_PROCESSOR_CANCEL':
return Object.assign({}, mopidy, {
enqueue_uris_batches: []
});
case 'MOPIDY_ENQUEUE_URIS_BATCH_DONE':
if (!mopidy.enqueue_uris_batches || mopidy.enqueue_uris_batches.length <= 0){
// Batches empty, so we've been cancelled
var batches = []
} else {
var batches = mopidy.enqueue_uris_batches
batches.shift()
}
return Object.assign({}, mopidy, {
enqueue_uris_batches: batches
});
/**
* Library
@ -133,7 +107,7 @@ export default function reducer(mopidy = {}, action){
} else {
var uris = action.uris
}
return Object.assign({}, mopidy, { library_playlists: uris })
return Object.assign({}, mopidy, { library_playlists: helpers.removeDuplicates(uris) })
case 'MOPIDY_LIBRARY_ARTISTS_LOADED':
if (mopidy.library_artists){
@ -141,7 +115,7 @@ export default function reducer(mopidy = {}, action){
} else {
var uris = action.uris
}
return Object.assign({}, mopidy, { library_artists: uris })
return Object.assign({}, mopidy, { library_artists: helpers.removeDuplicates(uris) })
case 'MOPIDY_LIBRARY_ALBUMS_LOADED':
if (mopidy.library_albums){
@ -149,36 +123,7 @@ export default function reducer(mopidy = {}, action){
} else {
var uris = action.uris
}
return Object.assign({}, mopidy, { library_albums: uris })
case 'MOPIDY_LIBRARY_PLAYLISTS_CLEAR':
return Object.assign({}, mopidy, { library_playlists: [] })
case 'MOPIDY_LIBRARY_ARTISTS_CLEAR':
return Object.assign({}, mopidy, { library_artists: [] })
case 'MOPIDY_LIBRARY_ALBUMS_CLEAR':
return Object.assign({}, mopidy, { library_albums: [] })
case 'MOPIDY_LIBRARY_PLAYLISTS_PROCESSOR_CANCELLED':
return Object.assign({}, mopidy, { library_playlists_status: 'cancelled' })
case 'MOPIDY_LIBRARY_PLAYLISTS_PROCESSOR_FINISHED':
return Object.assign({}, mopidy, { library_playlists_status: 'finished' })
case 'MOPIDY_LIBRARY_ARTISTS_PROCESSOR_CANCELLED':
return Object.assign({}, mopidy, { library_artists_status: 'cancelled' })
case 'MOPIDY_LIBRARY_ARTISTS_PROCESSOR_FINISHED':
return Object.assign({}, mopidy, { library_artists_status: 'finished' })
case 'MOPIDY_LIBRARY_ALBUMS_PROCESSOR_CANCELLED':
return Object.assign({}, mopidy, { library_albums_status: 'cancelled' })
case 'MOPIDY_LIBRARY_ALBUMS_PROCESSOR_FINISHED':
return Object.assign({}, mopidy, { library_albums_status: 'finished' })
return Object.assign({}, mopidy, { library_albums: helpers.removeDuplicates(uris) })
default:
return mopidy

View File

@ -1298,8 +1298,15 @@ export function reorderPlaylistTracks( uri, range_start, range_length, insert_be
export function getLibraryPlaylists(){
return (dispatch, getState) => {
dispatch({type: 'SPOTIFY_LIBRARY_PLAYLISTS_CLEAR'})
dispatch(uiActions.startProcess('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR','Loading Spotify playlists', {next: 'me/playlists?limit=50'}))
var last_run = getState().ui.processes.SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR
if (!last_run){
dispatch(uiActions.startProcess('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR','Loading Spotify playlists', {next: 'me/playlists?limit=50'}))
} else if (last_run.status == 'cancelled'){
dispatch(uiActions.resumeProcess('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR'))
} else if (last_run.status == 'finished'){
// TODO: do we want to force a refresh?
}
}
}
@ -1317,7 +1324,7 @@ export function getLibraryPlaylistsProcessor(data){
if (getState().ui.processes['SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR'] !== undefined){
var processor = getState().ui.processes['SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR']
if (processor.cancelling){
if (processor.status == 'cancelling'){
dispatch(uiActions.processCancelled('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR'))
return false
}
@ -1328,7 +1335,7 @@ export function getLibraryPlaylistsProcessor(data){
var total = response.total
var loaded = getState().spotify.library_playlists.length
var remaining = total - loaded
dispatch(uiActions.updateProcess('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR', 'Loading '+remaining+' Spotify playlists'))
dispatch(uiActions.updateProcess('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR', 'Loading '+remaining+' Spotify playlists', {next: response.next}))
dispatch(uiActions.runProcess('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR', {next: response.next}))
} else {
dispatch(uiActions.processFinished('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR'))
@ -1344,8 +1351,15 @@ export function getLibraryPlaylistsProcessor(data){
export function getLibraryArtists(){
return (dispatch, getState) => {
dispatch({type: 'SPOTIFY_LIBRARY_ARTISTS_CLEAR'})
dispatch(uiActions.startProcess('SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR','Loading Spotify artists', {next: 'me/following?type=artist&limit=50'}))
var last_run = getState().ui.processes.SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR
if (!last_run){
dispatch(uiActions.startProcess('SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR','Loading Spotify artists', {next: 'me/following?type=artist&limit=50'}))
} else if (last_run.status == 'cancelled'){
dispatch(uiActions.resumeProcess('SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR'))
} else if (last_run.status == 'finished'){
// TODO: do we want to force a refresh?
}
}
}
@ -1363,7 +1377,7 @@ export function getLibraryArtistsProcessor(data){
if (getState().ui.processes['SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR'] !== undefined){
var processor = getState().ui.processes['SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR']
if (processor.cancelling){
if (processor.status == 'cancelling'){
dispatch(uiActions.processCancelled('SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR'))
return false
}
@ -1374,7 +1388,7 @@ export function getLibraryArtistsProcessor(data){
var total = response.artists.total
var loaded = getState().spotify.library_artists.length
var remaining = total - loaded
dispatch(uiActions.updateProcess('SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR', 'Loading '+remaining+' Spotify artists'))
dispatch(uiActions.updateProcess('SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR', 'Loading '+remaining+' Spotify artists', {next: response.artists.next}))
dispatch(uiActions.runProcess('SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR', {next: response.artists.next}))
} else {
dispatch(uiActions.processFinished('SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR'))
@ -1390,8 +1404,15 @@ export function getLibraryArtistsProcessor(data){
export function getLibraryAlbums(){
return (dispatch, getState) => {
dispatch({type: 'SPOTIFY_LIBRARY_ALBUMS_CLEAR'})
dispatch(uiActions.startProcess('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR','Loading Spotify albums', {next: 'me/albums?limit=50'}))
var last_run = getState().ui.processes.SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR
if (!last_run){
dispatch(uiActions.startProcess('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR','Loading Spotify albums', {next: 'me/albums?limit=50'}))
} else if (last_run.status == 'cancelled'){
dispatch(uiActions.updateProcess('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR','Loading Spotify albums', {next: 'me/albums?limit=50'}))
} else if (last_run.status == 'finished'){
// TODO: do we want to force a refresh?
}
}
}
@ -1409,7 +1430,7 @@ export function getLibraryAlbumsProcessor(data){
if (getState().ui.processes['SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR'] !== undefined){
var processor = getState().ui.processes['SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR']
if (processor.cancelling){
if (processor.status == 'cancelling'){
dispatch(uiActions.processCancelled('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR'))
return false
}
@ -1420,7 +1441,7 @@ export function getLibraryAlbumsProcessor(data){
var total = response.total
var loaded = getState().spotify.library_albums.length
var remaining = total - loaded
dispatch(uiActions.updateProcess('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR', 'Loading '+remaining+' Spotify albums'))
dispatch(uiActions.updateProcess('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR', 'Loading '+remaining+' Spotify albums', {next: response.next}))
dispatch(uiActions.runProcess('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR', {next: response.next}))
} else {
dispatch(uiActions.processFinished('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR'))

View File

@ -156,7 +156,7 @@ export default function reducer(spotify = {}, action){
} else {
var uris = action.uris
}
return Object.assign({}, spotify, { library_playlists: uris })
return Object.assign({}, spotify, { library_playlists: helpers.removeDuplicates(uris) })
case 'SPOTIFY_LIBRARY_ARTISTS_LOADED':
if (spotify.library_artists){
@ -164,7 +164,7 @@ export default function reducer(spotify = {}, action){
} else {
var uris = action.uris
}
return Object.assign({}, spotify, { library_artists: uris })
return Object.assign({}, spotify, { library_artists: helpers.removeDuplicates(uris) })
case 'SPOTIFY_LIBRARY_ALBUMS_LOADED':
if (spotify.library_albums){
@ -172,7 +172,7 @@ export default function reducer(spotify = {}, action){
} else {
var uris = action.uris
}
return Object.assign({}, spotify, { library_albums: uris })
return Object.assign({}, spotify, { library_albums: helpers.removeDuplicates(uris) })
case 'SPOTIFY_LIBRARY_TRACKS_LOADED':
case 'SPOTIFY_LIBRARY_TRACKS_LOADED_MORE':
@ -210,25 +210,6 @@ export default function reducer(spotify = {}, action){
return Object.assign({}, spotify, { library_albums: [] })
case 'SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR_CANCELLED':
return Object.assign({}, spotify, { library_playlists_status: 'cancelled' })
case 'SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR_FINISHED':
return Object.assign({}, spotify, { library_playlists_status: 'finished' })
case 'SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR_CANCELLED':
return Object.assign({}, spotify, { library_artists_status: 'cancelled' })
case 'SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR_FINISHED':
return Object.assign({}, spotify, { library_artists_status: 'finished' })
case 'SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR_CANCELLED':
return Object.assign({}, spotify, { library_albums_status: 'cancelled' })
case 'SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR_FINISHED':
return Object.assign({}, spotify, { library_albums_status: 'finished' })
case 'SPOTIFY_LIBRARY_ALBUM_CHECK':
var items = Object.assign([], spotify.library_albums)
var index = items.indexOf(action.key)

View File

@ -177,6 +177,13 @@ export function startProcess(key,message,data = {}){
}
}
export function resumeProcess(key,message,data = {}){
return {
type: 'RESUME_PROCESS',
key: key
}
}
export function updateProcess(key,message,data = {}){
return {
type: 'UPDATE_PROCESS',

View File

@ -127,6 +127,18 @@ const UIMiddleware = (function(){
type: action.key,
data: action.data
})
store.dispatch({
type: action.key+'_STARTED'
})
next(action)
break
case 'RESUME_PROCESS':
store.dispatch({
type: action.key,
data: store.getState().ui.processes[action.key].data
})
console.log('resuming',store.getState().ui.processes[action.key].data)
next(action)
break

View File

@ -136,28 +136,36 @@ export default function reducer(ui = {}, action){
processes[action.key] = {
key: action.key,
message: action.message,
status: 'running',
data: data
}
return Object.assign({}, ui, {processes: processes})
case 'RESUME_PROCESS':
var processes = Object.assign({}, (ui.processes ? ui.processes : {}))
if (processes[action.key]){
processes[action.key] = Object.assign({}, processes[action.key], {status: 'running'})
}
return Object.assign({}, ui, {processes: processes})
case 'CANCEL_PROCESS':
var processes = Object.assign({}, (ui.processes ? ui.processes : {}))
if (processes[action.key]){
processes[action.key] = Object.assign(
{},
processes[action.key],
{
cancelling: true
}
)
processes[action.key] = Object.assign({}, processes[action.key], {status: 'cancelling'})
}
return Object.assign({}, ui, {processes: processes})
case 'PROCESS_CANCELLED':
var processes = Object.assign({}, (ui.processes ? ui.processes : {}))
if (processes[action.key]){
processes[action.key] = Object.assign({}, processes[action.key], {status: 'cancelled'})
}
return Object.assign({}, ui, {processes: processes})
case 'PROCESS_FINISHED':
var processes = Object.assign({}, (ui.processes ? ui.processes : {}))
if (processes[action.key]){
delete processes[action.key]
processes[action.key] = Object.assign({}, processes[action.key], {status: 'finished'})
}
return Object.assign({}, ui, {processes: processes})