perf: applied home cache technique to favorites and to settings

This commit is contained in:
Roberto Tonino 2020-09-21 21:54:00 +02:00
parent 53dd45b740
commit 55646c7179
13 changed files with 127 additions and 226 deletions

File diff suppressed because one or more lines are too long

View File

@ -35,7 +35,7 @@
</div> </div>
</div> </div>
<div v-else id="charts_table"> <div v-else id="charts_table">
<button @click="changeCountry">{{ $t('charts.changeCountry') }}</button> <button @click="onChangeCountry">{{ $t('charts.changeCountry') }}</button>
<button @click.stop="addToQueue" :data-link="'https://www.deezer.com/playlist/' + id"> <button @click.stop="addToQueue" :data-link="'https://www.deezer.com/playlist/' + id">
{{ $t('charts.download') }} {{ $t('charts.download') }}
</button> </button>
@ -127,7 +127,6 @@ export default {
async created() { async created() {
socket.on('setChartTracks', this.setTracklist) socket.on('setChartTracks', this.setTracklist)
this.$on('hook:destroyed', () => { this.$on('hook:destroyed', () => {
console.log('destroyed')
socket.off('setChartTracks') socket.off('setChartTracks')
}) })
@ -170,10 +169,9 @@ export default {
socket.emit('getChartTracks', this.id) socket.emit('getChartTracks', this.id)
}, },
setTracklist(data) { setTracklist(data) {
console.log('set tracklist')
this.chart = data this.chart = data
}, },
changeCountry() { onChangeCountry() {
this.country = '' this.country = ''
this.id = 0 this.id = 0
}, },

View File

@ -210,13 +210,15 @@
</style> </style>
<script> <script>
import { mapGetters } from 'vuex'
import { socket } from '@/utils/socket'
import { showView } from '@js/tabs' import { showView } from '@js/tabs'
import Downloads from '@/utils/downloads'
import { socket } from '@/utils/socket'
import { sendAddToQueue } from '@/utils/downloads'
import { convertDuration } from '@/utils/utils' import { convertDuration } from '@/utils/utils'
import { toast } from '@/utils/toasts' import { toast } from '@/utils/toasts'
import { getFavoritesData } from '@/data/favorites'
export default { export default {
data() { data() {
return { return {
@ -229,45 +231,33 @@ export default {
tabs: ['playlist', 'album', 'artist', 'track'] tabs: ['playlist', 'album', 'artist', 'track']
} }
}, },
computed: { async created() {
...mapGetters(['getFavorites']), const favoritesData = await getFavoritesData()
needToWait() {
return Object.keys(this.getFavorites).length === 0 this.setFavorites(favoritesData)
}
}, },
mounted() { mounted() {
// ! Need to implement memorization of the last tab clicked
// ! Use router query
this.waitFavorites()
socket.on('updated_userFavorites', this.updated_userFavorites) socket.on('updated_userFavorites', this.updated_userFavorites)
socket.on('updated_userSpotifyPlaylists', this.updated_userSpotifyPlaylists) socket.on('updated_userSpotifyPlaylists', this.updated_userSpotifyPlaylists)
socket.on('updated_userPlaylists', this.updated_userPlaylists) socket.on('updated_userPlaylists', this.updated_userPlaylists)
socket.on('updated_userAlbums', this.updated_userAlbums) socket.on('updated_userAlbums', this.updated_userAlbums)
socket.on('updated_userArtist', this.updated_userArtist) socket.on('updated_userArtist', this.updated_userArtist)
socket.on('updated_userTracks', this.updated_userTracks) socket.on('updated_userTracks', this.updated_userTracks)
this.$on('hook:destroyed', () => {
socket.off('updated_userFavorites')
socket.off('updated_userSpotifyPlaylists')
socket.off('updated_userPlaylists')
socket.off('updated_userAlbums')
socket.off('updated_userArtist')
socket.off('updated_userTracks')
})
}, },
methods: { methods: {
artistView: showView.bind(null, 'artist'), artistView: showView.bind(null, 'artist'),
albumView: showView.bind(null, 'album'), albumView: showView.bind(null, 'album'),
playlistView: showView.bind(null, 'playlist'), playlistView: showView.bind(null, 'playlist'),
spotifyPlaylistView: showView.bind(null, 'spotifyplaylist'), spotifyPlaylistView: showView.bind(null, 'spotifyplaylist'),
waitFavorites() {
if (this.needToWait) {
// Checking if the saving of the settings is completed
let unsub = this.$store.subscribeAction({
after: (action, state) => {
if (action.type === 'setFavorites') {
this.initFavorites()
unsub()
}
}
})
} else {
this.initFavorites()
}
},
playPausePreview(e) { playPausePreview(e) {
EventBus.$emit('trackPreview:playPausePreview', e) EventBus.$emit('trackPreview:playPausePreview', e)
}, },
@ -279,13 +269,13 @@ export default {
}, },
convertDuration, convertDuration,
addToQueue(e) { addToQueue(e) {
e.stopPropagation() sendAddToQueue(e.currentTarget.dataset.link)
Downloads.sendAddToQueue(e.currentTarget.dataset.link)
}, },
updated_userSpotifyPlaylists(data) { updated_userSpotifyPlaylists(data) {
this.spotifyPlaylists = data this.spotifyPlaylists = data
}, },
updated_userPlaylists(data) { updated_userPlaylists(data) {
console.log(data)
this.playlists = data this.playlists = data
}, },
updated_userAlbums(data) { updated_userAlbums(data) {
@ -307,11 +297,7 @@ export default {
} }
}, },
updated_userFavorites(data) { updated_userFavorites(data) {
const { tracks, albums, artists, playlists } = data this.setFavorites(data)
this.tracks = typeof tracks === 'string' ? JSON.parse(tracks) : tracks
this.albums = albums
this.artists = artists
this.playlists = playlists
// Removing animation class only when the animation has completed an iteration // Removing animation class only when the animation has completed an iteration
// Prevents animation ugly stutter // Prevents animation ugly stutter
@ -324,8 +310,13 @@ export default {
{ once: true } { once: true }
) )
}, },
initFavorites() { setFavorites(data) {
this.updated_userFavorites(this.getFavorites) const { tracks, albums, artists, playlists } = data
this.tracks = tracks
this.albums = albums
this.artists = artists
this.playlists = playlists
} }
} }
} }

