diff --git a/src/renderer/store/modules/history.js b/src/renderer/store/modules/history.js index 5e41ce4c..96aaa033 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 } }