Skip to content

Commit 3452e12

Browse files
authored
chore: add show-me for utilityProcess (#1651)
chore: add show-me for utilityProcess
1 parent 7a37cd9 commit 3452e12

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/templates.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const SHOW_ME_TEMPLATES: Templates = {
2727
SystemPreferences: 'systemPreferences',
2828
TouchBar: 'TouchBar',
2929
Tray: 'Tray',
30+
utilityProcess: 'utilityProcess',
3031
WebContents: 'WebContents',
3132
WebFrame: 'WebFrame',
3233
},
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
process.parentPort.on('message', (e) => {
2+
if (e.data === 'Hello from parent!') {
3+
process.parentPort.postMessage('Hello from child!')
4+
}
5+
})

static/show-me/utilityprocess/main.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// The "utilityProcess" module allows creating a child process with
2+
// Node.js and Message ports enabled.It provides the equivalent of[`child_process.fork`][] API from Node.js
3+
// but instead uses[Services API][] from Chromium to launch the child process.
4+
//
5+
// For more info, see:
6+
// https://electronjs.org/docs/api/utility-process
7+
8+
const { app, utilityProcess } = require('electron/main')
9+
const path = require('node:path')
10+
11+
app.whenReady().then(() => {
12+
const child = utilityProcess.fork(path.join(__dirname, 'child.js'))
13+
14+
child.on('spawn', () => {
15+
console.log(`Child process spawned with PID: ${child.pid}`)
16+
child.postMessage('Hello from parent!')
17+
})
18+
19+
child.on('message', (message) => {
20+
console.log(`Received message from child: ${message}`)
21+
})
22+
23+
child.on('exit', (code) => {
24+
console.log(`Child exited with code: ${code}`)
25+
})
26+
})

0 commit comments

Comments
 (0)