ipcProviderWrapper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. @module MistUI
  3. */
  4. /**
  5. The IPC provider wrapper to communicate to the backend
  6. @class ipcProviderWrapper
  7. @constructor
  8. */
  9. const { ipcRenderer } = require('electron');
  10. /**
  11. Gets the writable property.
  12. @method on('ipcProvider-setWritable')
  13. */
  14. ipcRenderer.on('ipcProvider-setWritable', (e, writable) => {
  15. // console.debug('ipcProvider-setWritable', writable);
  16. ipcProviderWrapper.writable = writable;
  17. });
  18. const ipcProviderWrapper = {
  19. writable: false,
  20. /**
  21. Connects the IPC on the backend to the geth node
  22. Note: web3.eth.net.isListening (previously: web3.isConnected) will always return true,
  23. as otherwise race conditions can occur,
  24. letting it look like youre not connected via IPC.
  25. @method connect
  26. */
  27. connect(path) {
  28. // console.debug('ipcProviderWrapper: connect');
  29. ipcRenderer.send('ipcProvider-create', path);
  30. return this;
  31. },
  32. /**
  33. Returns data from the IPC through the backend
  34. @method on
  35. @param {String} name `connect`, `error`, `end`, `timeout` or `data`
  36. @param {Funciton} callback
  37. */
  38. on(name, callback) {
  39. // console.debug('ipcProviderWrapper: add listener', name);
  40. ipcRenderer.on(`ipcProvider-${name}`, (e, result) => {
  41. callback(result);
  42. });
  43. },
  44. /**
  45. Returns data from the IPC through the backend
  46. @method once
  47. @param {String} name `connect`, `error`, `end`, `timeout` or `data`
  48. @param {Funciton} callback
  49. */
  50. once(name, callback) {
  51. // console.debug('ipcProviderWrapper: add listener', name);
  52. ipcRenderer.once(`ipcProvider-${name}`, (e, result) => {
  53. callback(result);
  54. });
  55. },
  56. /**
  57. Removes listener
  58. @method removeListener
  59. */
  60. removeListener(name, callback) {
  61. // console.debug('ipcProviderWrapper: remove listener', name);
  62. ipcRenderer.removeListener(`ipcProvider-${name}`, callback);
  63. },
  64. /**
  65. Removes all listeners
  66. @method removeAllListeners
  67. */
  68. removeAllListeners(name) {
  69. // console.debug('ipcProviderWrapper: remove all listeners', name);
  70. if (name) {
  71. ipcRenderer.removeAllListeners(`ipcProvider-${name}`);
  72. } else {
  73. ipcRenderer.removeAllListeners('ipcProvider-error');
  74. ipcRenderer.removeAllListeners('ipcProvider-end');
  75. ipcRenderer.removeAllListeners('ipcProvider-timeout');
  76. ipcRenderer.removeAllListeners('ipcProvider-connect');
  77. }
  78. },
  79. /**
  80. Write to the IPC connection through the backend
  81. @method write
  82. */
  83. write(payload) {
  84. // console.debug('ipcProviderWrapper: write payload');
  85. ipcRenderer.send('ipcProvider-write', payload);
  86. },
  87. /**
  88. Write synchronous to the IPC connection through the backend
  89. @method writeSync
  90. */
  91. writeSync(payload) {
  92. // console.debug('ipcProviderWrapper: write payload (sync)');
  93. return ipcRenderer.sendSync('ipcProvider-writeSync', payload);
  94. }
  95. };
  96. module.exports = ipcProviderWrapper;