Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various performance improvements to the store #5522

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/renderer/store/modules/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down
44 changes: 12 additions & 32 deletions src/renderer/store/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -420,6 +418,8 @@ const stateWithSideEffects = {
}
}

const settingsWithSideEffects = Object.keys(stateWithSideEffects)

const customState = {
}

Expand All @@ -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({},
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/store/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down