Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

渲染进程给主进程发布消息,渲染进程得不到主进程传递的回调消息 #19

Open
core-admin opened this issue Jul 9, 2022 · 0 comments

Comments

@core-admin
Copy link

core-admin commented Jul 9, 2022

主进程在监听了一个消息类型,然后在渲染进程在发送这个消息,主进程整成被监听到了,回调函数正常被执行,此时调用cb给渲染进程传递消息,cb函数执行体内,并不会将消息体传递给渲染进程,导致渲染进程接收消息的promise一直显示超时错误,经过排查,发现,在渲染进程执行request方法时,存储了当前promise的控制权函数,正常来说,当主进程接收到此消息后,回调函数被调用,回调函数的第二个参数cb被执行时,内部应该把存储在渲染进程中的控制promise是否成功的函数执行,而进过debugger发现,主进程在执行存储在requestCb内的函数时,是不存在的,因为函数被存储在了渲染进程的requestCb中。

image

image

上图中,main.js 取 requestCb中存储的回调函数取的是主进程的requestCb变量,而渲染进程发送消息时,存储的回调是存储到了render.js中的requestCb变量中的。

// 入口文件 main.js 主进程
ipc.response('home_view', (data, cb) => {
  console.log(data);
  cb('main send message');
});
// 渲染进程 index.vue 中的script postMessageToMainProcess由click行为触发
async function postMessageToMainProcess() {
  debugger;
  const res = await ipc.request('main', 'home_view', 'home view message');
  console.log(res);
}

附上仓库地址:https://gitee.com/fslfsl/electron-client.git

不知道是我的用法有问题,还是代码的问题,请作者赐教,感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant