feat: setup IPC communication framework

- Add IPC types definition with all communication channels
- Implement preload script with API surface for renderer process
- Add IPC handler registration stub in main entry point

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-02-28 22:19:21 +08:00
parent f112046178
commit 7cfbce7505
3 changed files with 43 additions and 5 deletions

View File

@@ -52,6 +52,12 @@ app.whenReady().then(() => {
// IPC test
ipcMain.on('ping', () => console.log('pong'))
// IPC Handler Registration
// TODO: Register IPC handlers for:
// - automation:clean, automation:extract, automation:stop, automation:progress
// - database:query-production-orders, database:query-materials-to-delete
// - auth:login, auth:logout
createWindow()
app.on('activate', function () {

View File

@@ -0,0 +1,10 @@
export interface IPCChannels {
'automation:clean': any;
'automation:extract': any;
'automation:stop': any;
'automation:progress': any;
'database:query-production-orders': any;
'database:query-materials-to-delete': any;
'auth:login': any;
'auth:logout': any;
}

View File

@@ -1,8 +1,28 @@
import { contextBridge } from 'electron'
import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
// Custom APIs for renderer
const api = {}
const API = {
automation: {
clean: (params: any) => ipcRenderer.invoke('automation:clean', params),
extract: (params: any) => ipcRenderer.invoke('automation:extract', params),
stop: () => ipcRenderer.invoke('automation:stop'),
onProgress: (callback: (progress: any) => void) => {
const listener = (_: any, progress: any) => callback(progress);
ipcRenderer.on('automation:progress', listener);
return () => ipcRenderer.removeListener('automation:progress', listener);
},
},
database: {
queryProductionOrders: (productionIds: string[]) =>
ipcRenderer.invoke('database:query-production-orders', productionIds),
queryMaterialsToDelete: (managerNames: string[] | null) =>
ipcRenderer.invoke('database:query-materials-to-delete', managerNames),
},
auth: {
login: (credentials: any) => ipcRenderer.invoke('auth:login', credentials),
logout: () => ipcRenderer.invoke('auth:logout'),
},
}
// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
@@ -10,7 +30,7 @@ const api = {}
if (process.contextIsolated) {
try {
contextBridge.exposeInMainWorld('electron', electronAPI)
contextBridge.exposeInMainWorld('api', api)
contextBridge.exposeInMainWorld('api', API)
} catch (error) {
console.error(error)
}
@@ -18,5 +38,7 @@ if (process.contextIsolated) {
// @ts-ignore (define in dts)
window.electron = electronAPI
// @ts-ignore (define in dts)
window.api = api
window.api = API
}
export type API = typeof API