UNPKG

electron-event-flux

Version:

Redux store which synchronizes between instances in multiple process

111 lines (110 loc) 4.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var findIndex_1 = require("./utils/findIndex"); var constants_1 = require("./constants"); var BrowserMainClient = /** @class */ (function () { function BrowserMainClient(callbacks, log) { var _this = this; this.clients = {}; this.clientInfos = []; this.handleMessage = function (event) { var callbacks = _this.callbacks; var _a = event.data || {}, action = _a.action, data = _a.data, invokeId = _a.invokeId, senderId = _a.senderId, clientId = _a.clientId; if (action === constants_1.renderDispatchName) { // Renderer register self callbacks.handleRendererMessage(data).then(function (result) { _this.clients[clientId].postMessage({ action: constants_1.mainReturnName, invokeId: invokeId, data: result, }, '*'); }, function (err) { var errObj = { name: err.name, message: err.message }; Object.keys(err).forEach(function (key) { return errObj[key] = err[key]; }); _this.clients[clientId].postMessage({ action: constants_1.mainReturnName, invokeId: invokeId, error: errObj, }, '*'); }); } else if (action === constants_1.winMessageName) { _this.clients[clientId].postMessage({ action: constants_1.winMessageName, senderId: senderId, data: data, }, '*'); } else if (action === 'close') { // Child window has closed var index = findIndex_1.default(_this.clientInfos, function (item) { return item.clientId === clientId; }); if (index !== -1) _this.clientInfos.splice(index, 1); delete _this.clients[clientId]; callbacks.deleteWin(clientId); } else if (action === constants_1.renderRegisterName) { callbacks.addWin(clientId); _this.clientInfos.push({ clientId: clientId }); _this.clients[clientId].postMessage({ action: constants_1.mainInitName, data: [ callbacks.getInitStates(clientId), callbacks.getStores(clientId) ], }, '*'); } }; this.callbacks = callbacks; window.isMainClient = true; window.addEventListener("message", this.handleMessage); this.addWin('mainClient', window); } BrowserMainClient.prototype.addWin = function (clientId, newWin) { this.clients[clientId] = newWin; }; BrowserMainClient.prototype.getForwardClients = function () { return this.clientInfos; }; BrowserMainClient.prototype.dispatchToRenderer = function (client, payload) { var window = this.clients[client.clientId]; window && window.postMessage({ action: constants_1.mainDispatchName, data: payload }, '*'); }; // sendMessage(win, message) { // win && win.postMessage({ action: messageName, data: message }, '*'); // } BrowserMainClient.prototype.sendWinMsg = function (clientId, message) { var win = this.clients[clientId]; win && win.postMessage({ action: constants_1.messageName, data: message }, '*'); }; // closeAllWindows() { // Object.keys(this.clients).forEach(clientId => { // let window = this.clients[clientId]; // window && window.close(); // }); // } BrowserMainClient.prototype.changeClientAction = function (clientId, params) { var win = this.clients[clientId]; // this.sendMessage(win, { action: 'change-props', url }); win && win.postMessage({ action: "__INIT_WINDOW__", data: params }, '*'); }; BrowserMainClient.prototype.isRegister = function (clientId) { return !!this.clients[clientId]; }; BrowserMainClient.prototype.whenRegister = function (clientId, callback) { if (this.isRegister(clientId)) { return callback(); } var whenMessage = function (event) { var action = (event.data || {}).action; if (action === constants_1.renderDispatchName) { window.removeEventListener("message", whenMessage); callback(); } }; window.addEventListener("message", whenMessage); }; BrowserMainClient.prototype.isClose = function (clientId) { return !this.clients[clientId]; }; return BrowserMainClient; }()); exports.default = BrowserMainClient;