Skip to content

Commit b74f1d1

Browse files
authored
Merge pull request #99 from mashirooooo/main
feat: ipc类型更改
2 parents b733c5f + fff25ba commit b74f1d1

File tree

15 files changed

+606
-274
lines changed

15 files changed

+606
-274
lines changed

src/ipc.ts

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
2+
import type { IpcMainInvokeEvent, IpcRendererEvent, MessageBoxOptions, MessageBoxReturnValue, PrinterInfo, WebContentsPrintOptions } from "electron"
3+
import { ProgressInfo } from "electron-updater";
4+
5+
6+
type IpcMainEventListener<Send = void, Receive = void> = {
7+
ipcMainHandle: (event: IpcMainInvokeEvent, args: Send) => Receive | Promise<Receive>;
8+
ipcRendererInvoke: (args: Send) => Promise<Receive>;
9+
}
10+
11+
type IpcRendererEventListener<Send = void> = {
12+
ipcRendererOn: (event: IpcRendererEvent, args?: Send) => void;
13+
webContentSend: (args: Send) => void;
14+
}
15+
16+
export const enum IpcChannel {
17+
18+
/**
19+
* 是否使用无边框
20+
*/
21+
IsUseSysTitle = "IsUseSysTitle",
22+
23+
/**
24+
* 窗口最小化
25+
*/
26+
WindowMini = "windows-mini",
27+
28+
/**
29+
* 窗口最大化
30+
*/
31+
WindowMax = "window-max",
32+
33+
/**
34+
* 窗口关闭
35+
*/
36+
WindowClose = "window-close",
37+
38+
/**
39+
* 检查更新
40+
*/
41+
CheckUpdate = "check-update",
42+
43+
/**
44+
* 确认更新
45+
*/
46+
ConfirmUpdate = "confirm-update",
47+
48+
/**
49+
* app退出
50+
*/
51+
AppClose = "app-close",
52+
53+
/**
54+
* 获取静态资源路径
55+
*/
56+
GetStaticPath = "get-static-path",
57+
58+
/**
59+
* 打开系统弹窗信息
60+
*/
61+
OpenMessagebox = "open-messagebox",
62+
63+
/**
64+
* 打开系统错误弹窗信息
65+
*/
66+
OpenErrorbox = "open-errorbox",
67+
68+
/**
69+
* 开启http服务
70+
*/
71+
StartServer = "start-server",
72+
73+
/**
74+
* 停止http服务
75+
*/
76+
StopServer = "stop-server",
77+
78+
/**
79+
* 增量更新
80+
*/
81+
HotUpdate = "hot-update",
82+
83+
/**
84+
* 增量更新2
85+
*/
86+
HotUpdateTest = "hot-update-test",
87+
88+
/**
89+
* 下载东西
90+
*/
91+
StartDownload = "start-download",
92+
93+
/**
94+
* 打开新的弹窗
95+
*/
96+
OpenWin = "open-win",
97+
98+
99+
/**
100+
* 获取打印机信息
101+
*/
102+
GetPrinters = "getPrinters",
103+
104+
/**
105+
* 打印
106+
*/
107+
PrintHandlePrint = "printHandlePrint",
108+
109+
/**
110+
* 打开测试打印页面
111+
*/
112+
OpenPrintDemoWindow = "openPrintDemoWindow",
113+
114+
115+
116+
/**
117+
* 下载进度回调
118+
*/
119+
DownloadProgress = "download-progress",
120+
121+
/**
122+
* 下载错误回调
123+
*/
124+
DownloadError = "download-error",
125+
126+
/**
127+
* 下载暂停回调
128+
*/
129+
DownloadPaused = "download-paused",
130+
131+
/**
132+
* 下载完成回调
133+
*/
134+
DownloadDone = "download-done",
135+
136+
UpdateMsg = "UpdateMsg",
137+
138+
/**
139+
* 热更新状态回调
140+
*/
141+
HotUpdateStatus = "hot-update-status",
142+
143+
/**
144+
* 数据测试回调
145+
*/
146+
SendDataTest = "send-data-test",
147+
148+
}
149+
150+
151+
152+
153+
154+
type IpcMainEvent = {
155+
[IpcChannel.AppClose]: IpcMainEventListener
156+
[IpcChannel.CheckUpdate]: IpcMainEventListener
157+
[IpcChannel.ConfirmUpdate]: IpcMainEventListener
158+
[IpcChannel.GetStaticPath]: IpcMainEventListener<void, string>
159+
[IpcChannel.HotUpdate]: IpcMainEventListener
160+
[IpcChannel.HotUpdateTest]: IpcMainEventListener
161+
[IpcChannel.IsUseSysTitle]: IpcMainEventListener<void, boolean>
162+
[IpcChannel.OpenErrorbox]: IpcMainEventListener<{ title: string, message: string }, void>
163+
[IpcChannel.OpenMessagebox]: IpcMainEventListener<MessageBoxOptions, MessageBoxReturnValue>
164+
[IpcChannel.OpenWin]: IpcMainEventListener<{
165+
166+
/**
167+
* 新的窗口地址
168+
*
169+
* @type {string}
170+
*/
171+
url: string,
172+
173+
/**
174+
* 是否是支付页
175+
*
176+
* @type {boolean}
177+
*/
178+
IsPay?: boolean,
179+
180+
/**
181+
* 支付参数
182+
*
183+
* @type {string}
184+
*/
185+
PayUrl?: string,
186+
187+
/**
188+
* 发送的新页面数据
189+
*
190+
* @type {unknown}
191+
*/
192+
sendData?: unknown
193+
}, void>
194+
[IpcChannel.StartDownload]: IpcMainEventListener<string, void>
195+
[IpcChannel.StartServer]: IpcMainEventListener<void, string>
196+
[IpcChannel.StopServer]: IpcMainEventListener<void, string>
197+
[IpcChannel.WindowClose]: IpcMainEventListener
198+
[IpcChannel.WindowMax]: IpcMainEventListener<void, { status: boolean }>
199+
[IpcChannel.WindowMini]: IpcMainEventListener
200+
[IpcChannel.GetPrinters]: IpcMainEventListener<void, PrinterInfo[]>
201+
[IpcChannel.PrintHandlePrint]: IpcMainEventListener<WebContentsPrintOptions, { success: boolean, failureReason: string }>
202+
[IpcChannel.OpenPrintDemoWindow]: IpcMainEventListener
203+
}
204+
type IpcRenderderEvent = {
205+
[IpcChannel.DownloadProgress]: IpcRendererEventListener<number>
206+
[IpcChannel.DownloadError]: IpcRendererEventListener<boolean>
207+
[IpcChannel.DownloadPaused]: IpcRendererEventListener<boolean>
208+
[IpcChannel.DownloadDone]: IpcRendererEventListener<{
209+
210+
/**
211+
* 下载的文件路径
212+
*
213+
* @type {string}
214+
*/
215+
filePath: string
216+
}
217+
>
218+
[IpcChannel.UpdateMsg]: IpcRendererEventListener<{
219+
state: number;
220+
msg: string | ProgressInfo;
221+
}>
222+
[IpcChannel.HotUpdateStatus]: IpcRendererEventListener<{
223+
status: "init" | "downloading" | "moving" | "finished" | "failed" | "download",
224+
message: string
225+
}>
226+
227+
[IpcChannel.SendDataTest]: IpcRendererEventListener<unknown>
228+
229+
}
230+
231+
232+
233+
234+
export type IpcMainHandle = {
235+
[Key in keyof IpcMainEvent]: IpcMainEvent[Key]["ipcMainHandle"]
236+
}
237+
238+
export type IpcRendererInvoke = {
239+
[Key in keyof IpcMainEvent]: IpcMainEvent[Key]["ipcRendererInvoke"]
240+
}
241+
242+
export type IpcRendererOn = {
243+
[Key in keyof IpcRenderderEvent]: IpcRenderderEvent[Key]["ipcRendererOn"]
244+
}
245+
246+
export type WebContentSend = {
247+
[Key in keyof IpcRenderderEvent]: IpcRenderderEvent[Key]["webContentSend"]
248+
}

