Skip to content

Commit

Permalink
feat: use ipcRenderer demo
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed Jan 3, 2024
1 parent 7ec0463 commit 3bdccd2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function createWindow() {

// Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
contextIsolation: false,
// contextIsolation: false,
},
})

Expand Down
25 changes: 25 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
import { ipcRenderer, contextBridge } from 'electron'

// --------- Expose some API to the Renderer process ---------
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))

// `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
}

// --------- Preload scripts loading ---------
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
return new Promise((resolve) => {
if (condition.includes(document.readyState)) {
Expand Down
3 changes: 1 addition & 2 deletions src/demos/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ipcRenderer } from 'electron'

ipcRenderer.on('main-process-message', (_event, ...args) => {
window.ipcRenderer.on('main-process-message', (_event, ...args) => {
console.log('[Receive Main-process message]:', ...args)
})
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import App from './App.vue'

import './style.css'

// `nodeIntegration` needs to be enabled in the Main process.
import './demos/ipc'
// If you want use Node.js, the`nodeIntegration` needs to be enabled in the Main process.
// import './demos/node'
// import './demos/ipc'

createApp(App)
.mount('#app')
Expand Down
5 changes: 5 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ declare module '*.vue' {
const component: DefineComponent<{}, {}, any>
export default component
}

interface Window {
// expose in the `electron/preload/index.ts`
ipcRenderer: import('electron').IpcRenderer
}

0 comments on commit 3bdccd2

Please sign in to comment.