Skip to content

Commit

Permalink
Merge pull request #99 from mashirooooo/main
Browse files Browse the repository at this point in the history
feat: ipc类型更改
  • Loading branch information
umbrella22 authored Jun 20, 2023
2 parents b733c5f + fff25ba commit b74f1d1
Show file tree
Hide file tree
Showing 15 changed files with 606 additions and 274 deletions.
248 changes: 248 additions & 0 deletions src/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@

import type { IpcMainInvokeEvent, IpcRendererEvent, MessageBoxOptions, MessageBoxReturnValue, PrinterInfo, WebContentsPrintOptions } from "electron"
import { ProgressInfo } from "electron-updater";


type IpcMainEventListener<Send = void, Receive = void> = {
ipcMainHandle: (event: IpcMainInvokeEvent, args: Send) => Receive | Promise<Receive>;
ipcRendererInvoke: (args: Send) => Promise<Receive>;
}

type IpcRendererEventListener<Send = void> = {
ipcRendererOn: (event: IpcRendererEvent, args?: Send) => void;
webContentSend: (args: Send) => void;
}

export const enum IpcChannel {

/**
* 是否使用无边框
*/
IsUseSysTitle = "IsUseSysTitle",

/**
* 窗口最小化
*/
WindowMini = "windows-mini",

/**
* 窗口最大化
*/
WindowMax = "window-max",

/**
* 窗口关闭
*/
WindowClose = "window-close",

/**
* 检查更新
*/
CheckUpdate = "check-update",

/**
* 确认更新
*/
ConfirmUpdate = "confirm-update",

/**
* app退出
*/
AppClose = "app-close",

/**
* 获取静态资源路径
*/
GetStaticPath = "get-static-path",

/**
* 打开系统弹窗信息
*/
OpenMessagebox = "open-messagebox",

/**
* 打开系统错误弹窗信息
*/
OpenErrorbox = "open-errorbox",

/**
* 开启http服务
*/
StartServer = "start-server",

/**
* 停止http服务
*/
StopServer = "stop-server",

/**
* 增量更新
*/
HotUpdate = "hot-update",

/**
* 增量更新2
*/
HotUpdateTest = "hot-update-test",

/**
* 下载东西
*/
StartDownload = "start-download",

/**
* 打开新的弹窗
*/
OpenWin = "open-win",


/**
* 获取打印机信息
*/
GetPrinters = "getPrinters",

/**
* 打印
*/
PrintHandlePrint = "printHandlePrint",

/**
* 打开测试打印页面
*/
OpenPrintDemoWindow = "openPrintDemoWindow",



/**
* 下载进度回调
*/
DownloadProgress = "download-progress",

/**
* 下载错误回调
*/
DownloadError = "download-error",

/**
* 下载暂停回调
*/
DownloadPaused = "download-paused",

/**
* 下载完成回调
*/
DownloadDone = "download-done",

UpdateMsg = "UpdateMsg",

/**
* 热更新状态回调
*/
HotUpdateStatus = "hot-update-status",

/**
* 数据测试回调
*/
SendDataTest = "send-data-test",

}





type IpcMainEvent = {
[IpcChannel.AppClose]: IpcMainEventListener
[IpcChannel.CheckUpdate]: IpcMainEventListener
[IpcChannel.ConfirmUpdate]: IpcMainEventListener
[IpcChannel.GetStaticPath]: IpcMainEventListener<void, string>
[IpcChannel.HotUpdate]: IpcMainEventListener
[IpcChannel.HotUpdateTest]: IpcMainEventListener
[IpcChannel.IsUseSysTitle]: IpcMainEventListener<void, boolean>
[IpcChannel.OpenErrorbox]: IpcMainEventListener<{ title: string, message: string }, void>
[IpcChannel.OpenMessagebox]: IpcMainEventListener<MessageBoxOptions, MessageBoxReturnValue>
[IpcChannel.OpenWin]: IpcMainEventListener<{

/**
* 新的窗口地址
*
* @type {string}
*/
url: string,

/**
* 是否是支付页
*
* @type {boolean}
*/
IsPay?: boolean,

/**
* 支付参数
*
* @type {string}
*/
PayUrl?: string,

/**
* 发送的新页面数据
*
* @type {unknown}
*/
sendData?: unknown
}, void>
[IpcChannel.StartDownload]: IpcMainEventListener<string, void>
[IpcChannel.StartServer]: IpcMainEventListener<void, string>
[IpcChannel.StopServer]: IpcMainEventListener<void, string>
[IpcChannel.WindowClose]: IpcMainEventListener
[IpcChannel.WindowMax]: IpcMainEventListener<void, { status: boolean }>
[IpcChannel.WindowMini]: IpcMainEventListener
[IpcChannel.GetPrinters]: IpcMainEventListener<void, PrinterInfo[]>
[IpcChannel.PrintHandlePrint]: IpcMainEventListener<WebContentsPrintOptions, { success: boolean, failureReason: string }>
[IpcChannel.OpenPrintDemoWindow]: IpcMainEventListener
}
type IpcRenderderEvent = {
[IpcChannel.DownloadProgress]: IpcRendererEventListener<number>
[IpcChannel.DownloadError]: IpcRendererEventListener<boolean>
[IpcChannel.DownloadPaused]: IpcRendererEventListener<boolean>
[IpcChannel.DownloadDone]: IpcRendererEventListener<{

/**
* 下载的文件路径
*
* @type {string}
*/
filePath: string
}
>
[IpcChannel.UpdateMsg]: IpcRendererEventListener<{
state: number;
msg: string | ProgressInfo;
}>
[IpcChannel.HotUpdateStatus]: IpcRendererEventListener<{
status: "init" | "downloading" | "moving" | "finished" | "failed" | "download",
message: string
}>

[IpcChannel.SendDataTest]: IpcRendererEventListener<unknown>

}




