feat: add ipcRenderer.invoke() (#18449)

This commit is contained in:
Jeremy Apthorp 2019-05-31 10:25:19 -07:00 committed by GitHub
parent b180fb376c
commit c436997840
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 389 additions and 20 deletions

View file

@ -57,10 +57,39 @@ Removes all listeners, or those of the specified `channel`.
* `...args` any[]
Send a message to the main process asynchronously via `channel`, you can also
send arbitrary arguments. Arguments will be serialized in JSON internally and
send arbitrary arguments. Arguments will be serialized as JSON internally and
hence no functions or prototype chain will be included.
The main process handles it by listening for `channel` with [`ipcMain`](ipc-main.md) module.
The main process handles it by listening for `channel` with the
[`ipcMain`](ipc-main.md) module.
### `ipcRenderer.invoke(channel[, arg1][, arg2][, ...])`
* `channel` String
* `...args` any[]
Returns `Promise<any>` - Resolves with the response from the main process.
Send a message to the main process asynchronously via `channel` and expect an
asynchronous result. Arguments will be serialized as JSON internally and
hence no functions or prototype chain will be included.
The main process should listen for `channel` with
[`ipcMain.handle()`](ipc-main.md#ipcmainhandlechannel-listener).
For example:
```javascript
// Renderer process
ipcRenderer.invoke('some-name', someArgument).then((result) => {
// ...
})
// Main process
ipcMain.handle('some-name', async (event, someArgument) => {
const result = await doSomeWork(someArgument)
return result
})
```
### `ipcRenderer.sendSync(channel[, arg1][, arg2][, ...])`