View File

@ -658,6 +658,8 @@ import { socket } from '@/utils/socket'
import EventBus from '@/utils/EventBus' import EventBus from '@/utils/EventBus'
import flags from '@/utils/flags' import flags from '@/utils/flags'
import { getSettingsData } from '@/data/settings'
export default { export default {
data() { data() {
return { return {
@ -670,7 +672,7 @@ export default {
lastSettings: {}, lastSettings: {},
spotifyFeatures: {}, spotifyFeatures: {},
lastCredentials: {}, lastCredentials: {},
// defaultSettings: {}, defaultSettings: {},
lastUser: '', lastUser: '',
spotifyUser: '', spotifyUser: '',
slimDownloads: false, slimDownloads: false,
@ -682,9 +684,6 @@ export default {
}, },
computed: { computed: {
...mapGetters({ ...mapGetters({
getSettings: 'getSettings',
getCredentials: 'getCredentials',
getDefaultSettings: 'getDefaultSettings',
arl: 'getARL', arl: 'getARL',
user: 'getUser', user: 'getUser',
isLoggedIn: 'isLoggedIn' isLoggedIn: 'isLoggedIn'
@ -707,11 +706,16 @@ export default {
return `https://e-cdns-images.dzcdn.net/images/user/${this.user.picture}/125x125-000000-80-0-0.jpg` return `https://e-cdns-images.dzcdn.net/images/user/${this.user.picture}/125x125-000000-80-0-0.jpg`
} }
}, },
mounted() { async mounted() {
this.locales = this.$i18n.availableLocales this.locales = this.$i18n.availableLocales
this.revertSettings() const { settingsData, defaultSettingsData, spotifyCredentials } = await getSettingsData()
this.revertCredentials()
this.defaultSettings = defaultSettingsData
this.initSettings(settingsData, spotifyCredentials)
// this.revertSettings()
// this.revertCredentials()
let storedLocale = localStorage.getItem('locale') let storedLocale = localStorage.getItem('locale')
@ -737,6 +741,7 @@ export default {
this.changeSlimDownloads = 'true' === localStorage.getItem('slimDownloads') this.changeSlimDownloads = 'true' === localStorage.getItem('slimDownloads')
let volume = parseInt(localStorage.getItem('previewVolume')) let volume = parseInt(localStorage.getItem('previewVolume'))
if (isNaN(volume)) { if (isNaN(volume)) {
volume = 80 volume = 80
localStorage.setItem('previewVolume', volume) localStorage.setItem('previewVolume', volume)
@ -744,39 +749,28 @@ export default {
window.vol.preview_max_volume = volume window.vol.preview_max_volume = volume
this.waitSettings()
socket.on('updateSettings', this.updateSettings) socket.on('updateSettings', this.updateSettings)
socket.on('accountChanged', this.accountChanged) socket.on('accountChanged', this.accountChanged)
socket.on('familyAccounts', this.initAccounts) socket.on('familyAccounts', this.initAccounts)
socket.on('downloadFolderSelected', this.downloadFolderSelected) socket.on('downloadFolderSelected', this.downloadFolderSelected)
socket.on('applogin_arl', this.setArl) socket.on('applogin_arl', this.setArl)
},
this.$on('hook:destroyed', () => {
socket.off('updateSettings')
socket.off('accountChanged')
socket.off('familyAccounts')
socket.off('downloadFolderSelected')
socket.off('applogin_arl')
})
},
methods: { methods: {
...mapActions({ ...mapActions({
dispatchARL: 'setARL' dispatchARL: 'setARL'
}), }),
waitSettings() {
if (this.needToWait) {
// Checking if the saving of the settings is completed
let unsub = this.$store.subscribeAction({
after: (action, state) => {
if (action.type === 'setSettings') {
this.initSettings()
unsub()
}
}
})
} else {
this.initSettings()
}
},
revertSettings() { revertSettings() {
// this.settings = { ...this.lastSettings }
this.settings = JSON.parse(JSON.stringify(this.lastSettings)) this.settings = JSON.parse(JSON.stringify(this.lastSettings))
}, },
revertCredentials() { revertCredentials() {
// this.spotifyCredentials = { ...this.lastCredentials }
this.spotifyCredentials = JSON.parse(JSON.stringify(this.lastCredentials)) this.spotifyCredentials = JSON.parse(JSON.stringify(this.lastCredentials))
this.spotifyUser = (' ' + this.lastUser).slice(1) this.spotifyUser = (' ' + this.lastUser).slice(1)
}, },
@ -800,13 +794,11 @@ 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.lastCredentials = { ...this.spotifyFeatures }
this.lastSettings = JSON.parse(JSON.stringify(this.settings)) this.lastSettings = JSON.parse(JSON.stringify(this.settings))
this.lastCredentials = JSON.parse(JSON.stringify(this.spotifyFeatures)) 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
this.lastUser = (' ' + this.spotifyUser).slice(1) this.lastUser = (' ' + this.spotifyUser).slice(1)
@ -823,19 +815,13 @@ export default {
console.log(folder) console.log(folder)
this.$set(this.settings, 'downloadLocation', folder) this.$set(this.settings, 'downloadLocation', folder)
}, },
// loadDefaultSettings() { loadSettings(data) {
// this.defaultSettings = { ...this.getDefaultSettings } this.lastSettings = JSON.parse(JSON.stringify(data))
// }, this.settings = JSON.parse(JSON.stringify(data))
loadSettings() { },
// this.lastSettings = { ...this.getSettings } loadCredentials(credentials) {
this.lastSettings = JSON.parse(JSON.stringify(this.getSettings)) this.lastCredentials = JSON.parse(JSON.stringify(credentials))
// this.lastCredentials = { ...this.getCredentials } this.spotifyFeatures = JSON.parse(JSON.stringify(credentials))
this.lastCredentials = JSON.parse(JSON.stringify(this.getCredentials))
// this.settings = { ...this.getSettings }
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()
@ -858,6 +844,7 @@ export default {
this.$refs.username.innerText = user.name this.$refs.username.innerText = user.name
this.$refs.userpicture.src = `https://e-cdns-images.dzcdn.net/images/user/${user.picture}/125x125-000000-80-0-0.jpg` this.$refs.userpicture.src = `https://e-cdns-images.dzcdn.net/images/user/${user.picture}/125x125-000000-80-0-0.jpg`
this.accountNum = accountNum this.accountNum = accountNum
localStorage.setItem('accountNum', this.accountNum) localStorage.setItem('accountNum', this.accountNum)
}, },
initAccounts(accounts) { initAccounts(accounts) {
@ -866,20 +853,21 @@ export default {
logout() { logout() {
socket.emit('logout') socket.emit('logout')
}, },
initSettings() { initSettings(settings, credentials) {
// this.loadDefaultSettings() // this.loadDefaultSettings()
this.loadSettings() this.loadSettings(settings)
this.loadCredentials(credentials)
toast(this.$t('settings.toasts.init'), 'settings') toast(this.$t('settings.toasts.init'), 'settings')
}, },
updateSettings() { updateSettings(newSettings, newCredentials) {
this.loadSettings() this.loadSettings(newSettings)
this.loadCredentials(newCredentials)
toast(this.$t('settings.toasts.update'), 'settings') toast(this.$t('settings.toasts.update'), 'settings')
}, },
resetSettings() { resetSettings() {
// this.settings = { ...this.getDefaultSettings } this.settings = JSON.parse(JSON.stringify(this.defaultSettings))
this.settings = JSON.parse(JSON.stringify(this.getDefaultSettings))
} }
} }
} }

View File

@ -14,6 +14,7 @@ export function getChartsData() {
chartsData = data chartsData = data
cached = true cached = true
socket.off('init_charts')
resolve(data) resolve(data)
}) })
}) })

22
src/data/favorites.js Normal file
View File

@ -0,0 +1,22 @@
import { socket } from '@/utils/socket'
let favoritesData = {}
let cached = false
export function getFavoritesData() {
if (cached) {
return favoritesData
} else {
socket.emit('get_favorites_data')
return new Promise((resolve, reject) => {
socket.on('init_favorites', data => {
favoritesData = data
cached = true
socket.off('init_favorites')
resolve(data)
})
})
}
}

View File

@ -14,6 +14,7 @@ export function getHomeData() {
homeData = data homeData = data
cached = true cached = true
socket.off('init_home')
resolve(data) resolve(data)
}) })
}) })

