UNPKG

electron-event-flux

Version:

Redux store which synchronizes between instances in multiple process

277 lines (276 loc) 11 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var StoreBase_1 = require("./StoreBase"); var constants_1 = require("./constants"); function genBrowserUrl(url, clientId, parentId) { if (url === void 0) { url = ''; } var genUrl = new URL(url, location.href); if (genUrl.search) { genUrl.search += "&clientId=" + clientId; } else { genUrl.search = "?clientId=" + clientId; } genUrl.search += '&isSlave=1'; if (parentId) genUrl.search += "&parentId=" + parentId; return genUrl.toString(); } var MultiWinStore = /** @class */ (function (_super) { __extends(MultiWinStore, _super); function MultiWinStore() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.clientIds = []; // namedWinId to clientId map _this.clientIdNameMap = {}; // clientId to namedWinId map _this.clientNameIdMap = {}; _this.groupsMap = {}; _this.clientWins = {}; return _this; } MultiWinStore.prototype.init = function () { var _this = this; if (typeof window === 'object') { window.addEventListener("message", function (event) { var _a = event.data || {}, action = _a.action, clientId = _a.clientId; if (action === 'close') { _this._removeClientId(clientId); } }); window.addEventListener("beforeunload", function (event) { _this.closeAllWindows(); }); // 将main window加入到 新创建窗口列表中 this._addWinProps("mainClient", window, { name: "mainClient", groups: ["main"] }); } }; // 新增clientId对应的 winProps MultiWinStore.prototype._addWinProps = function (clientId, win, winProps) { if (!win) return; this.clientIds.push(clientId); this.clientWins[clientId] = win; if (winProps.name) { this.clientNameIdMap[winProps.name] = clientId; this.clientIdNameMap[clientId] = winProps.name; } if (winProps.groups) { this.groupsMap[clientId] = winProps.groups; } }; MultiWinStore.prototype._parseWinProps = function (winProps) { var parseProps = typeof winProps === 'string' ? { path: winProps } : winProps; // 默认窗口被分组到main分组 if (parseProps.groups === undefined) { parseProps.groups = ['main']; } return parseProps; }; // 删除窗口的clientId MultiWinStore.prototype._removeClientId = function (clientId) { var name = this.clientIdNameMap[clientId]; delete this.clientWins[clientId]; if (name) { delete this.clientNameIdMap[name]; delete this.clientIdNameMap[clientId]; } delete this.groupsMap[clientId]; var index = this.clientIds.indexOf(clientId); if (index !== -1) this.clientIds.splice(index, 1); }; MultiWinStore.prototype._createWinForBrowser = function (winProps, parentClientId, params) { var clientId = this._genClientId(); // get url from winProps winProps = this._parseWinProps(winProps); // create new browser window var win = this.createBrowserWin(genBrowserUrl(winProps.path, clientId, parentClientId), clientId, params); this._addWinProps(clientId, win, winProps); this._appStore.mainClient.addWin(clientId, win); return clientId; }; MultiWinStore.prototype._createWinForElectron = function (winProps, parentClientId, params) { return ""; }; MultiWinStore.prototype.createWin = function (winProps, parentClientId, params) { if (typeof window === 'object') { return this._createWinForBrowser(winProps, parentClientId, params); } else { return this._createWinForElectron(winProps, parentClientId, params); } }; // Create new win if the specific winId is not exists MultiWinStore.prototype.createOrOpenWin = function (winName, url, parentClientId, params) { // winName对应的窗口未存在,则创建新窗口 if (!this.clientNameIdMap[winName]) { var winProps = typeof url === 'string' ? { path: url, name: winName } : __assign({}, url, { name: winName }); return this.createWin(winProps, parentClientId, params); } else { var clientId = this.clientNameIdMap[winName]; this.changeClientAction(clientId, typeof url === 'string' ? url : url.path); this.activeWin(clientId); return clientId; } }; MultiWinStore.prototype.closeWin = function (clientId) { var win = this.clientWins[clientId]; if (win) win.close(); }; MultiWinStore.prototype.closeWinByName = function (name) { var clientId = this.clientNameIdMap[name]; if (clientId) this.closeWin(clientId); }; MultiWinStore.prototype.closeWinByGroup = function (group) { var e_1, _a; try { for (var _b = __values(this.clientIds), _c = _b.next(); !_c.done; _c = _b.next()) { var clientId = _c.value; var groups = this.groupsMap[clientId]; if (groups && groups.indexOf(group) !== -1) { this.closeWin(clientId); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } }; MultiWinStore.prototype.activeWin = function (clientId) { }; MultiWinStore.prototype.activeWindow = function (clientId) { this.activeWin(clientId); }; MultiWinStore.prototype.activeWinByName = function (name) { var clientId = this.clientNameIdMap[name]; if (clientId) this.activeWin(clientId); }; MultiWinStore.prototype.activeWinByGroup = function (group) { var e_2, _a; try { for (var _b = __values(this.clientIds), _c = _b.next(); !_c.done; _c = _b.next()) { var clientId = _c.value; var groups = this.groupsMap[clientId]; if (groups && groups.indexOf(group) !== -1) { this.activeWin(clientId); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } }; MultiWinStore.prototype.sendWinMsg = function (clientId, message) { this._appStore.mainClient.sendWinMsg(clientId, message); }; MultiWinStore.prototype.sendWinMsgByName = function (name, message) { var clientId = this.clientNameIdMap[name]; if (clientId) this.sendWinMsg(clientId, message); }; MultiWinStore.prototype.sendWinMsgByGroup = function (group, message) { var e_3, _a; try { for (var _b = __values(this.clientIds), _c = _b.next(); !_c.done; _c = _b.next()) { var clientId = _c.value; var groups = this.groupsMap[clientId]; if (groups && groups.indexOf(group) !== -1) { this.sendWinMsg(clientId, message); } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } }; MultiWinStore.prototype._genClientId = function () { var clientId = 'win' + Math.floor(Math.random() * 10000); if (this.clientIds.indexOf(clientId) !== -1) { return this._genClientId(); } return clientId; }; MultiWinStore.prototype.closeAllWindows = function () { var _this = this; this.clientIds.slice(0).forEach(function (clientId) { _this.closeWin(clientId); }); }; MultiWinStore.prototype.createBrowserWin = function (url, clientId, params) { var _this = this; if (params === void 0) { params = {}; } if (!params.width) params.width = 400; if (!params.height) params.height = 400; var featureStr = Object.keys(params).map(function (key) { return key + "=" + params[key]; }).join(','); var childWin = window.open(url, "newwindow", featureStr + ", toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no, titlebar=no"); if (childWin) childWin.onbeforeunload = function () { return _this._removeClientId(clientId); }; return childWin; }; MultiWinStore.prototype.createElectronWin = function (url, clientId, parentClientId, params) { console.error('Please provide the createElectronWin'); return; }; MultiWinStore.prototype.onChangeAction = function (clientId, action) { }; MultiWinStore.prototype.changeClientAction = function (clientId, url) { this._appStore.mainClient.changeClientAction(clientId, { url: url }); }; MultiWinStore.prototype.getWinRootStore = function (clientId) { return this.appStores[constants_1.winManagerStoreName].winPackMapStore.get(clientId); }; return MultiWinStore; }(StoreBase_1.default)); exports.default = MultiWinStore;