久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何向 ipc 渲染器發送添加回調

How to add a callback to ipc renderer send(如何向 ipc 渲染器發送添加回調)
本文介紹了如何向 ipc 渲染器發送添加回調的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

谷歌上說你可以給它添加一個回調,但文檔只是說arg1、arg2、arg3"等等.

Googling says you can add a callback to it, but the documentation just says "arg1, arg2, arg3" etc.

他們也有 sendSync,但我不希望在發送我的事件時阻止 [我們試圖通過瀏覽器做盡可能多的工作,因為在節點中編寫客戶端代碼似乎有點愚蠢].

They also have sendSync, but I'd prefer not to block while my event is being sent [we're trying to do as much work through the browser as possible, because writing client code in node, seems a little daft].

如果創建者有一個 sendSync,那么他們肯定有一個帶有回調的版本,或者更好的承諾.

If the creators have a sendSync, then surely they have a version with callbacks, or better yet promises.

一些我想做的事情的例子:

Some examples of things i'd like to be able to do:

//callback
ipcRenderer.send('anaction', '[1, 2, 3]', function() { console.log('done anaction') });
//promise
ipcRenderer.send('anaction', '[1, 2, 3]')
    .then(function() { console.log('done anaction') });

//sync exists, but it blocks. I'm looking for a non-blocking function
ipcRenderer.sendSync('anacount', '[1, 2, 3]')
console.log('done anaction');

推薦答案

如果有人在 2020 年仍在尋找這個問題的答案,處理從主線程回復到渲染器的更好方法是不要使用send,而是使用 ipcMain.handleipcRenderer.invoke,利用async/await并返回Promises:

In case anybody is still looking for an answer to this question in 2020, a better way to handle replying from the main thread back to the renderer is not to use send at all, but rather to use ipcMain.handle and ipcRenderer.invoke, which make use of async/await and return Promises:

ma??in.js

import { ipcMain } from 'electron';

ipcMain.handle('an-action', async (event, arg) => {
    // do stuff
    await awaitableProcess();
    return "foo";
}

renderer.js

import { ipcRenderer } from 'electron';

(async () => {
    const result = await ipcRenderer.invoke('an-action', [1, 2, 3]);
    console.log(result); // prints "foo"
})();


ipcMain.handleipcRenderer.invokecontextBridge,允許您公開一個 API 以向主線程請求來自渲染器線程的數據,并在它返回后對其進行處理.


ipcMain.handle and ipcRenderer.invoke are compatible with contextBridge, allowing you to expose an API to ask the main thread for data from the renderer thread and do something with it once it comes back.

ma??in.js

import { ipcMain, BrowserWindow } from 'electron';

ipcMain.handle('an-action', async (event, arg) => {
    // do stuff
    await awaitableProcess();
    return "foo";
}

new BrowserWindow({
    ...
    webPreferences: {
        contextIsolation: true,
        preload: "preload.js" // MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY if you're using webpack
    }
    ...
});

preload.js

import { ipcRenderer, contextBridge } from 'electron';

// Adds an object 'api' to the global window object:
contextBridge.exposeInMainWorld('api', {
    doAction: async (arg) => {
        return await ipcRenderer.invoke('an-action', arg);
    }
});

renderer.js

(async () => {
    const response = await window.api.doAction([1,2,3]);
    console.log(response); // we now have the response from the main thread without exposing
                           // ipcRenderer, leaving the app less vulnerable to attack    
})();

這篇關于如何向 ipc 渲染器發送添加回調的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 91调教打屁股xxxx网站 | 一区二区三区在线看 | 久热久 | 国产精品99久久久久久久久 | 欧美成人精品一区 | 久久精品视频免费看 | 成人亚洲精品 | 波多野结衣之双调教hd | 一级肉体全黄裸片 | 欧美日本| 亚洲国产黄色 | av在线免费网站 | av黄色片| 欧美美女一区二区 | 男女操操操| 欧美色婷婷 | 午夜在线观看视频网站 | 国产精品国产三级国产 | 一二区视频| 日韩一区二区三区在线播放 | 日韩午夜在线观看 | 日本毛片在线观看 | 久久免费小视频 | 亚洲第一在线 | 久久亚洲免费视频 | 欧美综合一区二区三区 | 精品一区二区三区四区五区 | www.日本在线 | 欧美日韩在线一区 | 亚洲一区二区欧美 | 乳大翘臀1v1h糙汉 | 亚洲图片一区二区 | 伊人久久久 | 欧美视频一区二区三区 | 日韩 国产 欧美 | 免费av一区二区 | 免费国产视频 | 一级片久久 | 在线成人小视频 | 中文字幕不卡视频 | 国产一区高清 |