export type IpcMainHandle = {
[Key in keyof IpcMainEvent]: IpcMainEvent[Key]["ipcMainHandle"]
}

export type IpcRendererInvoke = {
[Key in keyof IpcMainEvent]: IpcMainEvent[Key]["ipcRendererInvoke"]
}

export type IpcRendererOn = {
[Key in keyof IpcRenderderEvent]: IpcRenderderEvent[Key]["ipcRendererOn"]
}

export type WebContentSend = {
[Key in keyof IpcRenderderEvent]: IpcRenderderEvent[Key]["webContentSend"]
}
6 changes: 3 additions & 3 deletions src/main/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SingleServer {
}
server: Server;
statrServer() {
return new Promise((resolve, reject) => {
return new Promise((resolve: (value: string) => void, reject) => {
try {
this.server.listen(port);
resolve("服务端已经启动");
Expand All @@ -38,7 +38,7 @@ class SingleServer {
});
}
stopServer() {
return new Promise((resolve, reject) => {
return new Promise((resolve: (value: string) => void, reject) => {
this.server.close((err) => {
if (err) {
switch ((err as any).code) {
Expand All @@ -49,7 +49,7 @@ class SingleServer {
reject(err);
}
} else {
resolve(1);
resolve("服务端已关闭");
}
});
});
Expand Down
27 changes: 18 additions & 9 deletions src/main/services/HotUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import extract from 'extract-zip'
import { version } from '../../../package.json'
import { hotPublishConfig } from '../config/hotPublish'
import axios from 'axios'
import { webContentSend } from './ipcMain'
import { IpcChannel } from '../../ipc'

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

const updateInfo = {
const updateInfo: {
status: "init" | "downloading" | "moving" | "finished" | "failed";
message: string;
} = {
status: 'init',
message: ''
}
Expand All @@ -58,35 +63,39 @@ const updateInfo = {
* @date 2021-03-05
*/
export const updater = async (windows?: BrowserWindow) => {
const statusCallback = (status: {
status: "init" | "downloading" | "moving" | "finished" | "failed" ;
message: string;
}) => {
if (windows) webContentSend(windows.webContents, IpcChannel.HotUpdateStatus, status)
}
try {
const res = await request({ url: `${hotPublishConfig.url}/${hotPublishConfig.configName}.json?time=${new Date().getTime()}`, })
if (!gt(res.data.version, version)) return

await emptyDir(updatePath)
const filePath = join(updatePath, res.data.name)
updateInfo.status = 'downloading'
if (windows) windows.webContents.send('hot-update-status', updateInfo);
statusCallback(updateInfo);
await download(`${hotPublishConfig.url}/${res.data.name}`, filePath);
const buffer = await readFile(filePath)
const sha256 = hash(buffer)
if (sha256 !== res.data.hash) throw new Error('sha256 error')
const appPathTemp = join(updatePath, 'temp')
await extract(filePath, { dir: appPathTemp })
updateInfo.status = 'moving'
if (windows) windows.webContents.send('hot-update-status', updateInfo);
statusCallback(updateInfo);
await remove(join(`${appPath}`, 'dist'));
await remove(join(`${appPath}`, 'package.json'));
await copy(appPathTemp, appPath)
updateInfo.status = 'finished'
if (windows) windows.webContents.send('hot-update-status', updateInfo);
statusCallback(updateInfo);
resolve('success')



} catch (error) {
console.log(error)
updateInfo.status = 'failed'
updateInfo.message = error
if (windows) windows.webContents.send('hot-update-status', updateInfo)
updateInfo.message = error.message ? error.message : error
statusCallback(updateInfo)
}
}

Expand Down
Loading

0 comments on commit b74f1d1

Please sign in to comment.