electron-event-flux
Version:
Redux store which synchronizes between instances in multiple process
50 lines (49 loc) • 2.56 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var constants_1 = require("./constants");
var BrowserRendererClient = /** @class */ (function () {
function BrowserRendererClient(callback, onGetAction, onGetResult, onGetMessage, onGetWinMessage) {
var clientId = window['clientId'] || 'mainClient';
this.clientId = clientId;
var mainWin = window['isMainClient'] ? window : window.opener;
mainWin.postMessage({ action: constants_1.renderRegisterName, clientId: clientId, data: {} }, '*');
window.addEventListener('message', function (event) {
var _a = event.data || {}, action = _a.action, error = _a.error, data = _a.data, senderId = _a.senderId, invokeId = _a.invokeId;
if (action === constants_1.mainInitName) {
callback(data[0], data[1]);
}
else if (action === constants_1.mainDispatchName) {
onGetAction(data);
}
else if (action === constants_1.mainReturnName) {
onGetResult(invokeId, error, data);
}
else if (action === constants_1.messageName) {
onGetMessage(data);
}
else if (action === constants_1.winMessageName) {
onGetWinMessage(senderId, data);
}
});
window.addEventListener('unload', function () {
mainWin.postMessage({ action: 'close', clientId: clientId });
});
}
// Forward update to the main process so that it can forward the update to all other renderers
BrowserRendererClient.prototype.forward = function (invokeId, action) {
var clientId = this.clientId;
var mainWin = window['isMainClient'] ? window : window.opener;
mainWin.postMessage({ action: constants_1.renderDispatchName, data: action, invokeId: invokeId, clientId: clientId }, '*');
};
BrowserRendererClient.prototype.sendMessage = function (args) {
var mainWin = window['isMainClient'] ? window : window.opener;
mainWin.postMessage({ action: constants_1.messageName, data: args }, '*');
};
BrowserRendererClient.prototype.sendWindowMessage = function (clientId, args) {
var mainWin = window['isMainClient'] ? window : window.opener;
var senderId = this.clientId;
mainWin.postMessage({ action: constants_1.winMessageName, senderId: senderId, clientId: clientId, data: args }, '*');
};
return BrowserRendererClient;
}());
exports.default = BrowserRendererClient;
;