Compare commits

...

3 Commits

Author SHA1 Message Date
Leo Wang(草鞋没号)
11e9c4bffa feat: better ipcRenderer api expose 2024-03-11 16:19:01 +08:00
Leo Wang(草鞋没号)
a9952f16c8 feat: upgrade to electron@29 #467 2024-03-11 11:10:58 +08:00
Leo Wang(草鞋没号)
8a73559962 chore: bump deps 2024-03-11 11:03:06 +08:00
2 changed files with 28 additions and 28 deletions

View File

@ -1,26 +1,27 @@
import { ipcRenderer, contextBridge } from 'electron'
// --------- Expose some API to the Renderer process ---------
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
contextBridge.exposeInMainWorld('ipcRenderer', {
on(...args: Parameters<typeof ipcRenderer.on>) {
const [channel, listener] = args
ipcRenderer.on(channel, (event, ...args) => listener(event, ...args))
},
off(...args: Parameters<typeof ipcRenderer.off>) {
const [channel, ...omit] = args
ipcRenderer.off(channel, ...omit)
},
send(...args: Parameters<typeof ipcRenderer.send>) {
const [channel, ...omit] = args
ipcRenderer.send(channel, ...omit)
},
invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
const [channel, ...omit] = args
ipcRenderer.invoke(channel, ...omit)
},
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
function withPrototype(obj: Record<string, any>) {
const protos = Object.getPrototypeOf(obj)
for (const [key, value] of Object.entries(protos)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) continue
if (typeof value === 'function') {
// Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function.
obj[key] = function (...args: any) {
return value.call(obj, ...args)
}
} else {
obj[key] = value
}
}
return obj
}
// You can expose other APTs you need here.
// ...
})
// --------- Preload scripts loading ---------
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {

View File

@ -25,15 +25,14 @@
"preview": "vite preview"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.0.1",
"electron": "^28.1.0",
"electron-builder": "^24.9.1",
"tree-kill": "^1.2.2",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-electron": "^0.28.0",
"@vitejs/plugin-vue": "^5.0.4",
"electron": "^29.1.1",
"electron-builder": "^24.13.3",
"typescript": "^5.4.2",
"vite": "^5.1.5",
"vite-plugin-electron": "^0.28.2",
"vite-plugin-electron-renderer": "^0.14.5",
"vue": "^3.4.1",
"vue-tsc": "^1.8.27"
"vue": "^3.4.21",
"vue-tsc": "^2.0.6"
}
}