Store/History: Prevent actions from unnecessarily using the filesystem

Most actions in the history module would grab the history from disk
every time the history was updated and updated the cache this way.

This commit makes these actions perform these modifications in memory
instead of unnecessarily reaching into the disk.
This commit is contained in:
Svallinn 2021-06-20 19:24:35 +01:00
parent e3167782ce
commit 8f4a5ee1bd
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
1 changed files with 48 additions and 12 deletions

View File

@ -16,32 +16,55 @@ const actions = {
commit('setHistoryCache', results) commit('setHistoryCache', results)
}, },
async updateHistory({ dispatch }, videoData) { async updateHistory({ commit, state }, entry) {
await historyDb.update( await historyDb.update(
{ videoId: videoData.videoId }, { videoId: entry.videoId },
videoData, entry,
{ upsert: true } { upsert: true }
) )
dispatch('grabHistory')
const entryIndex = state.historyCache.findIndex((currentEntry) => {
return entry.videoId === currentEntry.videoId
})
entryIndex === -1
? commit('insertNewEntryToHistoryCache', entry)
: commit('hoistEntryToTopOfHistoryCache', {
currentIndex: entryIndex,
updatedEntry: entry
})
}, },
async removeFromHistory({ dispatch }, videoId) { async removeFromHistory({ commit }, videoId) {
await historyDb.remove({ videoId: videoId }) await historyDb.remove({ videoId: videoId })
dispatch('grabHistory')
const updatedCache = state.historyCache.filter((entry) => {
return entry.videoId !== videoId
})
commit('setHistoryCache', updatedCache)
}, },
async removeAllHistory({ dispatch }) { async removeAllHistory({ commit }) {
await historyDb.remove({}, { multi: true }) await historyDb.remove({}, { multi: true })
dispatch('grabHistory') commit('setHistoryCache', [])
}, },
async updateWatchProgress({ dispatch }, videoData) { async updateWatchProgress({ commit }, entry) {
await historyDb.update( await historyDb.update(
{ videoId: videoData.videoId }, { videoId: entry.videoId },
{ $set: { watchProgress: videoData.watchProgress } }, { $set: { watchProgress: entry.watchProgress } },
{ upsert: true } { upsert: true }
) )
dispatch('grabHistory')
const entryIndex = state.historyCache.findIndex((currentEntry) => {
return entry.videoId === currentEntry.videoId
})
commit('updateEntryWatchProgressInHistoryCache', {
index: entryIndex,
value: entry.watchProgress
})
}, },
compactHistory(_) { compactHistory(_) {
@ -52,6 +75,19 @@ const actions = {
const mutations = { const mutations = {
setHistoryCache(state, historyCache) { setHistoryCache(state, historyCache) {
state.historyCache = historyCache state.historyCache = historyCache
},
insertNewEntryToHistoryCache(state, entry) {
state.historyCache.unshift(entry)
},
hoistEntryToTopOfHistoryCache(state, { currentIndex, updatedEntry }) {
state.historyCache.splice(currentIndex, 1)
state.historyCache.unshift(updatedEntry)
},
updateEntryWatchProgressInHistoryCache(state, { index, value }) {
state.historyCache[index].watchProgress = value
} }
} }