docs: avoid leaking the IpcRendererEvent in contextBridge examples (#40321)

* docs: avoid leaking the `IpcRendererEvent` in `contextBridge` examples

* Update docs/fiddles/ipc/pattern-3/preload.js

Co-authored-by: David Sanders <dsanders11@ucsbalum.com>

* Update docs/tutorial/ipc.md

Co-authored-by: David Sanders <dsanders11@ucsbalum.com>

* Update docs/tutorial/ipc.md

Co-authored-by: David Sanders <dsanders11@ucsbalum.com>

---------

Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
This commit is contained in:
Milan Burda 2023-11-01 13:46:25 -04:00 committed by GitHub
parent 425efb5e47
commit b163187235
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View file

@ -1,5 +1,6 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')
contextBridge.exposeInMainWorld('electronAPI', {
handleCounter: (callback) => ipcRenderer.on('update-counter', () => callback())
onUpdateCounter: (callback) => ipcRenderer.on('update-counter', (_event, value) => callback(value)),
counterValue: (value) => ipcRenderer.send('counter-value', value)
})

View file

@ -1,8 +1,8 @@
const counter = document.getElementById('counter')
window.electronAPI.handleCounter((event, value) => {
window.electronAPI.onUpdateCounter((value) => {
const oldValue = Number(counter.innerText)
const newValue = oldValue + value
counter.innerText = newValue
event.sender.send('counter-value', newValue)
counter.innerText = newValue.toString()
window.electronAPI.counterValue(newValue)
})