Skip to content

Commit

Permalink
debounce save settings to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
aarron-lee committed Nov 28, 2023
1 parent e62f3e2 commit 48cf2cd
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/redux-modules/rgbSlice.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';
import { get, merge } from 'lodash';
import { debounce, get, merge } from 'lodash';
import type { RootState } from './store';
import { setCurrentGameId, setInitialState } from './extraActions';
import { extractCurrentGameId, getServerApi } from '../backend/utils';
Expand Down Expand Up @@ -283,6 +283,25 @@ const mutatingActionTypes = [
rgbSlice.actions.setHue.type
];

// persist RGB settings to the backend
const saveRgbSettings = (store: any) => {
const serverApi = getServerApi();

const {
rgb: { rgbProfiles, perGameProfilesEnabled }
} = store.getState();
const currentGameId = perGameProfilesEnabled
? extractCurrentGameId()
: 'default';

serverApi?.callPluginMethod('save_rgb_settings', {
rgbProfiles,
currentGameId
});
};

const debouncedSaveRgbSettings = debounce(saveRgbSettings, 100);

export const saveRgbSettingsMiddleware =
(store: any) => (next: any) => (action: any) => {
const { type } = action;
Expand All @@ -291,18 +310,8 @@ export const saveRgbSettingsMiddleware =
const result = next(action);

if (mutatingActionTypes.includes(type)) {
// get latest state from store
const {
rgb: { rgbProfiles, perGameProfilesEnabled }
} = store.getState();
const currentGameId = perGameProfilesEnabled
? extractCurrentGameId()
: 'default';

serverApi?.callPluginMethod('save_rgb_settings', {
rgbProfiles,
currentGameId
});
// save to backend
debouncedSaveRgbSettings(store);
}
if (type === setInitialState.type || type === setCurrentGameId.type) {
// tell backend to sync LEDs to current FE state
Expand Down

0 comments on commit 48cf2cd

Please sign in to comment.