From 8f4a5ee1bd7ff815f460336501011942a3278a2f Mon Sep 17 00:00:00 2001 From: Svallinn <41585298+Svallinn@users.noreply.github.com> Date: Sun, 20 Jun 2021 19:24:35 +0100 Subject: [PATCH] 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. --- src/renderer/store/modules/history.js | 60 +++++++++++++++++++++------ 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/src/renderer/store/modules/history.js b/src/renderer/store/modules/history.js index 5e41ce4ca..96aaa033d 100644 --- a/src/renderer/store/modules/history.js +++ b/src/renderer/store/modules/history.js @@ -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 } }