Skip to content

Commit d34b67a

Browse files
committed
extracted ipc handler registration to another module
1 parent c2238dc commit d34b67a

File tree

3 files changed

+120
-99
lines changed

3 files changed

+120
-99
lines changed

.eslintrc.cjs

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ module.exports = {
1414
"no-unused-expressions": "warn",
1515
"@typescript-eslint/explicit-function-return-type": "off",
1616
"prettier/prettier": 0,
17-
"react/prop-types": "off"
17+
"react/prop-types": "off",
18+
"@typescript-eslint/no-unused-vars": [
19+
"warn",
20+
{
21+
argsIgnorePattern: "^_",
22+
varsIgnorePattern: "^_",
23+
caughtErrorsIgnorePattern: "^_"
24+
}
25+
]
1826
}
1927
};

src/main/index.ts

+5-98
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { electronApp, is, optimizer } from "@electron-toolkit/utils";
2-
import { CardData, PersonaFormData } from "@shared/types";
3-
import { BrowserWindow, Menu, Tray, app, dialog, ipcMain, nativeImage, net, protocol, shell } from "electron";
2+
import { BrowserWindow, Menu, Tray, app, dialog, nativeImage, net, protocol, shell } from "electron";
43
import { autoUpdater } from "electron-updater";
54
import path, { join } from "path";
65
import icon from "../../resources/icon.png?asset";
6+
import ipc from "./lib/ipc";
77
import blob from "./lib/store/blob";
88
import secret from "./lib/store/secret";
99
import setting from "./lib/store/setting";
1010
import sqlite from "./lib/store/sqlite";
1111
import { cardsRootPath, personasRootPath } from "./lib/utils";
12-
import { XFetchConfig, xfetch } from "./lib/xfetch";
1312

14-
let window: any;
1513
// let loadingWindow: any;
14+
let window: any;
1615
let isQuiting = false;
1716

