Skip to content

Commit a9952f1

Browse files
committed
feat: upgrade to electron@29 #467
1 parent 8a73559 commit a9952f1

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

electron/preload/index.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
import { ipcRenderer, contextBridge } from 'electron'
22

33
// --------- Expose some API to the Renderer process ---------
4-
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
4+
contextBridge.exposeInMainWorld('app', {
5+
onEvent(cb) {
6+
const channel = 'main-process-message'
7+
const channel2 = 'other-ipc-channel'
58

6-
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
7-
function withPrototype(obj: Record<string, any>) {
8-
const protos = Object.getPrototypeOf(obj)
9-
10-
for (const [key, value] of Object.entries(protos)) {
11-
if (Object.prototype.hasOwnProperty.call(obj, key)) continue
12-
13-
if (typeof value === 'function') {
14-
// Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function.
15-
obj[key] = function (...args: any) {
16-
return value.call(obj, ...args)
17-
}
18-
} else {
19-
obj[key] = value
20-
}
21-
}
22-
return obj
23-
}
9+
ipcRenderer.on(channel, (_e, ...args) => cb(channel, ...args))
10+
ipcRenderer.on(channel2, (_e, ...args) => cb(channel2, ...args))
11+
},
12+
})
2413

2514
// --------- Preload scripts loading ---------
2615
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {

src/demos/ipc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
window.ipcRenderer.on('main-process-message', (_event, ...args) => {
3-
console.log('[Receive Main-process message]:', ...args)
2+
window.app.onEvent((channel, ...args) => {
3+
console.log(channel, ...args)
44
})

src/vite-env.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ declare module '*.vue' {
77
}
88

99
interface Window {
10-
// expose in the `electron/preload/index.ts`
11-
ipcRenderer: import('electron').IpcRenderer
10+
app: {
11+
// Expose in the `electron/preload/index.ts`
12+
onEvent: (cb: (channel: string, ...args: any[]) => void) => void
13+
}
1214
}

0 commit comments

Comments
 (0)