27
src/data/settings.js Normal file
View File

@ -0,0 +1,27 @@
import { socket } from '@/utils/socket'
let settingsData = {}
let defaultSettingsData = {}
let spotifyCredentials = {}
let cached = false
export function getSettingsData() {
if (cached) {
return { settingsData, defaultSettingsData, spotifyCredentials }
} else {
socket.emit('get_settings_data')
return new Promise((resolve, reject) => {
socket.on('init_settings', (settings, credentials, defaults) => {
settingsData = settings
defaultSettingsData = defaults
spotifyCredentials = credentials
// cached = true
socket.off('init_settings')
resolve({ settingsData, defaultSettingsData, spotifyCredentials })
})
})
}
}

View File

@ -47,7 +47,10 @@ const routes = [
{ {
path: '/favorites', path: '/favorites',
name: 'Favorites', name: 'Favorites',
component: TheFavoritesTab component: TheFavoritesTab,
meta: {
notKeepAlive: true
}
}, },
{ {
path: '/errors', path: '/errors',
@ -106,12 +109,6 @@ router.beforeEach((to, from, next) => {
id: to.params.id id: to.params.id
} }
break break
case 'Home':
// socket.emit('get_home_data')
break
case 'Charts':
// socket.emit('get_charts_data')
break
default: default:
break break

View File

@ -1,10 +1,6 @@
import Vuex from 'vuex' import Vuex from 'vuex'
import Vue from 'vue' import Vue from 'vue'
import settings from '@/store/modules/settings'
import defaultSettings from '@/store/modules/defaultSettings'
import spotifyCredentials from '@/store/modules/spotifyCredentials'
import favorites from '@/store/modules/favorites'
import about from '@/store/modules/about' import about from '@/store/modules/about'
import login from '@/store/modules/login' import login from '@/store/modules/login'
import errors from '@/store/modules/errors' import errors from '@/store/modules/errors'
@ -15,10 +11,6 @@ Vue.use(Vuex)
// Create store // Create store
export default new Vuex.Store({ export default new Vuex.Store({
modules: { modules: {
settings,
defaultSettings,
spotifyCredentials,
favorites,
about, about,
login, login,
errors errors

View File

@ -1,65 +0,0 @@
import Vue from 'vue'
const state = {
albums: [],
artists: [],
playlists: [],
tracks: [],
test: ''
}
const actions = {
setFavorites({ commit, dispatch }, payload) {
payload.playlists.forEach((playlist, index) => {
commit('SET_FAVORITES_PLAYLISTS', { index, data: playlist })
})
payload.albums.forEach((album, index) => {
commit('SET_FAVORITES_ALBUMS', { index, data: album })
})
payload.artists.forEach((artist, index) => {
commit('SET_FAVORITES_ARTISTS', { index, data: artist })
})
dispatch('setFavoritesTracks', payload.tracks)
},
setFavoritesTracks({ commit }, payload) {
commit('SET_FAVORITES_TRACKS', payload)
}
}
const getters = {
getFavorites: state => state,
getFavoritesAlbums: state => state.albums,
getFavoritesArtists: state => state.artists,
getFavoritesPlaylists: state => state.playlists,
getFavoritesTracks: state => state.tracks
}
const mutations = {
SET_FAVORITES_ALBUMS: (state, payload) => {
Vue.set(state.albums, payload.index, payload.data)
},
SET_FAVORITES_ARTISTS: (state, payload) => {
Vue.set(state.artists, payload.index, payload.data)
},
SET_FAVORITES_PLAYLISTS: (state, payload) => {
Vue.set(state.playlists, payload.index, payload.data)
},
SET_FAVORITES_TRACKS: (state, payload) => {
if (typeof payload !== 'string') {
console.error('[deemix] Not setting the favorites tracks because they are not in string format')
return
}
state.tracks = payload
}
}
export default {
state,
actions,
getters,
mutations
}

View File

@ -1,31 +0,0 @@
import Vue from 'vue'
const state = {}
const actions = {
setSettings({ commit }, payload) {
for (const settingName in payload) {
if (!payload.hasOwnProperty(settingName)) return
const settingValue = payload[settingName]
commit('SET_UNKNOWN_SETTING', { settingName, settingValue })
}
}
}
const getters = {
getSettings: state => state
}
const mutations = {
SET_UNKNOWN_SETTING(state, payload) {
Vue.set(state, payload.settingName, payload.settingValue)
}
}
export default {
state,
actions,
getters,
mutations
}

View File

@ -6,26 +6,6 @@ socket.on('connect', () => {
document.getElementById('start_app_placeholder').classList.add('loading_placeholder--hidden') document.getElementById('start_app_placeholder').classList.add('loading_placeholder--hidden')
}) })
// socket.on('init_charts', charts => {
// store.dispatch('cacheCharts', charts)
// })
socket.on('init_favorites', favorites => {
favorites.tracks = JSON.stringify(favorites.tracks)
store.dispatch('setFavorites', favorites)
})
socket.on('init_settings', (settings, credentials, defaults) => {
store.dispatch('setSettings', settings)
store.dispatch('setDefaultSettings', defaults)
store.dispatch('setCredentials', credentials)
})
// socket.on('init_home', 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)
}) })