Skip to content

Commit c38c974

Browse files
committed
mergeWith
1 parent ed643b6 commit c38c974

File tree

1 file changed

+12
-45
lines changed

1 file changed

+12
-45
lines changed

packages/redux/src/selectors.ts

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { UndoPartial } from '@deephaven/utils';
22
import { memoize } from 'proxy-memoize';
3-
import type { RootState, WorkspaceSettings } from './store';
3+
import type { RootState } from './store';
4+
import _ from 'lodash';
45

56
const EMPTY_OBJECT = Object.freeze({});
67

@@ -55,58 +56,24 @@ export const getWorkspace = <State extends RootState>(
5556
return workspace;
5657
};
5758

58-
/**
59-
* Helpter function to replace settings values
60-
* Any nested objects under workspace settings need to be handled here
61-
* @param key the key of the setting to replace
62-
* @param settings current settings object
63-
* @param customizedSettings customized settings to apply
64-
*/
65-
const replaceSettings = (
66-
key: keyof WorkspaceSettings,
67-
settings: WorkspaceSettings,
68-
customizedSettings: UndoPartial<WorkspaceSettings>
69-
): void => {
70-
let newSettings = customizedSettings[key];
71-
if (key === 'notebookSettings') {
72-
const customizedLinter =
73-
customizedSettings.notebookSettings?.python?.linter;
74-
const settingsLinter = settings.notebookSettings?.python?.linter;
75-
newSettings = {
76-
...settings.notebookSettings,
77-
...customizedSettings.notebookSettings,
78-
python: {
79-
linter: {
80-
isEnabled: customizedLinter?.isEnabled ?? settingsLinter?.isEnabled,
81-
config: customizedLinter?.config ?? settingsLinter?.config,
82-
},
83-
},
84-
};
85-
}
86-
// @ts-expect-error assign non-undefined customized settings to settings
87-
settings[key] = newSettings;
88-
};
89-
9059
// Settings
9160
export const getSettings = memoize(
9261
<State extends RootState>(
9362
store: State
9463
): UndoPartial<State['workspace']['data']['settings']> => {
9564
const customizedSettings = getWorkspace(store)?.data.settings ?? {};
9665

97-
const settings = { ...getDefaultWorkspaceSettings(store) };
98-
const keys = Object.keys(customizedSettings) as (keyof WorkspaceSettings)[];
99-
for (let i = 0; i < keys.length; i += 1) {
100-
const key = keys[i];
101-
if (customizedSettings[key] !== undefined) {
102-
replaceSettings(
103-
key,
104-
settings,
105-
customizedSettings as UndoPartial<WorkspaceSettings>
106-
);
66+
return _.mergeWith(
67+
{},
68+
getDefaultWorkspaceSettings(store),
69+
customizedSettings,
70+
(objValue, srcValue) => {
71+
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
72+
return srcValue;
73+
}
74+
return undefined;
10775
}
108-
}
109-
return settings as UndoPartial<State['workspace']['data']['settings']>;
76+
) as UndoPartial<State['workspace']['data']['settings']>;
11077
}
11178
);
11279

0 commit comments

Comments
 (0)