electron-reactive-ipc
Version:
rxjs based electron IPC channel wrapper
84 lines • 3.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElectronIPCMain = 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 ElectronIPCMain = /** @class */ (function (_super) {
tslib_1.__extends(ElectronIPCMain, _super);
function ElectronIPCMain(win) {
var _this = _super.call(this, win) || this;
_this.initMain();
return _this;
}
ElectronIPCMain.initialize = function (win) {
if (!this._instance)
this._instance = new ElectronIPCMain(win);
this._instance.deaultWindow = win;
return this._instance;
};
ElectronIPCMain.prototype.get = function (channelName, id) {
var _a, _b, _c;
return (_c = this._channels[(_b = id !== null && id !== void 0 ? id : (_a = this.win) === null || _a === void 0 ? void 0 : _a.webContents.id) !== null && _b !== void 0 ? _b : 0]) === null || _c === void 0 ? void 0 : _c[channelName];
};
ElectronIPCMain.prototype.getChannelsForWindow = function (id) {
var _a, _b;
return this._channels[(_b = id !== null && id !== void 0 ? id : (_a = this.win) === null || _a === void 0 ? void 0 : _a.webContents.id) !== null && _b !== void 0 ? _b : 0];
};
Object.defineProperty(ElectronIPCMain.prototype, "deaultWindow", {
set: function (win) {
this.win = win;
},
enumerable: false,
configurable: true
});
ElectronIPCMain.prototype.addChanel = function (name, data, win) {
return _super.prototype.addChanel.call(this, name, data, win);
};
ElectronIPCMain.prototype.initMain = function () {
var _this = this;
console.log('init main');
electron_1.ipcMain.on(types_1.ControlFlags.INIT, function (event) {
var _a;
var sender = event.sender;
console.log("init renderer: ".concat(sender.id));
_this.resetChannels(sender.id);
_this.isInitialized = true;
sender.send(types_1.ControlFlags.INIT);
(_a = _this.que[sender.id]) === null || _a === void 0 ? void 0 : _a.forEach(function (channelName) {
var _a;
sender.send(types_1.ControlFlags.CREATE, {
channel: channelName,
data: (_a = _this._channels[sender.id][channelName]) === null || _a === void 0 ? void 0 : _a.localValue,
});
});
_this.que[sender.id] = [];
});
electron_1.ipcMain.on(types_1.ControlFlags.CREATE, function (event, data) {
_this.setDataIncoming(data, event.sender);
});
electron_1.ipcMain.on(types_1.ControlFlags.CLOSE, function (event, data) {
var channel = _this._channels[event.sender.id][data.channel];
if (channel) {
channel.close();
delete _this._channels[event.sender.id][data.channel];
}
});
electron_1.ipcMain.on(types_1.ControlFlags.DATA, function (event, data) {
//console.log(event.sender.id, data)
_this.setDataIncoming(data, event.sender);
});
electron_1.ipcMain.once(types_1.ControlFlags.QUIT, function (event) {
_this.resetChannels(event.sender.id);
_this.isInitialized = false;
_this.loadQue(event.sender.id);
});
electron_1.ipcMain.on(types_1.ControlFlags.RELOAD, function (event) {
_this.loadQue(event === null || event === void 0 ? void 0 : event.sender.id);
});
};
return ElectronIPCMain;
}(electron_ipc_1.ElectronIPC));
exports.ElectronIPCMain = ElectronIPCMain;
//# sourceMappingURL=electron-ipc-main.js.map