-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Inter-Process Communication (IPC)
- Loading branch information
Showing
6 changed files
with
71 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// src/main/EventSystem.js | ||
const { ipcMain } = require('electron'); | ||
|
||
class EventSystem { | ||
constructor() { | ||
console.log("[EVENT-SYSTEM] created"); | ||
this.initializeIpcListeners(); | ||
} | ||
|
||
initializeIpcListeners() { | ||
ipcMain.on('renderer-event', (event, arg) => { | ||
console.log('[EVENT-SYSTEM] Received message from renderer:', arg); | ||
event.reply('main-event-response', 'Response from main process'); | ||
}); | ||
} | ||
|
||
test() { | ||
console.log('[EVENT-SYSTEM] test function executed'); | ||
} | ||
} | ||
|
||
module.exports = EventSystem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,19 @@ | ||
// See the Electron documentation for details on how to use preload scripts: | ||
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts | ||
// src/main/preload.js | ||
const { contextBridge, ipcRenderer } = require('electron'); | ||
|
||
contextBridge.exposeInMainWorld('api', { | ||
send: (channel, data) => { | ||
// whitelist channels to ensure security | ||
let validChannels = ['renderer-event']; // Make sure to use the correct channel name | ||
if (validChannels.includes(channel)) { | ||
ipcRenderer.send(channel, data); | ||
} | ||
}, | ||
receive: (channel, func) => { | ||
let validChannels = ['main-event-response']; // Make sure to use the correct channel name | ||
if (validChannels.includes(channel)) { | ||
// Deliberately strip event as it includes `sender` | ||
ipcRenderer.on(channel, (event, ...args) => func(...args)); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// src/renderer/RendererEventSystem.js | ||
export default class RendererEventSystem { | ||
constructor() { | ||
console.log("[R-EVENT-SYSTEM] created"); | ||
this.initializeIpcListeners(); | ||
} | ||
|
||
initializeIpcListeners() { | ||
window.api.receive('main-event-response', (data) => { | ||
console.log('[R-EVENT-SYSTEM] Received response from main process:', data); | ||
}); | ||
} | ||
|
||
test() { | ||
console.log('[R-EVENT-SYSTEM] test function executed'); | ||
window.api.send('renderer-event', 'Hello to Better Prompts'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// renderer.js | ||
import './index.css'; | ||
import RendererEventSystem from './RendererEventSystem'; | ||
|
||
console.log('👋 This message is being logged by "renderer.js", included via webpack'); | ||
let rEventSystem = new RendererEventSystem(); | ||
rEventSystem.test(); |