ipc-renderer-internal-utils.ts 769 B

123456789101112131415161718192021222324
  1. import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
  2. type IPCHandler = (event: Electron.IpcRendererEvent, ...args: any[]) => any
  3. export const handle = function <T extends IPCHandler> (channel: string, handler: T) {
  4. ipcRendererInternal.on(channel, async (event, requestId, ...args) => {
  5. const replyChannel = `${channel}_RESPONSE_${requestId}`;
  6. try {
  7. event.sender.send(replyChannel, null, await handler(event, ...args));
  8. } catch (error) {
  9. event.sender.send(replyChannel, error);
  10. }
  11. });
  12. };
  13. export function invokeSync<T> (command: string, ...args: any[]): T {
  14. const [error, result] = ipcRendererInternal.sendSync(command, ...args);
  15. if (error) {
  16. throw error;
  17. } else {
  18. return result;
  19. }
  20. }