Skip to content

Commit

Permalink
chore: add show-me for utilityProcess (#1651)
Browse files Browse the repository at this point in the history
chore: add show-me for utilityProcess
  • Loading branch information
codebytere authored Nov 19, 2024
1 parent 7a37cd9 commit 3452e12
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const SHOW_ME_TEMPLATES: Templates = {
SystemPreferences: 'systemPreferences',
TouchBar: 'TouchBar',
Tray: 'Tray',
utilityProcess: 'utilityProcess',
WebContents: 'WebContents',
WebFrame: 'WebFrame',
},
Expand Down
5 changes: 5 additions & 0 deletions static/show-me/utilityprocess/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
process.parentPort.on('message', (e) => {
if (e.data === 'Hello from parent!') {
process.parentPort.postMessage('Hello from child!')
}
})
26 changes: 26 additions & 0 deletions static/show-me/utilityprocess/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The "utilityProcess" module allows creating a child process with
// Node.js and Message ports enabled.It provides the equivalent of[`child_process.fork`][] API from Node.js
// but instead uses[Services API][] from Chromium to launch the child process.
//
// For more info, see:
// https://electronjs.org/docs/api/utility-process

const { app, utilityProcess } = require('electron/main')
const path = require('node:path')

app.whenReady().then(() => {
const child = utilityProcess.fork(path.join(__dirname, 'child.js'))

child.on('spawn', () => {
console.log(`Child process spawned with PID: ${child.pid}`)
child.postMessage('Hello from parent!')
})

child.on('message', (message) => {
console.log(`Received message from child: ${message}`)
})

child.on('exit', (code) => {
console.log(`Child exited with code: ${code}`)
})
})

0 comments on commit 3452e12

Please sign in to comment.