src/main/server/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SingleServer {
1616
}
1717
server: Server;
1818
statrServer() {
19-
return new Promise((resolve, reject) => {
19+
return new Promise((resolve: (value: string) => void, reject) => {
2020
try {
2121
this.server.listen(port);
2222
resolve("服务端已经启动");
@@ -38,7 +38,7 @@ class SingleServer {
3838
});
3939
}
4040
stopServer() {
41-
return new Promise((resolve, reject) => {
41+
return new Promise((resolve: (value: string) => void, reject) => {
4242
this.server.close((err) => {
4343
if (err) {
4444
switch ((err as any).code) {
@@ -49,7 +49,7 @@ class SingleServer {
4949
reject(err);
5050
}
5151
} else {
52-
resolve(1);
52+
resolve("服务端已关闭");
5353
}
5454
});
5555
});

src/main/services/HotUpdater.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import extract from 'extract-zip'
1313
import { version } from '../../../package.json'
1414
import { hotPublishConfig } from '../config/hotPublish'
1515
import axios from 'axios'
16+
import { webContentSend } from './ipcMain'
17+
import { IpcChannel } from '../../ipc'
1618

1719
const streamPipeline = promisify(pipeline)
1820
const appPath = app.getAppPath()
@@ -46,7 +48,10 @@ async function download(url: string, filePath: string) {
4648
await streamPipeline(res.data, createWriteStream(filePath))
4749
}
4850

49-
const updateInfo = {
51+
const updateInfo: {
52+
status: "init" | "downloading" | "moving" | "finished" | "failed";
53+
message: string;
54+
} = {
5055
status: 'init',
5156
message: ''
5257
}
@@ -58,35 +63,39 @@ const updateInfo = {
5863
* @date 2021-03-05
5964
*/
6065
export const updater = async (windows?: BrowserWindow) => {
66+
const statusCallback = (status: {
67+
status: "init" | "downloading" | "moving" | "finished" | "failed" ;
68+
message: string;
69+
}) => {
70+
if (windows) webContentSend(windows.webContents, IpcChannel.HotUpdateStatus, status)
71+
}
6172
try {
6273
const res = await request({ url: `${hotPublishConfig.url}/${hotPublishConfig.configName}.json?time=${new Date().getTime()}`, })
6374
if (!gt(res.data.version, version)) return
64-
6575
await emptyDir(updatePath)
6676
const filePath = join(updatePath, res.data.name)
6777
updateInfo.status = 'downloading'
68-
if (windows) windows.webContents.send('hot-update-status', updateInfo);
78+
statusCallback(updateInfo);
6979
await download(`${hotPublishConfig.url}/${res.data.name}`, filePath);
7080
const buffer = await readFile(filePath)
7181
const sha256 = hash(buffer)
7282
if (sha256 !== res.data.hash) throw new Error('sha256 error')
7383
const appPathTemp = join(updatePath, 'temp')
7484
await extract(filePath, { dir: appPathTemp })
7585
updateInfo.status = 'moving'
76-
if (windows) windows.webContents.send('hot-update-status', updateInfo);
86+
statusCallback(updateInfo);
7787
await remove(join(`${appPath}`, 'dist'));
7888
await remove(join(`${appPath}`, 'package.json'));
7989
await copy(appPathTemp, appPath)
8090
updateInfo.status = 'finished'
81-
if (windows) windows.webContents.send('hot-update-status', updateInfo);
91+
statusCallback(updateInfo);
8292
resolve('success')
8393

84-
85-
8694
} catch (error) {
95+
console.log(error)
8796
updateInfo.status = 'failed'
88-
updateInfo.message = error
89-
if (windows) windows.webContents.send('hot-update-status', updateInfo)
97+
updateInfo.message = error.message ? error.message : error
98+
statusCallback(updateInfo)
9099
}
91100
}
92101

0 commit comments

Comments
 (0)