@@ -2,7 +2,7 @@ import { join } from 'path';
22
33import type { PayloadAction } from '@reduxjs/toolkit' ;
44import { createAsyncThunk , createSlice } from '@reduxjs/toolkit' ;
5- import { merge } from 'lodash-es' ;
5+ import { mergeWith } from 'lodash-es' ;
66
77import type { RootState } from '@app/store/index.js' ;
88import type { StateFileToIniParserMap } from '@app/store/types.js' ;
@@ -164,6 +164,15 @@ export const loadStateFiles = createAsyncThunk<
164164 return state ;
165165} ) ;
166166
167+ // Custom merge function that replaces arrays instead of merging them element-wise
168+ const mergeReplaceArrays = ( object : any , source : any , ...rest : any [ ] ) =>
169+ mergeWith ( object , source , ...rest , ( objValue : unknown , srcValue : unknown ) => {
170+ if ( Array . isArray ( srcValue ) ) {
171+ return srcValue ;
172+ }
173+ return undefined ;
174+ } ) ;
175+
167176export const emhttp = createSlice ( {
168177 name : 'emhttp' ,
169178 initialState,
@@ -176,7 +185,7 @@ export const emhttp = createSlice({
176185 } >
177186 ) {
178187 const { field } = action . payload ;
179- return merge ( state , { [ field ] : action . payload . state } ) ;
188+ return mergeReplaceArrays ( state , { [ field ] : action . payload . state } ) ;
180189 } ,
181190 } ,
182191 extraReducers ( builder ) {
@@ -185,18 +194,16 @@ export const emhttp = createSlice({
185194 } ) ;
186195
187196 builder . addCase ( loadStateFiles . fulfilled , ( state , action ) => {
188- merge ( state , action . payload , { status : FileLoadStatus . LOADED } ) ;
197+ mergeReplaceArrays ( state , action . payload , { status : FileLoadStatus . LOADED } ) ;
189198 } ) ;
190199
191200 builder . addCase ( loadStateFiles . rejected , ( state , action ) => {
192- merge ( state , action . payload , { status : FileLoadStatus . FAILED_LOADING } ) ;
201+ mergeReplaceArrays ( state , action . payload , { status : FileLoadStatus . FAILED_LOADING } ) ;
193202 } ) ;
194203
195204 builder . addCase ( loadSingleStateFile . fulfilled , ( state , action ) => {
196205 if ( action . payload ) {
197- // const changedKey = Object.keys(action.payload)[0]
198- // emhttpLogger.debug('Key', changedKey, 'Difference in changes', getDiff(action.payload, { [changedKey]: state[changedKey] } ))
199- merge ( state , action . payload ) ;
206+ mergeReplaceArrays ( state , action . payload ) ;
200207 } else {
201208 emhttpLogger . warn ( 'Invalid payload returned from loadSingleStateFile()' ) ;
202209 }
0 commit comments