feat: simple but efficient cache of home data

This commit is contained in:
Roberto Tonino 2020-09-21 19:12:14 +02:00
parent 63c86c1267
commit 5593dc085e
12 changed files with 114 additions and 49 deletions

File diff suppressed because one or more lines are too long

View File

@ -5,11 +5,21 @@
<keep-alive> <keep-alive>
<router-view <router-view
v-if="!$route.meta.notKeepAlive"
v-show="!loading" v-show="!loading"
:key="$route.fullPath" :key="$route.fullPath"
:perform-scrolled-search="performScrolledSearch" :perform-scrolled-search="performScrolledSearch"
exclude=""
></router-view> ></router-view>
</keep-alive> </keep-alive>
<router-view
v-if="$route.meta.notKeepAlive"
v-show="!loading"
:key="$route.fullPath"
:perform-scrolled-search="performScrolledSearch"
exclude=""
></router-view>
</div> </div>
</section> </section>
</template> </template>

View File

@ -127,7 +127,6 @@ export default {
} }
}, },
initQueue(data) { initQueue(data) {
console.log('this.initQueue')
const { const {
queue: initQueue, queue: initQueue,
queueComplete: initQueueComplete, queueComplete: initQueueComplete,
@ -159,7 +158,6 @@ export default {
} }
}, },
addToQueue(queueItem, current = false) { addToQueue(queueItem, current = false) {
console.log('this.addToQueue')
if (Array.isArray(queueItem)) { if (Array.isArray(queueItem)) {
if (queueItem.length > 1) { if (queueItem.length > 1) {
queueItem.forEach((item, i) => { queueItem.forEach((item, i) => {
@ -210,7 +208,6 @@ export default {
updateQueue(update) { updateQueue(update) {
// downloaded and failed default to false? // downloaded and failed default to false?
const { uuid, downloaded, failed, progress, conversion, error, data, errid } = update const { uuid, downloaded, failed, progress, conversion, error, data, errid } = update
console.log('this.updateQueue', !!this.queueList[uuid])
if (uuid && this.queue.indexOf(uuid) > -1) { if (uuid && this.queue.indexOf(uuid) > -1) {
if (downloaded) { if (downloaded) {
@ -232,7 +229,6 @@ export default {
} }
}, },
removeFromQueue(uuid) { removeFromQueue(uuid) {
console.log('this.removeFromQueue')
let index = this.queue.indexOf(uuid) let index = this.queue.indexOf(uuid)
if (index > -1) { if (index > -1) {
@ -241,7 +237,6 @@ export default {
} }
}, },
removeAllDownloads(currentItem) { removeAllDownloads(currentItem) {
console.log('this.removeFromQueue', currentItem)
this.queueComplete = [] this.queueComplete = []
if (!currentItem) { if (!currentItem) {
@ -257,7 +252,6 @@ export default {
} }
}, },
removedFinishedDownloads() { removedFinishedDownloads() {
console.log('this.removedFinishedDownloads')
this.queueComplete.forEach(uuid => { this.queueComplete.forEach(uuid => {
this.$delete(this.queueList, uuid) this.$delete(this.queueList, uuid)
}) })
@ -279,11 +273,9 @@ export default {
localStorage.setItem('downloadTabOpen', !isHidden) localStorage.setItem('downloadTabOpen', !isHidden)
}, },
cleanQueue() { cleanQueue() {
console.log('this.cleanQueue')
socket.emit('removeFinishedDownloads') socket.emit('removeFinishedDownloads')
}, },
cancelQueue() { cancelQueue() {
console.log('this.cancelQueue')
socket.emit('cancelAllDownloads') socket.emit('cancelAllDownloads')
}, },
openDownloadsFolder() { openDownloadsFolder() {
@ -307,12 +299,9 @@ export default {
document.addEventListener('mousemove', this.handleDrag) document.addEventListener('mousemove', this.handleDrag)
}, },
startDownload(uuid) { startDownload(uuid) {
console.log('this.startDownload')
this.$set(this.queueList[uuid], 'status', 'downloading') this.$set(this.queueList[uuid], 'status', 'downloading')
}, },
finishDownload(uuid) { finishDownload(uuid) {
console.log('this.finishDownload')
let isInQueue = this.queue.indexOf(uuid) > -1 let isInQueue = this.queue.indexOf(uuid) > -1
if (!isInQueue) return if (!isInQueue) return
@ -332,7 +321,6 @@ export default {
} }
}, },
startConversion(uuid) { startConversion(uuid) {
console.log('this.startConversion')
this.$set(this.queueList[uuid], 'status', 'converting') this.$set(this.queueList[uuid], 'status', 'converting')
this.$set(this.queueList[uuid], 'conversion', 0) this.$set(this.queueList[uuid], 'conversion', 0)
}, },

View File

@ -299,13 +299,16 @@ export default {
}, },
reloadTabs() { reloadTabs() {
this.$refs.reloadButton.classList.add('spin') this.$refs.reloadButton.classList.add('spin')
socket.emit('update_userFavorites') socket.emit('update_userFavorites')
if (localStorage.getItem('spotifyUser'))
if (localStorage.getItem('spotifyUser')) {
socket.emit('update_userSpotifyPlaylists', localStorage.getItem('spotifyUser')) socket.emit('update_userSpotifyPlaylists', localStorage.getItem('spotifyUser'))
}
}, },
updated_userFavorites(data) { updated_userFavorites(data) {
const { tracks, albums, artists, playlists } = data const { tracks, albums, artists, playlists } = data
this.tracks = tracks this.tracks = typeof tracks === 'string' ? JSON.parse(tracks) : tracks
this.albums = albums this.albums = albums
this.artists = artists this.artists = artists
this.playlists = playlists this.playlists = playlists

View File

@ -82,16 +82,26 @@ import { socket } from '@/utils/socket'
import { showView } from '@js/tabs' import { showView } from '@js/tabs'
import Downloads from '@/utils/downloads' import Downloads from '@/utils/downloads'
import { getHomeData } from '@/data/home'
export default { export default {
name: 'the-home-tab',
data() { data() {
return { return {
playlists: [], playlists: [],
albums: [] albums: []
} }
}, },
async created() {
// if (localStorage.getItem('arl')) {
// this.$refs.notLogged.classList.add('hide')
// }
const homeData = await getHomeData()
this.initHome(homeData)
},
computed: { computed: {
...mapGetters(['getHomeData', 'isLoggedIn']), ...mapGetters([/* 'getHomeData', */ 'isLoggedIn']),
needToWait() { needToWait() {
return this.getHomeData.albums.data.length === 0 && this.getHomeData.playlists.data.length === 0 return this.getHomeData.albums.data.length === 0 && this.getHomeData.playlists.data.length === 0
} }
@ -128,13 +138,6 @@ export default {
this.initHome(this.getHomeData) this.initHome(this.getHomeData)
} }
} }
},
mounted() {
// if (localStorage.getItem('arl')) {
// this.$refs.notLogged.classList.add('hide')
// }
this.checkIfWaitData(this.getHomeData)
} }
} }
</script> </script>

View File

@ -772,10 +772,12 @@ export default {
} }
}, },
revertSettings() { revertSettings() {
this.settings = { ...this.lastSettings } // this.settings = { ...this.lastSettings }
this.settings = JSON.parse(JSON.stringify(this.lastSettings))
}, },
revertCredentials() { revertCredentials() {
this.spotifyCredentials = { ...this.lastCredentials } // this.spotifyCredentials = { ...this.lastCredentials }
this.spotifyCredentials = JSON.parse(JSON.stringify(this.lastCredentials))
this.spotifyUser = (' ' + this.lastUser).slice(1) this.spotifyUser = (' ' + this.lastUser).slice(1)
}, },
copyARLtoClipboard() { copyARLtoClipboard() {
@ -798,8 +800,12 @@ export default {
localStorage.setItem('previewVolume', this.previewVolume.preview_max_volume) localStorage.setItem('previewVolume', this.previewVolume.preview_max_volume)
}, },
saveSettings() { saveSettings() {
this.lastSettings = { ...this.settings } // this.lastSettings = { ...this.settings }
this.lastCredentials = { ...this.spotifyFeatures } // this.lastCredentials = { ...this.spotifyFeatures }
this.lastSettings = JSON.parse(JSON.stringify(this.settings))
this.lastCredentials = JSON.parse(JSON.stringify(this.spotifyFeatures))
let changed = false let changed = false
if (this.lastUser != this.spotifyUser) { if (this.lastUser != this.spotifyUser) {
// force cloning without linking // force cloning without linking
@ -821,11 +827,15 @@ export default {
// this.defaultSettings = { ...this.getDefaultSettings } // this.defaultSettings = { ...this.getDefaultSettings }
// }, // },
loadSettings() { loadSettings() {
this.lastSettings = { ...this.getSettings } // this.lastSettings = { ...this.getSettings }
this.lastCredentials = { ...this.getCredentials } this.lastSettings = JSON.parse(JSON.stringify(this.getSettings))
// this.lastCredentials = { ...this.getCredentials }
this.lastCredentials = JSON.parse(JSON.stringify(this.getCredentials))
this.settings = { ...this.getSettings } // this.settings = { ...this.getSettings }
this.spotifyFeatures = { ...this.getCredentials } this.settings = JSON.parse(JSON.stringify(this.getSettings))
// this.spotifyFeatures = { ...this.getCredentials }
this.spotifyFeatures = JSON.parse(JSON.stringify(this.getCredentials))
}, },
login() { login() {
let newArl = this.$refs.loginInput.value.trim() let newArl = this.$refs.loginInput.value.trim()
@ -868,7 +878,8 @@ export default {
toast(this.$t('settings.toasts.update'), 'settings') toast(this.$t('settings.toasts.update'), 'settings')
}, },
resetSettings() { resetSettings() {
this.settings = { ...this.getDefaultSettings } // this.settings = { ...this.getDefaultSettings }
this.settings = JSON.parse(JSON.stringify(this.getDefaultSettings))
} }
} }
} }

21
src/data/charts.js Normal file
View File

@ -0,0 +1,21 @@
import { socket } from '@/utils/socket'
let chartsData = {}
let cached = false
export function getChartsData() {
if (cached) {
return chartsData
} else {
socket.emit('get_charts_data')
return new Promise((resolve, reject) => {
socket.on('init_charts', data => {
chartsData = data
cached = true
resolve(data)
})
})
}
}

21
src/data/home.js Normal file
View File

@ -0,0 +1,21 @@
import { socket } from '@/utils/socket'
let homeData = {}
let cached = false
export function getHomeData() {
if (cached) {
return homeData
} else {
socket.emit('get_home_data')
return new Promise((resolve, reject) => {
socket.on('init_home', data => {
homeData = data
cached = true
resolve(data)
})
})
}
}

View File

@ -21,7 +21,10 @@ const routes = [
{ {
path: '/', path: '/',
name: 'Home', name: 'Home',
component: TheHomeTab component: TheHomeTab,
meta: {
notKeepAlive: true
}
}, },
{ {
path: '/tracklist/:type/:id', path: '/tracklist/:type/:id',
@ -101,7 +104,7 @@ router.beforeEach((to, from, next) => {
} }
break break
case 'Home': case 'Home':
socket.emit('get_home_data') // socket.emit('get_home_data')
break break
case 'Charts': case 'Charts':
socket.emit('get_charts_data') socket.emit('get_charts_data')

View File

@ -1,7 +1,7 @@
import Vuex from 'vuex' import Vuex from 'vuex'
import Vue from 'vue' import Vue from 'vue'
import home from '@/store/modules/home' // import home from '@/store/modules/home'
import settings from '@/store/modules/settings' import settings from '@/store/modules/settings'
import defaultSettings from '@/store/modules/defaultSettings' import defaultSettings from '@/store/modules/defaultSettings'
import spotifyCredentials from '@/store/modules/spotifyCredentials' import spotifyCredentials from '@/store/modules/spotifyCredentials'
@ -17,7 +17,7 @@ Vue.use(Vuex)
// Create store // Create store
export default new Vuex.Store({ export default new Vuex.Store({
modules: { modules: {
home, // home,
settings, settings,
defaultSettings, defaultSettings,
spotifyCredentials, spotifyCredentials,

View File

@ -22,12 +22,10 @@ const actions = {
commit('SET_FAVORITES_ARTISTS', { index, data: artist }) commit('SET_FAVORITES_ARTISTS', { index, data: artist })
}) })
// dispatch('setFavoritesTracks', payload.tracks) dispatch('setFavoritesTracks', payload.tracks)
}, },
setFavoritesTracks({ commit }, payload) { setFavoritesTracks({ commit }, payload) {
payload.forEach((track, index) => { commit('SET_FAVORITES_TRACKS', payload)
commit('SET_FAVORITES_TRACKS', { index, data: track })
})
} }
} }
@ -50,7 +48,12 @@ const mutations = {
Vue.set(state.playlists, payload.index, payload.data) Vue.set(state.playlists, payload.index, payload.data)
}, },
SET_FAVORITES_TRACKS: (state, payload) => { SET_FAVORITES_TRACKS: (state, payload) => {
Vue.set(state.tracks, payload.index, payload.data) if (typeof payload !== 'string') {
console.error('[deemix] Not setting the favorites tracks because they are not in string format')
return
}
state.tracks = payload
} }
} }

View File

@ -11,6 +11,7 @@ socket.on('init_charts', charts => {
}) })
socket.on('init_favorites', favorites => { socket.on('init_favorites', favorites => {
favorites.tracks = JSON.stringify(favorites.tracks)
store.dispatch('setFavorites', favorites) store.dispatch('setFavorites', favorites)
}) })
@ -20,9 +21,10 @@ socket.on('init_settings', (settings, credentials, defaults) => {
store.dispatch('setCredentials', credentials) store.dispatch('setCredentials', credentials)
}) })
socket.on('init_home', data => { // socket.on('init_home', data => {
store.dispatch('cacheHomeData', data) // console.log('init home', Date.now())
}) // store.dispatch('cacheHomeData', data)
// })
socket.on('init_update', data => { socket.on('init_update', data => {
store.dispatch('setAboutInfo', data) store.dispatch('setAboutInfo', data)