ipcRenderer.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const { ipcRenderer: ipc } = window.require && window.require('electron') || {}
  2. /**
  3. * (将废弃,请使用 $ipcInvoke 代替)异步调用主函数
  4. * @param ipc
  5. * @param channel
  6. * @param param
  7. * @returns {Promise<unknown>}
  8. */
  9. const call = (ipc, channel, param) => {
  10. return new Promise((resolve) => {
  11. ipc.once(channel, (event, result) => {
  12. console.log('[ipcRenderer] [call] result:', result)
  13. resolve(result)
  14. })
  15. ipc.send(channel, param)
  16. })
  17. }
  18. /**
  19. * 发送异步消息(invoke/handle 模型)
  20. * @param ipc
  21. * @param channel
  22. * @param param
  23. * @returns {Promise}
  24. */
  25. const invoke = (ipc, channel, param) => {
  26. const message = ipc.invoke(channel, param);
  27. return message;
  28. }
  29. /**
  30. * 发送同步消息(send/on 模型)
  31. * @param ipc
  32. * @param channel
  33. * @param param
  34. * @returns {Any}
  35. */
  36. const sendSync = (ipc, channel, param) => {
  37. const message = ipc.sendSync(channel, param);
  38. return message;
  39. }
  40. export default {
  41. install(Vue) {
  42. Vue.prototype.$ipc = ipc // 全局注入ipc
  43. Vue.prototype.$ipcCall = (channel, param) => call(ipc, channel, param)
  44. Vue.prototype.$ipcInvoke = (channel, param) => invoke(ipc, channel, param)
  45. Vue.prototype.$ipcSendSync = (channel, param) => sendSync(ipc, channel, param)
  46. }
  47. }