Skip to content

Commit ae63eeb

Browse files
committed
Use new backend for config update
1 parent f0fe3ca commit ae63eeb

File tree

10 files changed

+42
-20
lines changed

10 files changed

+42
-20
lines changed

src/ts/backend/config/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import * as fs from "fs";
77
const CONFIG_FILE_NAME = "config.json";
88
let config: Config | undefined = undefined;
99

10-
const getConfigFolder = async () => electron.ipcRenderer.invoke('config-file-name');
10+
const getConfigFolder = async () => electron.app.getPath('userData');
1111
const getConfigFile = async () => path.join(await getConfigFolder(), CONFIG_FILE_NAME);
1212

1313
export async function loadConfig() {
1414
const fileName = await getConfigFile();
1515
try {
16+
console.log(`Reading configuration file: ${fileName}`);
1617
const contents = await fs.promises.readFile(fileName).then(buf => buf.toString());
1718
config = JSON.parse(contents);
1819
} catch {

src/ts/backend/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {cpuInfo, cpuSpeedUpdate, cpuTemperatureUpdate, cpuUsageUpdate} from "./c
33
import {memoryInfo, memoryUsageUpdate} from "./memory";
44
import {networkAdapter, networkTransferUpdate, pingUpdate, webUpdate} from "./network";
55
import {partitionInfo} from "./disk";
6-
import {getConfig} from "./config";
6+
import {getConfig, updateConfig} from "./config";
77
import {getAllProcessInfo} from "./process/process";
88

99
export default class NodeBackend implements Backend {
@@ -26,4 +26,5 @@ export default class NodeBackend implements Backend {
2626
processInfo = getAllProcessInfo;
2727

2828
getConfig = async () => getConfig();
29+
updateConfig = updateConfig;
2930
}

src/ts/backend/server/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import {Backend} from "../../data/backend";
22
import ROUTES from "../../data/routes";
33

44
export default function registerBackend(
5-
backend: Backend, addRoute: (route: string, result: () => Promise<unknown>) => unknown
5+
backend: Backend,
6+
addRoute: (route: string, result: () => Promise<unknown>) => unknown,
7+
addInputRoute: (route: string, result: (input: unknown) => Promise<unknown>) => unknown,
68
) {
79

810
addRoute(ROUTES.CONFIG, backend.getConfig);
11+
addInputRoute(ROUTES.UPDATE_CONFIG, backend.updateConfig);
912

1013
addRoute(ROUTES.CPU_SPEED, backend.cpuSpeedUpdate);
1114
addRoute(ROUTES.CPU_TEMPERATURE, backend.cpuTemperatureUpdate);

src/ts/backend/server/ipc.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ export default function registerIpc(backend: Backend, ipcMain: IpcMain) {
88
return JSON.stringify(await f());
99
});
1010
}
11-
registerBackend(backend, registerRoute);
11+
const registerInputRoute = (route: string, f: (input: unknown) => unknown) => {
12+
ipcMain.handle(route, async (_, input) => {
13+
return JSON.stringify(await f(JSON.parse(input)));
14+
});
15+
}
16+
registerBackend(backend, registerRoute, registerInputRoute);
1217
}

src/ts/data/backend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ export interface Backend {
2525
partitionInfo(): Promise<PartitionInfo[]>;
2626

2727
getConfig(): Promise<Config>;
28+
updateConfig(config: Config): Promise<void>;
2829

2930
}

src/ts/data/routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const ROUTES = {
22
CONFIG: "/config",
3+
UPDATE_CONFIG: "/config/update",
34
CPU_SPEED: "/cpu/speed",
45
CPU_TEMPERATURE: "/cpu/temperature",
56
CPU_USAGE: "/cpu/usage",

src/ts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import index from "../html/index.html";
44
import registerIpc from "./backend/server/ipc";
55
import NodeBackend from "./backend";
66
import startRender from "./renderer";
7+
import {loadConfig} from "./backend/config";
78

89
async function createWindow(): Promise<void> {
910
const mainWindow = new BrowserWindow({
@@ -27,6 +28,7 @@ if (process && process.type === 'renderer') {
2728
startRender().then();
2829
} else {
2930
app.on("ready", async () => {
31+
await loadConfig();
3032
registerIpc(new NodeBackend(), ipcMain);
3133
await createWindow();
3234
});

src/ts/ui/config/network/interface.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22
import {ChangeEvent, ReactNode} from "react";
33
import {getAllInterfaces, getDefaultInterface} from "../../../backend/network";
4-
import {getConfig, updateConfig} from "../../../backend/config";
4+
import {BackendContext} from "../../backendContext";
55

66
interface PropType {
77
showLabel?: boolean;
@@ -14,6 +14,9 @@ interface StateType {
1414

1515
export default class NetworkInterfaceSettingDropdown extends React.Component<PropType, StateType> {
1616

17+
static contextType = BackendContext;
18+
context!: React.ContextType<typeof BackendContext>;
19+
1720
constructor(props: Readonly<PropType>) {
1821
super(props);
1922
this.state = {
@@ -45,10 +48,10 @@ export default class NetworkInterfaceSettingDropdown extends React.Component<Pro
4548
private onChange = (event: ChangeEvent<HTMLSelectElement>) => {
4649
const result = event?.target?.value;
4750
if (result) {
48-
const config = getConfig();
51+
const config = window.config;
4952
config.network.interface = result;
50-
updateConfig(config)
51-
.catch((err) => console.error("Unable to save config!", err));
53+
this.context.updateConfig(config)
54+
.catch((err: any) => console.error("Unable to save config!", err));
5255
}
5356
}
5457

src/ts/ui/serverBackend.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {Backend} from "../data/backend";
22
import ROUTES from "../data/routes";
33
import {ipcRenderer} from "electron";
4+
import {Config} from "../backend/config/interface";
45

56
export class ServerBackend implements Backend {
67

7-
protected connection: (route: string) => Promise<string>;
8+
protected connection: (route: string, input?: string) => Promise<string>;
89

9-
constructor(connection: (route: string) => Promise<string>) {
10+
constructor(connection: (route: string, input?: string) => Promise<string>) {
1011
this.connection = connection;
1112
}
1213

@@ -27,12 +28,16 @@ export class ServerBackend implements Backend {
2728

2829
partitionInfo = async () => JSON.parse(await this.connection(ROUTES.DISK_PARTITION_INFO));
2930
getConfig = async () => JSON.parse(await this.connection(ROUTES.CONFIG));
31+
updateConfig = async (config: Config) => {
32+
await this.connection(ROUTES.UPDATE_CONFIG, JSON.stringify(config));
33+
return;
34+
}
3035

3136
}
3237

3338
export class IpcBackend extends ServerBackend implements Backend {
3439
constructor() {
35-
const getResult = (route: string) => ipcRenderer.invoke(route);
40+
const getResult = (route: string, input?: string) => ipcRenderer.invoke(route, input);
3641
super(getResult);
3742
}
3843
}

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,16 +529,16 @@
529529
"@types/node" "*"
530530

531531
"@types/node@*":
532-
version "22.10.2"
533-
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.10.2.tgz#a485426e6d1fdafc7b0d4c7b24e2c78182ddabb9"
534-
integrity sha512-Xxr6BBRCAOQixvonOye19wnzyDiUtTeqldOOmj3CkeblonbccA12PFwlufvRdrpjXxqnmUaeiU5EOA+7s5diUQ==
532+
version "22.10.3"
533+
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.10.3.tgz#cdc2a89bf6e5d5e593fad08e83f74d7348d5dd10"
534+
integrity sha512-DifAyw4BkrufCILvD3ucnuN8eydUfc/C1GlyrnI+LK6543w5/L3VeVgf05o3B4fqSXP1dKYLOZsKfutpxPzZrw==
535535
dependencies:
536536
undici-types "~6.20.0"
537537

538538
"@types/node@^18.11.18":
539-
version "18.19.68"
540-
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.68.tgz#f4f10d9927a7eaf3568c46a6d739cc0967ccb701"
541-
integrity sha512-QGtpFH1vB99ZmTa63K4/FU8twThj4fuVSBkGddTp7uIL/cuoLWIUSL2RcOaigBhfR+hg5pgGkBnkoOxrTVBMKw==
539+
version "18.19.69"
540+
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.69.tgz#748d301818ba4b238854c53d290257a70aae7d01"
541+
integrity sha512-ECPdY1nlaiO/Y6GUnwgtAAhLNaQ53AyIVz+eILxpEo5OvuqE6yWkqWBIb5dU0DqhKQtMeny+FBD3PK6lm7L5xQ==
542542
dependencies:
543543
undici-types "~5.26.4"
544544

@@ -4719,9 +4719,9 @@ symbol-observable@^1.1.0:
47194719
integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==
47204720

47214721
systeminformation@^5:
4722-
version "5.23.25"
4723-
resolved "https://registry.yarnpkg.com/systeminformation/-/systeminformation-5.23.25.tgz#ca1f0a19c949e40e9909a8f2e94da6246600d27d"
4724-
integrity sha512-sX/q1AL8gjn+VizKPieuHAiwMyPmM4/oj0mW434sEzwv3m0vghF4aO/VHUvmbMdIa3o5MVFgY9JtvX6M2dL36g==
4722+
version "5.24.3"
4723+
resolved "https://registry.yarnpkg.com/systeminformation/-/systeminformation-5.24.3.tgz#f31ad0253d4595955c7f1bbee738a754c6a19d9b"
4724+
integrity sha512-f7uvLgsQIj9/gNcbNZWgw27sNJO5tQvi2tCo4/eVlynxS5slaa7EAN2vSD5jh0YljyjE8qQZ3qtAr7EkDCRyMw==
47254725

47264726
tapable@^2.1.1, tapable@^2.2.0, tapable@^2.2.1:
47274727
version "2.2.1"

0 commit comments

Comments
 (0)