ipc-renderer.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const { ipc } = process.electronBinding('ipc');
  3. const v8Util = process.electronBinding('v8_util');
  4. // Created by init.js.
  5. const ipcRenderer = v8Util.getHiddenValue(global, 'ipc');
  6. const internal = false;
  7. if (!ipcRenderer.send) {
  8. ipcRenderer.send = function (channel, ...args) {
  9. return ipc.send(internal, channel, args);
  10. };
  11. ipcRenderer.sendSync = function (channel, ...args) {
  12. const result = ipc.sendSync(internal, channel, args);
  13. if (!Array.isArray(result) || result.length !== 1) {
  14. throw new Error(`Unexpected return value from ipcRenderer.sendSync: ${result}`);
  15. }
  16. return result[0];
  17. };
  18. ipcRenderer.sendToHost = function (channel, ...args) {
  19. return ipc.sendToHost(channel, args);
  20. };
  21. ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
  22. return ipc.sendTo(internal, false, webContentsId, channel, args);
  23. };
  24. ipcRenderer.sendToAll = function (webContentsId, channel, ...args) {
  25. return ipc.sendTo(internal, true, webContentsId, channel, args);
  26. };
  27. ipcRenderer.invoke = function (channel, ...args) {
  28. return ipc.invoke(channel, args).then(({ error, result }) => {
  29. if (error) { throw new Error(`Error invoking remote method '${channel}': ${error}`); }
  30. return result;
  31. });
  32. };
  33. }
  34. module.exports = ipcRenderer;