From f075d29aba83a586bc8e143eaa0bea232f7f852c Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Sat, 3 Aug 2024 23:59:19 +0200 Subject: [PATCH] Various performance improvements to the store --- src/renderer/store/modules/profiles.js | 8 ++--- src/renderer/store/modules/settings.js | 44 +++++++------------------- src/renderer/store/modules/utils.js | 2 +- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/renderer/store/modules/profiles.js b/src/renderer/store/modules/profiles.js index 7345c1fa04d4f..70ca26b8aaa18 100644 --- a/src/renderer/store/modules/profiles.js +++ b/src/renderer/store/modules/profiles.js @@ -91,10 +91,10 @@ const actions = { commit('setProfileList', profiles) }, - async batchUpdateSubscriptionDetails({ getters, dispatch }, channels) { + async batchUpdateSubscriptionDetails({ dispatch, state }, channels) { if (channels.length === 0) { return } - const profileList = getters.getProfileList + const profileList = state.profileList for (const profile of profileList) { const currentProfileCopy = deepCopy(profile) @@ -128,9 +128,9 @@ const actions = { } }, - async updateSubscriptionDetails({ getters, dispatch }, { channelThumbnailUrl, channelName, channelId }) { + async updateSubscriptionDetails({ dispatch, state }, { channelThumbnailUrl, channelName, channelId }) { const thumbnail = channelThumbnailUrl?.replace(/=s\d*/, '=s176') ?? null // change thumbnail size if different - const profileList = getters.getProfileList + const profileList = state.profileList for (const profile of profileList) { const currentProfileCopy = deepCopy(profile) const channel = currentProfileCopy.subscriptions.find((channel) => { diff --git a/src/renderer/store/modules/settings.js b/src/renderer/store/modules/settings.js index e080dd225e67e..bf54469f16f11 100644 --- a/src/renderer/store/modules/settings.js +++ b/src/renderer/store/modules/settings.js @@ -385,16 +385,14 @@ const stateWithSideEffects = { await Promise.allSettled(loadPromises) i18n.locale = targetLocale - await dispatch('getRegionData', { - locale: targetLocale - }) + await dispatch('getRegionData', targetLocale) } }, defaultInvidiousInstance: { defaultValue: '', - sideEffectsHandler: ({ commit, getters }, value) => { - if (value !== '' && getters.getCurrentInvidiousInstance !== value) { + sideEffectsHandler: ({ commit, rootState }, value) => { + if (value !== '' && rootState.invidious.currentInvidiousInstance !== value) { commit('setCurrentInvidiousInstance', value) } } @@ -420,6 +418,8 @@ const stateWithSideEffects = { } } +const settingsWithSideEffects = Object.keys(stateWithSideEffects) + const customState = { } @@ -428,28 +428,8 @@ const customGetters = { const customMutations = {} -/**********/ -/* - * DO NOT TOUCH THIS SECTION - * If you wanna add to custom data or logic to the module, - * do so in the aproppriate `custom_` variable - * - * Some of the custom actions below use these properties, so I'll be - * adding them here instead of further down for clarity's sake - */ -Object.assign(customState, { - settingsWithSideEffects: Object.keys(stateWithSideEffects) -}) - -Object.assign(customGetters, { - settingHasSideEffects: (state) => { - return (id) => state.settingsWithSideEffects.includes(id) - } -}) -/**********/ - const customActions = { - grabUserSettings: async ({ commit, dispatch, getters }) => { + grabUserSettings: async ({ commit, dispatch }) => { try { // Assigning default settings for settings that have side effects const userSettings = Object.entries(Object.assign({}, @@ -459,7 +439,7 @@ const customActions = { for (const setting of userSettings) { const [_id, value] = setting - if (getters.settingHasSideEffects(_id)) { + if (settingsWithSideEffects.includes(_id)) { dispatch(defaultSideEffectsTriggerId(_id), value) } @@ -473,14 +453,14 @@ const customActions = { }, // Should be a root action, but we'll tolerate - setupListenersToSyncWindows: ({ commit, dispatch, getters }) => { + setupListenersToSyncWindows: ({ commit, dispatch }) => { if (process.env.IS_ELECTRON) { const { ipcRenderer } = require('electron') ipcRenderer.on(IpcChannels.SYNC_SETTINGS, (_, { event, data }) => { switch (event) { case SyncEvents.GENERAL.UPSERT: - if (getters.settingHasSideEffects(data._id)) { + if (settingsWithSideEffects.includes(data._id)) { dispatch(defaultSideEffectsTriggerId(data._id), data.value) } @@ -615,15 +595,15 @@ for (const settingId of Object.keys(state)) { mutations[mutationId] = (state, value) => { state[settingId] = value } // If setting has side effects, generate action to handle them - if (Object.keys(stateWithSideEffects).includes(settingId)) { + if (settingsWithSideEffects.includes(settingId)) { actions[triggerId] = stateWithSideEffects[settingId].sideEffectsHandler } - actions[updaterId] = async ({ commit, dispatch, getters }, value) => { + actions[updaterId] = async ({ commit, dispatch }, value) => { try { await DBSettingHandlers.upsert(settingId, value) - if (getters.settingHasSideEffects(settingId)) { + if (settingsWithSideEffects.includes(settingId)) { dispatch(triggerId, value) } diff --git a/src/renderer/store/modules/utils.js b/src/renderer/store/modules/utils.js index e3c858d13607c..ce9e710e4863d 100644 --- a/src/renderer/store/modules/utils.js +++ b/src/renderer/store/modules/utils.js @@ -401,7 +401,7 @@ const actions = { commit('setShowProgressBar', value) }, - async getRegionData ({ commit }, { locale }) { + async getRegionData ({ commit }, locale) { const localePathExists = process.env.GEOLOCATION_NAMES.includes(locale) const url = createWebURL(`/static/geolocations/${localePathExists ? locale : 'en-US'}.json`)