Skip to content

Commit

Permalink
In-memory search results cache
Browse files Browse the repository at this point in the history
- Switching between type and provider now negates need for repeat queries
- Mopidy caching varies, so for some providers this has a huge performance gain (eg YouTube)
- Spotify will requery anyway, but the browser handles query caching
  • Loading branch information
jaedb committed Oct 10, 2023
1 parent bec0cf9 commit 8dfd585
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/js/services/core/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ const CoreMiddleware = (function () {
// Trigger reducer immediately; this will hose out any previous results
next(action);

if (providers.includes('spotify:')) {
if (providers.includes('spotify')) {
store.dispatch(spotifyActions.getSearchResults(query));
}
store.dispatch(mopidyActions.getSearchResults(
query,
providers.filter((i) => i !== 'spotify:'), // Omit Spotify; handled above
providers.filter((i) => i !== 'spotify'), // Omit Spotify; handled above
));
break;
}
Expand Down
28 changes: 18 additions & 10 deletions src/js/services/mopidy/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
indexToArray,
} from '../../util/arrays';
import { getProvider, getSortSelector } from '../../util/selectors';
import { iterate } from 'localforage';

const mopidyActions = require('./actions.js');
const coreActions = require('../core/actions.js');
Expand Down Expand Up @@ -268,28 +269,40 @@ const MopidyMiddleware = (function () {
method = 'library.search',
data,
} = queue.shift();
const resultKey = getSearchResultKey({ provider, type, term });
const processKey = 'MOPIDY_GET_SEARCH_RESULTS';
const processor = store.getState().ui.processes[processKey];

if (processor && processor.status === 'cancelling') {
store.dispatch(uiActions.processCancelled('MOPIDY_GET_SEARCH_RESULTS'));
return;
}

const iterateNext = (store, queue) => {
if (queue.length) {
processSearchQueue(store, queue);
} else {
store.dispatch(uiActions.processFinished(processKey));
}
}

store.dispatch(uiActions.updateProcess(
processKey,
{
content: i18n(
'services.mopidy.searching',
{
provider: titleCase(provider.replace(':', '')),
provider: titleCase(provider),
type: requestType,
},
),
remaining: queue.length,
},
));

const resultKey = getSearchResultKey({ provider, type, term });
if (resultKey in store.getState().core.search_results) {
iterateNext(store, queue);
return;
}

// Each type has a different method of formatting and destructuring.
const processResults = {
Expand Down Expand Up @@ -377,12 +390,7 @@ const MopidyMiddleware = (function () {
processResults[requestType](response),
));
}

if (queue.length) {
processSearchQueue(store, queue);
} else {
store.dispatch(uiActions.processFinished(processKey));
}
iterateNext(store, queue);
},
);
};
Expand Down Expand Up @@ -1239,7 +1247,7 @@ const MopidyMiddleware = (function () {
provider,
requestType: type,
data: {
uris: [provider],
uris: [`${provider}:`],
},
};
switch (type) {
Expand Down
3 changes: 1 addition & 2 deletions src/js/util/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,7 @@ const upgradeSpotifyPlaylistUri = function (uri) {
return upgradeSpotifyPlaylistUris([uri])[0];
};

const getSearchResultKey = ({ provider, type, term }) =>
[provider.replace(':', ''), type, term].join(':');
const getSearchResultKey = ({ provider, type, term }) => [provider, type, term].join(':');

export {
debounce,
Expand Down
6 changes: 4 additions & 2 deletions src/js/util/useSearchQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const useSearchQuery = () => {
type = 'all',
providers: rawProviders = 'all',
} = useParams();
const allProviders = useSelector((state) => state.mopidy?.uri_schemes || []);
const allProviders = useSelector(
({ mopidy: { uri_schemes } }) => uri_schemes || []
).map((str) => str.replace(/:/g,''));
const providers = rawProviders == 'all'
? [...allProviders]
: rawProviders.split(',').filter((str) => allProviders.indexOf(str) > -1);
const providersString = providers.join(',').replace(/:/g,'');
const providersString = providers.join(',');

return {
term,
Expand Down
7 changes: 2 additions & 5 deletions src/js/views/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ const Search = () => {

useEffect(() => {
dispatch(setWindowTitle('Search'));
$(document).find('.search-form input').focus();
$(document).find('.search-form input').trigger('focus');
}, []);

useEffect(() => {
if (term) {
console.debug('STARTING SEARCH', { term, type, providers })
dispatch(setWindowTitle(i18n('search.title_window', { term: decodeURIComponent(term) })));
dispatch(startSearch({ term, type, providers }));
}
}, [])
}, [providersString, type, term])

const onSubmit = (term) => {
updateSearchQuery(term, providers);
Expand All @@ -57,8 +56,6 @@ const Search = () => {
const onReset = () => navigate('/search');

const onProvidersChange = (providers) => {
console.debug(providers)
// ON BLUR then trigger search event
updateSearchQuery(term, providers)
dispatch(hideContextMenu());
}
Expand Down

0 comments on commit 8dfd585

Please sign in to comment.