【问题描述】:
我第一次尝试使用 message-ports-reply-streams 时收到此错误消息:Invalid value for transfer。
在preload.js 我定义了这个api:
contextBridge.exposeInMainWorld(
"api", {
electronIpcPostMessage: (channel: string, message: any, transfer?: MessagePort[]) => {
ipcRenderer.postMessage(channel, message, transfer)
},
}
)
declare global {
interface Window {
api: {
electronIpcPostMessage: (channel: string, message: any, transfer?: MessagePort[]) => void;
}
}
并且,按照此处找到的示例:,在renderer React component 中,我将streaming request 定义如下:
const Layers = () => {
const componentIsMounted = React.useRef(true)
React.useEffect(() => {
const cb = (event, args) => {
try {
if (componentIsMounted.current) {
console.log("react-leaflet-layers-args: ", args)
}
} catch (err) {
console.log("err: ", err)
}
}
const makeStreamingRequest = (element, cb) => {
// MessageChannels are lightweight--it's cheap to create a new one for each request.
const { port1, port2 } = new MessageChannel()
// We send one end of the port to the main process ...
window.api.electronIpcPostMessage(
'give-me-a-stream',
{ element, count: 10 },
[port2]
)
// ... and we hang on to the other end.
// The main process will send messages to its end of the port,
// and close it when it's finished.
port1.onmessage = (event) => {
cb(event.data)
}
port1.onclose = () => {
console.log('stream ended')
}
}
makeStreamingRequest(42, (data) => {
console.log('got response data:', event.data)
})
// We will see "got response data: 42" 10 times.
return () => { // clean-up function
componentIsMounted.current = false
window.api.electronIpcRemoveListener(
"give-me-a-stream",
cb,
)
}
}, [])
如前所述,在运行Electron-React app 时,我在访问该组件呈现的页面时得到的error message 是: Invalid value for transfer。
从这个StackOverflow 问题:Invalid value for transfer while using ipcRenderer.postMessage of electron,看来我不是唯一一个遇到此类错误的人,但我还没有找到任何解决方案。
我做错了什么或错过了什么?如何解决问题?
我的目标是以streaming 的方式更好地发送一个非常大的geojson 文件,从main 进程到renderer 进程。这就是为什么我想尝试使用ipcRenderer.postMessage。顺便说一句,欢迎任何其他实现此目标的working 解决方案。
其他信息:
electron: v. 16
node: v. 16.13.0
O.S: Ubuntu 20.04
期待提示和帮助
【问题讨论】:
© 版权声明
本站下载的源码均来自公开网络收集转发二次开发而来,
若侵犯了您的合法权益,请来信通知我们1413333033@qq.com,
我们会及时删除,给您带来的不便,我们深表歉意。
下载用户仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,访问及下载者下载默认同意本站声明的免责申明,请合理使用切勿商用。
THE END
暂无评论内容