UNPKG

@el3um4s/ipc-for-electron

Version:

A package to simplify the communication between renderer and node js in Electron applications

54 lines (53 loc) 2.39 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); class IPC { constructor(channels) { this.nameAPI = "api"; this.validSendChannel = {}; this.validReceiveChannel = []; this.nameAPI = channels.nameAPI; this.validSendChannel = channels.validSendChannel; this.validReceiveChannel = channels.validReceiveChannel; } get channels() { return { nameAPI: this.nameAPI, validSendChannel: this.validSendChannel, validReceiveChannel: this.validReceiveChannel, }; } initIpcMain(ipcMain, customWindow) { return __awaiter(this, void 0, void 0, function* () { if (customWindow) { Object.keys(this.validSendChannel).forEach((key) => { ipcMain.on(key, (event, message) => __awaiter(this, void 0, void 0, function* () { try { if (!!customWindow && customWindow.webContents.id === event.sender.id) { yield this.validSendChannel[key](customWindow, event, message); } } catch (e) { if (e instanceof TypeError) { // console.log(e.name, e.message); } else { // console.log(e); } } })); }); } }); } } exports.default = IPC;