17+
// Prevent multiple app instances
1818
const gotTheLock = app.requestSingleInstanceLock();
19-
2019
if (!gotTheLock) {
2120
app.quit();
2221
}
@@ -156,99 +155,7 @@ app.whenReady().then(async () => {
156155
await blob.init();
157156
await secret.init();
158157
await setting.init();
159-
160-
ipcMain.handle("sqlite.run", async (_, query: string, params: [] = []) => {
161-
return sqlite.run(query, params);
162-
});
163-
ipcMain.handle("sqlite.all", async (_, query: string, params: [] = []) => {
164-
return sqlite.all(query, params);
165-
});
166-
ipcMain.handle("sqlite.get", async (_, query: string, params: [] = []) => {
167-
return sqlite.get(query, params);
168-
});
169-
ipcMain.handle("sqlite.runAsTransaction", async (_, queries: string[], params: [][]) => {
170-
return sqlite.runAsTransaction(queries, params);
171-
});
172-
173-
ipcMain.handle("blob.image.get", async (_, path: string) => {
174-
return await blob.image.get(path);
175-
});
176-
177-
ipcMain.handle("blob.cards.get", async (_, card: string) => {
178-
return await blob.cards.get(card);
179-
});
180-
181-
ipcMain.handle(
182-
"blob.cards.create",
183-
async (_, cardData: CardData, bannerURI: string | null, avatarURI: string | null) => {
184-
return await blob.cards.create(cardData, bannerURI, avatarURI);
185-
}
186-
);
187-
188-
ipcMain.handle(
189-
"blob.cards.update",
190-
async (_, cardID: number, cardData: CardData, bannerURI: string | null, avatarURI: string | null) => {
191-
return await blob.cards.update(cardID, cardData, bannerURI, avatarURI);
192-
}
193-
);
194-
195-
ipcMain.handle("blob.cards.del", async (_, cardID: number) => {
196-
return await blob.cards.del(cardID);
197-
});
198-
199-
ipcMain.handle("blob.cards.exportToZip", async (_, card: string) => {
200-
return await blob.cards.exportToZip(card);
201-
});
202-
203-
ipcMain.handle("blob.cards.importFromZip", async (_, zip: string) => {
204-
return await blob.cards.importFromZip(zip);
205-
});
206-
207-
ipcMain.handle("blob.personas.get", async (_, persona: string) => {
208-
return await blob.personas.get(persona);
209-
});
210-
ipcMain.handle("blob.personas.post", async (_, data: PersonaFormData) => {
211-
return await blob.personas.post(data);
212-
});
213-
214-
ipcMain.handle("blob.personas.put", async (_, id: number, data: PersonaFormData) => {
215-
return await blob.personas.put(id, data);
216-
});
217-
218-
ipcMain.handle("secret.get", async (_, k: string) => {
219-
return await secret.get(k);
220-
});
221-
222-
ipcMain.handle("secret.set", async (_, k: string, v: string) => {
223-
return await secret.set(k, v);
224-
});
225-
226-
ipcMain.handle(
227-
"xfetch.post",
228-
async (_, url: string, body?: object, headers?: Record<string, string>, config?: XFetchConfig) => {
229-
return await xfetch.post(url, body, headers, config);
230-
}
231-
);
232-
233-
ipcMain.handle("xfetch.get", async (_, url: string, headers?: Record<string, string>, config?: XFetchConfig) => {
234-
return await xfetch.get(url, headers, config);
235-
});
236-
237-
ipcMain.handle("xfetch.abort", async (_, uuid: string) => {
238-
return await xfetch.abort(uuid);
239-
});
240-
241-
ipcMain.handle("utils.openURL", async (_, url: string) => {
242-
return await shell.openExternal(url);
243-
});
244-
245-
ipcMain.handle("setting.get", async () => {
246-
return await setting.get();
247-
});
248-
249-
ipcMain.handle("setting.set", async (_, settings: any) => {
250-
return await setting.set(settings);
251-
});
158+
await ipc.init();
252159

253160
// Open or close DevTools using F12 in development
254161
// Ignore Cmd/Ctrl + R in production.

src/main/lib/ipc.ts

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { CardData, PersonaFormData } from "@shared/types";
2+
import { ipcMain, shell } from "electron";
3+
import blob from "./store/blob";
4+
import secret from "./store/secret";
5+
import setting from "./store/setting";
6+
import sqlite from "./store/sqlite";
7+
import { XFetchConfig, xfetch } from "./xfetch";
8+
9+
async function init() {
10+
ipcMain.handle("sqlite.run", async (_, query: string, params: [] = []) => {
11+
return sqlite.run(query, params);
12+
});
13+
ipcMain.handle("sqlite.all", async (_, query: string, params: [] = []) => {
14+
return sqlite.all(query, params);
15+
});
16+
ipcMain.handle("sqlite.get", async (_, query: string, params: [] = []) => {
17+
return sqlite.get(query, params);
18+
});
19+
ipcMain.handle("sqlite.runAsTransaction", async (_, queries: string[], params: [][]) => {
20+
return sqlite.runAsTransaction(queries, params);
21+
});
22+
23+
ipcMain.handle("blob.image.get", async (_, path: string) => {
24+
return await blob.image.get(path);
25+
});
26+
27+
ipcMain.handle("blob.cards.get", async (_, card: string) => {
28+
return await blob.cards.get(card);
29+
});
30+
31+
ipcMain.handle(
32+
"blob.cards.create",
33+
async (_, cardData: CardData, bannerURI: string | null, avatarURI: string | null) => {
34+
return await blob.cards.create(cardData, bannerURI, avatarURI);
35+
}
36+
);
37+
38+
ipcMain.handle(
39+
"blob.cards.update",
40+
async (_, cardID: number, cardData: CardData, bannerURI: string | null, avatarURI: string | null) => {
41+
return await blob.cards.update(cardID, cardData, bannerURI, avatarURI);
42+
}
43+
);
44+
45+
ipcMain.handle("blob.cards.del", async (_, cardID: number) => {
46+
return await blob.cards.del(cardID);
47+
});
48+
49+
ipcMain.handle("blob.cards.exportToZip", async (_, card: string) => {
50+
return await blob.cards.exportToZip(card);
51+
});
52+
53+
ipcMain.handle("blob.cards.importFromZip", async (_, zip: string) => {
54+
return await blob.cards.importFromZip(zip);
55+
});
56+
57+
ipcMain.handle("blob.personas.get", async (_, persona: string) => {
58+
return await blob.personas.get(persona);
59+
});
60+
ipcMain.handle("blob.personas.post", async (_, data: PersonaFormData) => {
61+
return await blob.personas.post(data);
62+
});
63+
64+
ipcMain.handle("blob.personas.put", async (_, id: number, data: PersonaFormData) => {
65+
return await blob.personas.put(id, data);
66+
});
67+
68+
ipcMain.handle("secret.get", async (_, k: string) => {
69+
return await secret.get(k);
70+
});
71+
72+
ipcMain.handle("secret.set", async (_, k: string, v: string) => {
73+
return await secret.set(k, v);
74+
});
75+
76+
ipcMain.handle(
77+
"xfetch.post",
78+
async (_, url: string, body?: object, headers?: Record<string, string>, config?: XFetchConfig) => {
79+
return await xfetch.post(url, body, headers, config);
80+
}
81+
);
82+
83+
ipcMain.handle("xfetch.get", async (_, url: string, headers?: Record<string, string>, config?: XFetchConfig) => {
84+
return await xfetch.get(url, headers, config);
85+
});
86+
87+
ipcMain.handle("xfetch.abort", async (_, uuid: string) => {
88+
return await xfetch.abort(uuid);
89+
});
90+
91+
ipcMain.handle("utils.openURL", async (_, url: string) => {
92+
return await shell.openExternal(url);
93+
});
94+
95+
ipcMain.handle("setting.get", async () => {
96+
return await setting.get();
97+
});
98+
99+
ipcMain.handle("setting.set", async (_, settings: any) => {
100+
return await setting.set(settings);
101+
});
102+
}
103+
104+
export default {
105+
init
106+
};

0 commit comments

Comments
 (0)