久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 天天干天天干 | 九九热这里 | 视频一区二区在线观看 | 99久久精品免费看国产免费软件 | 91在线看| 日本黄色激情视频 | 成人亚洲精品久久久久软件 | 免费在线播放黄色 | 999久久久| 国产日韩精品视频 | 在线免费观看毛片 | 亚洲va在线va天堂va狼色在线 | 免费观看a级毛片在线播放 黄网站免费入口 | 欧美国产精品 | 理论片午午伦夜理片影院 | 亚洲精品日韩一区二区电影 | 国产97人人超碰caoprom | 人人做人人澡人人爽欧美 | 日韩国产精品一区二区三区 | 黑人中文字幕一区二区三区 | 91精品久久久久久久久久入口 | 精品国产乱码久久久久久影片 | 日韩一区二区不卡 | 日本黄视频在线观看 | 成人av片在线观看 | 国产xxxx岁13xxxxhd | 亚洲免费一区二区 | 欧美激情亚洲激情 | www国产成人 | 91最新在线视频 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 99九九视频| 亚洲精品乱码久久久久久按摩观 | 在线观看第一区 | 午夜精品一区二区三区在线观看 | 中文日韩在线 | 欧美精品一二三区 | 在线播放国产一区二区三区 | 中文在线播放 | 久久久久久网站 | 国产中文原创 |