diff --git a/src/js/services/mopidy/actions.js b/src/js/services/mopidy/actions.js index ab78b5cb..cc42d5d3 100755 --- a/src/js/services/mopidy/actions.js +++ b/src/js/services/mopidy/actions.js @@ -49,7 +49,7 @@ export function changeTrack( tlid ){ } } -export function playURIs( uris, from_uri = null ){ +export function playURIs(uris, from_uri = null){ return { type: 'MOPIDY_PLAY_URIS', uris: uris, @@ -57,7 +57,7 @@ export function playURIs( uris, from_uri = null ){ } } -export function enqueueURIs( uris, from_uri = null, next = false, at_position = null ){ +export function enqueueURIs(uris, from_uri = null, next = false, at_position = null){ return { type: 'MOPIDY_ENQUEUE_URIS', uris: uris, @@ -67,6 +67,18 @@ export function enqueueURIs( uris, from_uri = null, next = false, at_position = } } +export function enqueueURIsBatchDone(){ + return { + type: 'MOPIDY_ENQUEUE_URIS_BATCH_DONE' + } +} + +export function enqueueUrisProcessor(){ + return { + type: 'MOPIDY_ENQUEUE_URIS_PROCESSOR' + } +} + export function playPlaylist(uri){ return { type: 'MOPIDY_PLAY_PLAYLIST', diff --git a/src/js/services/mopidy/middleware.js b/src/js/services/mopidy/middleware.js index db827df8..cd9351d4 100755 --- a/src/js/services/mopidy/middleware.js +++ b/src/js/services/mopidy/middleware.js @@ -284,8 +284,44 @@ const MopidyMiddleware = (function(){ break case 'MOPIDY_ENQUEUE_URIS': - var uris_added = 0 - var remaining_uris = Object.assign([], action.uris) + console.log(action) + + // split into batches + var uris = Object.assign([], action.uris) + var batches = [] + while (uris.length > 0){ + batches.push({ + uris: uris.splice(0,5), + at_position: action.at_position, + next: action.next, + from_uri: action.from_uri + }) + } + + // pass off this modified action to the reducer (and other middleware) + action.batches = batches + next(action) + + // start our processor + store.dispatch(mopidyActions.enqueueUrisProcessor()) + break + + case 'MOPIDY_ENQUEUE_URIS_PROCESSOR': + console.log(action) + + // make sure we have some uris in the queue + if (store.getState().mopidy.enqueue_uris_batches && store.getState().mopidy.enqueue_uris_batches.length > 0){ + + var batches = store.getState().mopidy.enqueue_uris_batches + var batch = batches[0] + store.dispatch(uiActions.startProcess('MOPIDY_ENQUEUE_URIS', 'Adding '+(batches.length*5)+' URI(s)')) + + // no batches means we're done here + } else { + store.dispatch(uiActions.stopProcess('MOPIDY_ENQUEUE_URIS')) + break + } + var current_track = store.getState().ui.current_track var current_tracklist = store.getState().ui.current_tracklist var current_track_index = -1 @@ -299,63 +335,34 @@ const MopidyMiddleware = (function(){ } } - let process_batch = function(){ - - // process is not running - if (store.getState().ui.processes && store.getState().ui.processes[action.type]){ - if (store.getState().ui.processes[action.type].cancelling){ - // recognise as cancelled - store.dispatch(uiActions.stopProcess(action.type)) - return false - } - } else { - return false - } - - // update our process details - store.dispatch(uiActions.startProcess(action.type, 'Adding '+remaining_uris.length+' URI(s)')) - - var params = {uris: remaining_uris.splice(0,5)} - if (action.next && current_track_index > -1){ - params.at_position = current_track_index + uris_added + 1 - } else if (action.at_position){ - params.at_position = action.at_position - } - - instruct(socket, store, 'tracklist.add', params) - .then( response => { - - // append our counter - uris_added += response.length - - // add metadata to queue - var tlids = [] - for (var i = 0; i < response.length; i++){ - tlids.push(response[i].tlid) - } - store.dispatch(pusherActions.addQueueMetadata(tlids, action.from_uri)) - - // still more URIs? run again in 100ms - // this gives our server time to handle other requests - // crude, but prevents locking the server - if (remaining_uris.length > 0){ - setTimeout( - function(){ - process_batch() - }, - 100 - ) - - // all done - } else { - store.dispatch(uiActions.stopProcess(action.type)) - } - }) + var params = {uris: batch.uris} + if (batch.next && current_track_index > -1){ + params.at_position = current_track_index + 1 + } else if (batch.at_position){ + params.at_position = batch.at_position } - // start processing - store.dispatch(uiActions.startProcess(action.type, 'Adding '+remaining_uris.length+' URI(s)')) - process_batch() + instruct(socket, store, 'tracklist.add', params) + .then( response => { + + // add metadata to queue + var tlids = [] + for (var i = 0; i < response.length; i++){ + tlids.push(response[i].tlid) + } + store.dispatch(pusherActions.addQueueMetadata(tlids, batch.from_uri)) + + // still more URIs? run again in 100ms + // this gives our server time to handle other requests + // crude, but prevents locking the server + setTimeout( + function(){ + store.dispatch(mopidyActions.enqueueURIsBatchDone()) + store.dispatch(mopidyActions.enqueueUrisProcessor()) + }, + 100 + ) + }) break @@ -383,6 +390,7 @@ const MopidyMiddleware = (function(){ store.dispatch(pusherActions.addQueueMetadata(tlids, action.from_uri)) } else { store.dispatch(uiActions.createNotification('Failed to add some URI(s)', 'bad')) + console.error('Failed to add some URI(s)', response) } // add the rest of our uris (if any) diff --git a/src/js/services/mopidy/reducer.js b/src/js/services/mopidy/reducer.js index 34485cf1..92d0e0c5 100755 --- a/src/js/services/mopidy/reducer.js +++ b/src/js/services/mopidy/reducer.js @@ -89,6 +89,28 @@ 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_BATCH_DONE': + if (!mopidy.enqueue_uris_batches || mopidy.enqueue_uris_batches.length <= 0){ + var batches = [] + console.error('Cannot remove batch when queue empty',action) + } else { + var batches = mopidy.enqueue_uris_batches + batches.shift() + } + return Object.assign({}, mopidy, { + enqueue_uris_batches: batches + }); + default: return mopidy }