electron-reactive-ipc
Version:
rxjs based electron IPC channel wrapper
186 lines • 6.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElectronIPC = void 0;
var tslib_1 = require("tslib");
var electron_1 = require("electron");
var channel_1 = require("./channel");
var types_1 = require("./types");
var rxjs_1 = require("rxjs");
var ElectronIPC = /** @class */ (function () {
function ElectronIPC(win) {
this.isInitialized = false;
if (this.isMain && !win)
throw new Error('Browser window is missing');
this.win = win;
this.que = {};
this._channels = {};
}
Object.defineProperty(ElectronIPC.prototype, "channels", {
get: function () {
var _a, _b;
return this._channels[(_b = (_a = this.win) === null || _a === void 0 ? void 0 : _a.webContents.id) !== null && _b !== void 0 ? _b : 0];
},
enumerable: false,
configurable: true
});
Object.defineProperty(ElectronIPC, "instance", {
get: function () {
return this._instance;
},
enumerable: false,
configurable: true
});
ElectronIPC.prototype.addChanel = function (name, data, win) {
var _a, _b;
if (!win && this.isMain)
win = this.win;
if (!win)
throw new Error('window object missing');
var channel = this.setDataOutgoing({ channel: name, data: data }, win.webContents);
if (channel) {
var id = 0;
if (channel.webContents)
id = channel.webContents.id;
if (!this.isInitialized) {
if (this.que[id])
this.que[id].push(name);
else
this.que[id] = [name];
}
var channels = this._channels[id];
if (channels)
Object.assign(channels, (_a = {}, _a[name] = channel, _a));
else
this._channels[id] = (_b = {}, _b[name] = channel, _b);
}
return channel;
};
Object.defineProperty(ElectronIPC.prototype, "isMain", {
get: function () {
return (process === null || process === void 0 ? void 0 : process.type) === types_1.ELectronProcessTypes.BROWSER;
},
enumerable: false,
configurable: true
});
ElectronIPC.prototype.resetChannels = function (id) {
var _this = this;
if (id === void 0) { id = 0; }
var channels = this._channels[id];
if (!channels)
return;
Object.entries(channels).forEach(function (_a) {
var _b;
var _c = tslib_1.__read(_a, 2), key = _c[0], val = _c[1];
if (!((_b = _this.que[id]) === null || _b === void 0 ? void 0 : _b.includes(key))) {
try {
val.close();
delete channels[key];
}
catch (error) {
console.log(error);
}
}
});
};
ElectronIPC.prototype.setDataIncoming = function (sd, webContents) {
var _a;
if (!sd)
return;
var id = 0;
if (webContents)
id = webContents.id;
else if (this.isMain) {
if (!this.win)
throw new Error('window object missing');
id = this.win.webContents.id;
}
var channels = this._channels[id];
if (!channels) {
var channel = new IPCChannel(sd.channel, sd.data, webContents, types_1.ChannelState.INCOMING);
this._channels[id] = (_a = {}, _a[sd.channel] = channel, _a);
return channel;
}
else {
var channel = channels[sd.channel];
if (!channel) {
channels[sd.channel] = new IPCChannel(sd.channel, sd.data, webContents, types_1.ChannelState.INCOMING);
return channels[sd.channel];
}
else
channel.recieve(sd.data);
}
};
ElectronIPC.prototype.setDataOutgoing = function (sd, webContents) {
if (!sd)
return;
var id = 0;
if (webContents)
id = webContents.id;
else if (this.isMain) {
if (!this.win)
throw new Error('window object missing');
id = this.win.webContents.id;
}
var channels = this._channels[id];
if (!channels)
return new IPCChannel(sd.channel, sd.data, webContents, types_1.ChannelState.OUTGOING);
else {
var channel = channels[sd.channel];
if (!channel)
return new IPCChannel(sd.channel, sd.data, webContents, types_1.ChannelState.OUTGOING);
else
channel.send(sd.data);
}
};
ElectronIPC.prototype.loadQue = function (id) {
var _this = this;
var _a;
if (id === void 0) { id = 0; }
(_a = Object.keys(this._channels[id])) === null || _a === void 0 ? void 0 : _a.forEach(function (val) {
if (_this.que[id])
_this.que[id].push(val);
else
_this.que[id] = [];
});
//console.log(this.que);
};
return ElectronIPC;
}());
exports.ElectronIPC = ElectronIPC;
var IPCChannel = /** @class */ (function (_super) {
tslib_1.__extends(IPCChannel, _super);
function IPCChannel(name, data, webContents, state) {
if (state === void 0) { state = types_1.ChannelState.OUTGOING; }
var _this = _super.call(this, data, state) || this;
_this.name = name;
if (_this.isMain)
_this.webContents = webContents;
_this.outgoing.pipe((0, rxjs_1.filter)(function (val) { return val ? true : false; })).subscribe(function (data) {
(_this.isMain ? _this.webContents : electron_1.ipcRenderer).send(types_1.ControlFlags.DATA, {
channel: _this.name,
data: data,
});
});
return _this;
}
IPCChannel.prototype.recieve = function (data) {
this.incoming.next(data);
};
Object.defineProperty(IPCChannel.prototype, "isMain", {
get: function () {
return (process === null || process === void 0 ? void 0 : process.type) === types_1.ELectronProcessTypes.BROWSER;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IPCChannel.prototype, "localValue", {
get: function () {
var _a;
return (_a = this.outgoing) === null || _a === void 0 ? void 0 : _a.value;
},
enumerable: false,
configurable: true
});
return IPCChannel;
}(channel_1.DuplexChannel));
//# sourceMappingURL=electron-ipc.js.map