index.js 892 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const { ipcMain: ipc } = require('electron')
  2. const path = require('path')
  3. const fs = require('fs')
  4. /**
  5. * 发送响应信息给渲染进程
  6. * @param event
  7. * @param channel
  8. * @param data
  9. * @private
  10. */
  11. const _echo = (event, channel, data) => {
  12. event.reply(`${channel}`, data)
  13. }
  14. /**
  15. * 执行主进程函数,并响应渲染进程
  16. * @param channel
  17. * @param callback
  18. */
  19. module.exports.answerRenderer = (channel, callback) => {
  20. ipc.on(channel, async (event, param) => {
  21. const result = await callback(param)
  22. _echo(event, channel, result)
  23. })
  24. }
  25. /**
  26. * 加载所有的主程序
  27. */
  28. module.exports.setup = () => {
  29. const ipcDir = path.normalize(__dirname + '/')
  30. fs.readdirSync(ipcDir).forEach(function (filename) {
  31. if (path.extname(filename) === '.js' && filename !== 'index.js') {
  32. const filePath = path.join(ipcDir, filename)
  33. require(filePath)
  34. }
  35. })
  36. }