mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-01-31 12:06:40 +08:00
feat: upgrade to electron@29 #467
This commit is contained in:
parent
8a73559962
commit
a9952f16c8
@ -1,26 +1,15 @@
|
|||||||
import { ipcRenderer, contextBridge } from 'electron'
|
import { ipcRenderer, contextBridge } from 'electron'
|
||||||
|
|
||||||
// --------- Expose some API to the Renderer process ---------
|
// --------- Expose some API to the Renderer process ---------
|
||||||
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
|
contextBridge.exposeInMainWorld('app', {
|
||||||
|
onEvent(cb) {
|
||||||
|
const channel = 'main-process-message'
|
||||||
|
const channel2 = 'other-ipc-channel'
|
||||||
|
|
||||||
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
|
ipcRenderer.on(channel, (_e, ...args) => cb(channel, ...args))
|
||||||
function withPrototype(obj: Record<string, any>) {
|
ipcRenderer.on(channel2, (_e, ...args) => cb(channel2, ...args))
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------- Preload scripts loading ---------
|
// --------- Preload scripts loading ---------
|
||||||
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
|
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
|
|
||||||
window.ipcRenderer.on('main-process-message', (_event, ...args) => {
|
window.app.onEvent((channel, ...args) => {
|
||||||
console.log('[Receive Main-process message]:', ...args)
|
console.log(channel, ...args)
|
||||||
})
|
})
|
||||||
|
6
src/vite-env.d.ts
vendored
6
src/vite-env.d.ts
vendored
@ -7,6 +7,8 @@ declare module '*.vue' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Window {
|
interface Window {
|
||||||
// expose in the `electron/preload/index.ts`
|
app: {
|
||||||
ipcRenderer: import('electron').IpcRenderer
|
// Expose in the `electron/preload/index.ts`
|
||||||
|
onEvent: (cb: (channel: string, ...args: any[]) => void) => void
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user