UNPKG

electron-reactive-ipc

Version:
104 lines 4.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElectronIPCRenderer = void 0; var tslib_1 = require("tslib"); var electron_1 = require("electron"); var electron_ipc_1 = require("./electron-ipc"); var types_1 = require("./types"); var ElectronIPCRenderer = /** @class */ (function (_super) { tslib_1.__extends(ElectronIPCRenderer, _super); function ElectronIPCRenderer() { var _this = _super.call(this) || this; _this.initRenderer(); return _this; } ElectronIPCRenderer.initialize = function () { if (!this._instance) this._instance = new ElectronIPCRenderer(); return this._instance; }; ElectronIPCRenderer.prototype.get = function (channelName) { var _a; return (_a = this._channels[0]) === null || _a === void 0 ? void 0 : _a[channelName]; }; ElectronIPCRenderer.prototype.addChanel = function (name, data) { return _super.prototype.addChanel.call(this, name, data); }; Object.defineProperty(ElectronIPCRenderer.prototype, "asExposed", { /** * For exposing functions using Electron's contextBridge api */ get: function () { var _this = this; return { getChannel: function (name) { return _this.get(name); }, addChannel: function (name, initialData) { return _this.addChanel(name, initialData); } }; }, enumerable: false, configurable: true }); ElectronIPCRenderer.prototype.initRenderer = function () { var _this = this; console.log('init renderer'); electron_1.ipcRenderer.once(types_1.ControlFlags.INIT, function (event) { var _a; console.log('init main'); _this.resetChannels(); _this.isInitialized = true; (_a = _this.que[0]) === null || _a === void 0 ? void 0 : _a.forEach(function (subjectName) { var _a; electron_1.ipcRenderer.send(types_1.ControlFlags.CREATE, { channel: subjectName, data: (_a = _this._channels[0][subjectName]) === null || _a === void 0 ? void 0 : _a.localValue, }); }); _this.que[0] = []; }); electron_1.ipcRenderer.on(types_1.ControlFlags.CREATE, function (event, data) { _this.setDataIncoming(data); }); electron_1.ipcRenderer.on(types_1.ControlFlags.CLOSE, function (event, data) { var channel = _this._channels[0][data.channel]; if (channel) { channel.close(); delete _this._channels[0][data.channel]; } }); electron_1.ipcRenderer.on(types_1.ControlFlags.DATA, function (event, data) { _this.setDataIncoming(data); }); electron_1.ipcRenderer.once(types_1.ControlFlags.QUIT, function (event) { _this.resetChannels(); _this.isInitialized = false; _this.loadQue(); }); this.pingMain(); window.onbeforeunload = function () { console.log('reloading'); electron_1.ipcRenderer.send(types_1.ControlFlags.RELOAD); }; }; ElectronIPCRenderer.prototype.pingMain = function () { var _this = this; electron_1.ipcRenderer.send(types_1.ControlFlags.INIT); var i = 0; var timer; timer = setInterval(function () { if (_this.isInitialized) { clearInterval(timer); return; } if (i > 3) { clearInterval(timer); throw new Error('IPC Handshake Failed'); } console.log("No response from main, reconnecting. Attermpt: ".concat(i + 2)); electron_1.ipcRenderer.send(types_1.ControlFlags.INIT); i++; }, 3000); }; return ElectronIPCRenderer; }(electron_ipc_1.ElectronIPC)); exports.ElectronIPCRenderer = ElectronIPCRenderer; //# sourceMappingURL=electron-ipc-renderer.js.map