Skip to content

Commit

Permalink
finally hide all the config IPC inside the config slice
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Dowideit <[email protected]>
  • Loading branch information
SvenDowideit committed May 18, 2022
1 parent 3b3f018 commit c1be631
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
24 changes: 2 additions & 22 deletions src/main/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare type IpcEvent = IpcRendererEvent & IpcMainEvent;
export function initConfigPromises() {
// gets written to .\AppData\Roaming\SolanaWorkbench\electron-cfg.json on windows
promiseIpc.on('CONFIG-GetAll', (event: IpcEvent | undefined) => {
logger.info('main: called CONFIG-GetAll', event);
logger.silly('main: called CONFIG-GetAll', event);
const config = cfg.get('config');
if (!config) {
return {};
Expand All @@ -23,30 +23,10 @@ export function initConfigPromises() {
promiseIpc.on(
'CONFIG-Set',
(key: unknown, val: unknown, event?: IpcEvent | undefined) => {
logger.info(`main: called CONFIG-Set, ${key}, ${val}, ${event}`);
logger.silly(`main: called CONFIG-Set, ${key}, ${val}, ${event}`);
return cfg.set(`config.${key}`, val);
}
);
}

export default {};
// TODO: https://github.com/sindresorhus/electron-store has schema, so am very likely to move to that

/*
PS C:\Users\svend> more '.\AppData\Roaming\SolanaWorkbench\electron-cfg.json'
{
"windowState": {
"main": {
"isMaximized": false,
"isFullScreen": false,
"x": 772,
"y": 177,
"width": 1994,
"height": 1359
}
},
"config": {
"analytics_enabled": "false"
}
}
*/
3 changes: 0 additions & 3 deletions src/main/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ contextBridge.exposeInMainWorld('electron', {
fetchAnchorIDL(msg) {
send('fetch-anchor-idl', msg);
},
config(msg) {
send('config', msg);
},
on(method, func) {
ipcRenderer.on(method, (event, ...args) => func(...args));
},
Expand Down
12 changes: 6 additions & 6 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
electron?: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
promiseIpc?: any;
}
}

Expand Down Expand Up @@ -228,12 +230,10 @@ function AnalyticsBanner() {
type="button"
onClick={() => {
dispatch(
setConfigValue({ key: ConfigKey.AnalyticsEnabled, value: false })
);
window.promiseIpc.send(
'CONFIG-Set',
ConfigKey.AnalyticsEnabled,
analyticsEnabled
setConfigValue({
key: ConfigKey.AnalyticsEnabled,
value: analyticsEnabled,
})
);
}}
>
Expand Down
8 changes: 5 additions & 3 deletions src/renderer/data/Config/configState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export const configSlice = createSlice({
) => {
if (state.values) {
state.values[action.payload.key] = action.payload.value;
window.promiseIpc
.send('CONFIG-Set', action.payload.key, action.payload.value)
.catch(logger.error);
}
},
},
Expand All @@ -65,7 +68,6 @@ export function useConfigState() {
window.promiseIpc
.send('CONFIG-GetAll')
.then((ret: ConfigMap) => {
logger.info(`CONFIG-GetAll => ConfigMap ${JSON.stringify(ret)}`);
dispatch(
setConfig({
values: ret,
Expand All @@ -74,9 +76,9 @@ export function useConfigState() {
);
return `return ${ret}`;
})
.catch((e) => logger.error(e));
.catch((e: Error) => logger.error(e));
}
}, [dispatch, config.loading]);
}, [dispatch, config.loading, config.values]);

return config;
}

0 comments on commit c1be631

Please sign in to comment.