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:
parent
e3167782ce
commit
8f4a5ee1bd
|
@ -16,32 +16,55 @@ const actions = {
|
|||
commit('setHistoryCache', results)
|
||||
},
|
||||
|
||||
async updateHistory({ dispatch }, videoData) {
|
||||
async updateHistory({ commit, state }, entry) {
|
||||
await historyDb.update(
|
||||
{ videoId: videoData.videoId },
|
||||
videoData,
|
||||
{ videoId: entry.videoId },
|
||||
entry,
|
||||
{ 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 })
|
||||
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 })
|
||||
dispatch('grabHistory')
|
||||
commit('setHistoryCache', [])
|
||||
},
|
||||
|
||||
async updateWatchProgress({ dispatch }, videoData) {
|
||||
async updateWatchProgress({ commit }, entry) {
|
||||
await historyDb.update(
|
||||
{ videoId: videoData.videoId },
|
||||
{ $set: { watchProgress: videoData.watchProgress } },
|
||||
{ videoId: entry.videoId },
|
||||
{ $set: { watchProgress: entry.watchProgress } },
|
||||
{ upsert: true }
|
||||
)
|
||||
dispatch('grabHistory')
|
||||
|
||||
const entryIndex = state.historyCache.findIndex((currentEntry) => {
|
||||
return entry.videoId === currentEntry.videoId
|
||||
})
|
||||
|
||||
commit('updateEntryWatchProgressInHistoryCache', {
|
||||
index: entryIndex,
|
||||
value: entry.watchProgress
|
||||
})
|
||||
},
|
||||
|
||||
compactHistory(_) {
|
||||
|
@ -52,6 +75,19 @@ const actions = {
|
|||
const mutations = {
|
||||
setHistoryCache(state, 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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue