electron-event-flux
Advanced tools
Comparing version 1.3.9 to 2.0.1
@@ -1,6 +0,6 @@ | ||
import StoreBase from 'event-flux/lib/StoreBase'; | ||
import StoreBase from './StoreBase'; | ||
export default class ActionRecordStore extends StoreBase { | ||
clientId: string; | ||
setAction(action: any): void; | ||
setAction(action: string): void; | ||
} | ||
//# sourceMappingURL=ActionRecordStore.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -17,3 +17,3 @@ extendStatics(d, b); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var StoreBase_1 = require("event-flux/lib/StoreBase"); | ||
var StoreBase_1 = require("./StoreBase"); | ||
var _a = require('./constants'), winManagerStoreName = _a.winManagerStoreName, winManagerKey = _a.winManagerKey; | ||
@@ -23,6 +23,8 @@ var ActionRecordStore = /** @class */ (function (_super) { | ||
function ActionRecordStore() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.clientId = ''; | ||
return _this; | ||
} | ||
ActionRecordStore.prototype.setAction = function (action) { | ||
this.appStores.multiWinStore.actionChanged(this.clientId, action); | ||
this.appStores.multiWinStore.onChangeAction(this.clientId, action); | ||
}; | ||
@@ -29,0 +31,0 @@ return ActionRecordStore; |
@@ -1,14 +0,21 @@ | ||
export default class ElectronMainClient { | ||
callbacks: any; | ||
clients: {}; | ||
clientInfos: any[]; | ||
constructor(callbacks: any); | ||
addWin(clientId: any, newWin: any): void; | ||
getForwardClients(): any[]; | ||
sendToRenderer(client: any, payload: any): void; | ||
sendMessage(win: any, message: any): void; | ||
sendMessageByClientId(clientId: any, message: any): void; | ||
closeAllWindows(): void; | ||
changeClientAction(clientId: any, params: any): void; | ||
import { Log } from "./utils/loggerApply"; | ||
import IMainClientCallbacks from "./IMainClientCallbacks"; | ||
import IMainClient, { IClientInfo } from './IMainClient'; | ||
export default class BrowserMainClient implements IMainClient { | ||
callbacks: IMainClientCallbacks; | ||
clients: { | ||
[clientId: string]: Window; | ||
}; | ||
clientInfos: IClientInfo[]; | ||
constructor(callbacks: IMainClientCallbacks, log: Log); | ||
private handleMessage; | ||
addWin(clientId: string, newWin: Window): void; | ||
getForwardClients(): IClientInfo[]; | ||
dispatchToRenderer(client: IClientInfo, payload: any): void; | ||
sendWinMsg(clientId: string, message: any): void; | ||
changeClientAction(clientId: string, params: any): void; | ||
isRegister(clientId: string): boolean; | ||
whenRegister(clientId: string, callback: () => void): void; | ||
isClose(clientId: string): boolean; | ||
} | ||
//# sourceMappingURL=BrowserMainClient.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var findIndex = require('lodash/findIndex'); | ||
var _a = require('./constants'), renderRegisterName = _a.renderRegisterName, renderDispatchName = _a.renderDispatchName, mainDispatchName = _a.mainDispatchName, mainInitName = _a.mainInitName, mainReturnName = _a.mainReturnName, winMessageName = _a.winMessageName, messageName = _a.messageName; | ||
var ElectronMainClient = /** @class */ (function () { | ||
function ElectronMainClient(callbacks) { | ||
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.callbacks = callbacks; | ||
window.isMainClient = true; | ||
window.addEventListener("message", function (event) { | ||
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 === renderDispatchName) { | ||
if (action === constants_1.renderDispatchName) { // Renderer register self | ||
callbacks.handleRendererMessage(data).then(function (result) { | ||
_this.clients[clientId].postMessage({ | ||
action: mainReturnName, | ||
action: constants_1.mainReturnName, | ||
invokeId: invokeId, | ||
@@ -26,3 +24,3 @@ data: result, | ||
_this.clients[clientId].postMessage({ | ||
action: mainReturnName, | ||
action: constants_1.mainReturnName, | ||
invokeId: invokeId, | ||
@@ -33,5 +31,5 @@ error: errObj, | ||
} | ||
else if (action === winMessageName) { | ||
else if (action === constants_1.winMessageName) { | ||
_this.clients[clientId].postMessage({ | ||
action: winMessageName, | ||
action: constants_1.winMessageName, | ||
senderId: senderId, | ||
@@ -42,48 +40,49 @@ data: data, | ||
else if (action === 'close') { // Child window has closed | ||
var index = findIndex(_this.clientInfos, function (item) { return item.clientId === clientId; }); | ||
var index = findIndex_1.default(_this.clientInfos, function (item) { return item.clientId === clientId; }); | ||
if (index !== -1) | ||
_this.clientInfos.splice(index, 1); | ||
_this.clients[clientId] = null; | ||
delete _this.clients[clientId]; | ||
callbacks.deleteWin(clientId); | ||
} | ||
else if (action === renderRegisterName) { | ||
var filter = data.filter; | ||
else if (action === constants_1.renderRegisterName) { | ||
callbacks.addWin(clientId); | ||
_this.clientInfos.push({ clientId: clientId, filter: filter }); | ||
_this.clientInfos.push({ clientId: clientId }); | ||
_this.clients[clientId].postMessage({ | ||
action: mainInitName, | ||
action: constants_1.mainInitName, | ||
data: [ | ||
callbacks.getInitStates(clientId, filter), | ||
callbacks.getStores(clientId, filter) | ||
callbacks.getInitStates(clientId), | ||
callbacks.getStores(clientId) | ||
], | ||
}, '*'); | ||
} | ||
}); | ||
}; | ||
this.callbacks = callbacks; | ||
window.isMainClient = true; | ||
window.addEventListener("message", this.handleMessage); | ||
this.addWin('mainClient', window); | ||
} | ||
ElectronMainClient.prototype.addWin = function (clientId, newWin) { | ||
BrowserMainClient.prototype.addWin = function (clientId, newWin) { | ||
this.clients[clientId] = newWin; | ||
}; | ||
ElectronMainClient.prototype.getForwardClients = function () { | ||
BrowserMainClient.prototype.getForwardClients = function () { | ||
return this.clientInfos; | ||
}; | ||
ElectronMainClient.prototype.sendToRenderer = function (client, payload) { | ||
BrowserMainClient.prototype.dispatchToRenderer = function (client, payload) { | ||
var window = this.clients[client.clientId]; | ||
window && window.postMessage({ action: mainDispatchName, data: payload }, '*'); | ||
window && window.postMessage({ action: constants_1.mainDispatchName, data: payload }, '*'); | ||
}; | ||
ElectronMainClient.prototype.sendMessage = function (win, message) { | ||
win && win.postMessage({ action: messageName, data: message }, '*'); | ||
}; | ||
ElectronMainClient.prototype.sendMessageByClientId = function (clientId, message) { | ||
// 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: messageName, data: message }, '*'); | ||
win && win.postMessage({ action: constants_1.messageName, data: message }, '*'); | ||
}; | ||
ElectronMainClient.prototype.closeAllWindows = function () { | ||
var _this = this; | ||
Object.keys(this.clients).forEach(function (clientId) { | ||
var window = _this.clients[clientId]; | ||
window && window.close(); | ||
}); | ||
}; | ||
ElectronMainClient.prototype.changeClientAction = function (clientId, params) { | ||
// 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]; | ||
@@ -93,4 +92,23 @@ // this.sendMessage(win, { action: 'change-props', url }); | ||
}; | ||
return ElectronMainClient; | ||
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 = ElectronMainClient; | ||
exports.default = BrowserMainClient; |
@@ -0,8 +1,9 @@ | ||
import { IStoreCallback, IActionCallback, IResultCallback, IMessageCallback, IWinMessageCallback } from './IRendererClient'; | ||
export default class BrowserRendererClient { | ||
clientId: any; | ||
constructor(filter: any, callback: any, onGetAction: any, onGetResult: any, onGetMessage: any, onGetWinMessage: any); | ||
forward(invokeId: any, action: any): void; | ||
constructor(callback: IStoreCallback, onGetAction: IActionCallback, onGetResult: IResultCallback, onGetMessage: IMessageCallback, onGetWinMessage: IWinMessageCallback); | ||
forward(invokeId: number, action: any): void; | ||
sendMessage(args: any): void; | ||
sendWindowMessage(clientId: any, args: any): void; | ||
sendWindowMessage(clientId: string, args: any): void; | ||
} | ||
//# sourceMappingURL=BrowserRendererClient.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _a = require('./constants'), renderDispatchName = _a.renderDispatchName, renderRegisterName = _a.renderRegisterName, mainDispatchName = _a.mainDispatchName, mainInitName = _a.mainInitName, mainReturnName = _a.mainReturnName, winMessageName = _a.winMessageName, messageName = _a.messageName; | ||
var constants_1 = require("./constants"); | ||
var BrowserRendererClient = /** @class */ (function () { | ||
function BrowserRendererClient(filter, callback, onGetAction, onGetResult, onGetMessage, onGetWinMessage) { | ||
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: renderRegisterName, clientId: clientId, data: { filter: filter } }, '*'); | ||
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 === mainInitName) { | ||
if (action === constants_1.mainInitName) { | ||
callback(data[0], data[1]); | ||
} | ||
else if (action === mainDispatchName) { | ||
else if (action === constants_1.mainDispatchName) { | ||
onGetAction(data); | ||
} | ||
else if (action === mainReturnName) { | ||
else if (action === constants_1.mainReturnName) { | ||
onGetResult(invokeId, error, data); | ||
} | ||
else if (action === messageName) { | ||
else if (action === constants_1.messageName) { | ||
onGetMessage(data); | ||
} | ||
else if (action === winMessageName) { | ||
else if (action === constants_1.winMessageName) { | ||
onGetWinMessage(senderId, data); | ||
@@ -36,7 +36,7 @@ } | ||
var mainWin = window['isMainClient'] ? window : window.opener; | ||
mainWin.postMessage({ action: renderDispatchName, data: action, invokeId: invokeId, clientId: clientId }, '*'); | ||
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: messageName, data: args }, '*'); | ||
mainWin.postMessage({ action: constants_1.messageName, data: args }, '*'); | ||
}; | ||
@@ -46,3 +46,3 @@ BrowserRendererClient.prototype.sendWindowMessage = function (clientId, args) { | ||
var senderId = this.clientId; | ||
mainWin.postMessage({ action: winMessageName, senderId: senderId, clientId: clientId, data: args }, '*'); | ||
mainWin.postMessage({ action: constants_1.winMessageName, senderId: senderId, clientId: clientId, data: args }, '*'); | ||
}; | ||
@@ -49,0 +49,0 @@ return BrowserRendererClient; |
@@ -0,1 +1,10 @@ | ||
export declare const renderRegisterName = "__RENDERER_REGISTER__"; | ||
export declare const renderDispatchName = "__RENDERER_DISPATCH__"; | ||
export declare const mainInitName = "__MAIN_INIT__"; | ||
export declare const mainDispatchName = "__MAIN_DISPATCH__"; | ||
export declare const mainReturnName = "__MAIN_RETURN__"; | ||
export declare const winManagerStoreName = "winManagerStore"; | ||
export declare const winManagerKey = "winManager"; | ||
export declare const messageName = "__MESSAGE__"; | ||
export declare const winMessageName = "__WIN_MESSAGE__"; | ||
//# sourceMappingURL=constants.d.ts.map |
@@ -1,11 +0,11 @@ | ||
module.exports = { | ||
renderRegisterName: 'event-flux-renderer-register', | ||
renderDispatchName: 'event-flux-renderer-dispatch', | ||
mainInitName: 'event-flux-main-init', | ||
mainDispatchName: 'event-flux-main-dispatch', | ||
mainReturnName: 'event-flux-main-return', | ||
winManagerStoreName: 'winManagerStore', | ||
winManagerKey: 'winManager', | ||
messageName: '__MESSAGE__', | ||
winMessageName: '__WIN_MESSAGE__', | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.renderRegisterName = '__RENDERER_REGISTER__'; | ||
exports.renderDispatchName = '__RENDERER_DISPATCH__'; | ||
exports.mainInitName = '__MAIN_INIT__'; | ||
exports.mainDispatchName = '__MAIN_DISPATCH__'; | ||
exports.mainReturnName = '__MAIN_RETURN__'; | ||
exports.winManagerStoreName = 'winManagerStore'; | ||
exports.winManagerKey = 'winManager'; | ||
exports.messageName = '__MESSAGE__'; | ||
exports.winMessageName = '__WIN_MESSAGE__'; |
import { Log } from "./utils/loggerApply"; | ||
export default class ElectronMainClient { | ||
unregisterRenderer: any; | ||
clientInfos: any; | ||
clientMap: any; | ||
import IMainClientCallbacks from "./IMainClientCallbacks"; | ||
import { WebContents, BrowserWindow, Event } from 'electron'; | ||
import IMainClient from './IMainClient'; | ||
interface IClientInfo { | ||
webContents: WebContents; | ||
window: BrowserWindow; | ||
clientId: string; | ||
} | ||
export default class ElectronMainClient implements IMainClient { | ||
clientInfos: IClientInfo[]; | ||
clientMap: { | ||
[key: string]: IClientInfo; | ||
}; | ||
log: Log; | ||
constructor(callbacks: any, log: Log); | ||
getForwardClients(): any; | ||
checkWebContents(webContents: any): boolean; | ||
sendToRenderer(client: any, payload: any): void; | ||
sendMessage(win: any, message: any): void; | ||
sendMessageByClientId(clientId: any, message: any): void; | ||
closeAllWindows(): void; | ||
getWindowByClientId(clientId: any): any; | ||
changeClientAction(clientId: any, params: any): void; | ||
mainClientCallbacks: IMainClientCallbacks; | ||
constructor(callbacks: IMainClientCallbacks, log: Log); | ||
private handleUnregisterRenderer; | ||
private handleRegister; | ||
private handleRendererDispatch; | ||
handleWinMessage: (event: Event, clientId: string, data: any) => void; | ||
private checkWebContents; | ||
private _sendForWebContents; | ||
getForwardClients(): IClientInfo[]; | ||
dispatchToRenderer(client: IClientInfo, payload: any): void; | ||
sendWinMsg(clientId: string, message: any): void; | ||
getWindow(clientId: string): BrowserWindow; | ||
getWebContents(clientId: string): WebContents; | ||
changeClientAction(clientId: string, params: any): void; | ||
isRegister(clientId: string): boolean; | ||
whenRegister(clientId: string, callback: () => void): void; | ||
isClose(clientId: string): boolean; | ||
} | ||
export {}; | ||
//# sourceMappingURL=ElectronMainClient.d.ts.map |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _a = require('./constants'), mainInitName = _a.mainInitName, mainDispatchName = _a.mainDispatchName, mainReturnName = _a.mainReturnName, renderDispatchName = _a.renderDispatchName, renderRegisterName = _a.renderRegisterName, messageName = _a.messageName, winMessageName = _a.winMessageName; | ||
var ipcMain = require('electron').ipcMain; | ||
var findIndex = require('lodash/findIndex'); | ||
var constants_1 = require("./constants"); | ||
var electron_1 = require("electron"); | ||
var findIndex_1 = require("./utils/findIndex"); | ||
; | ||
var ElectronMainClient = /** @class */ (function () { | ||
function ElectronMainClient(callbacks, log) { | ||
var _this = this; | ||
this.log = log; | ||
var clientInfos = []; // webContentsId -> {webContents, filter, clientId, windowId, active} | ||
var clientMap = {}; | ||
// Need to keep track of windows, as when a window refreshes it creates a new | ||
// webContents, and the old one must be unregistered | ||
// Cannot delete data, as events could still be sent after close | ||
// events when a BrowserWindow is created using remote | ||
var unregisterRenderer = function (clientId) { | ||
var existIndex = findIndex(clientInfos, function (item) { return item.clientId === clientId; }); | ||
if (existIndex !== -1) { | ||
clientInfos.splice(existIndex, 1); | ||
clientMap[clientId] = null; | ||
callbacks.deleteWin(clientId); | ||
} | ||
}; | ||
this.unregisterRenderer = unregisterRenderer; | ||
ipcMain.on(renderRegisterName, function (_a, _b) { | ||
this.clientInfos = []; | ||
this.clientMap = {}; | ||
// Renderer process register self, Then the main process will send the store the initial state to the renderer process | ||
this.handleRegister = function (_a, _b) { | ||
var sender = _a.sender; | ||
var filter = _b.filter, clientId = _b.clientId; | ||
var existIndex = findIndex(clientInfos, function (item) { return item.clientId === clientId; }); | ||
var clientId = _b.clientId; | ||
var existIndex = findIndex_1.default(_this.clientInfos, function (item) { return item.clientId === clientId; }); | ||
if (existIndex !== -1) { | ||
unregisterRenderer(clientId); | ||
_this.handleUnregisterRenderer(clientId); | ||
} | ||
var clientInfo = { | ||
webContents: sender, | ||
filter: filter, | ||
clientId: clientId, | ||
window: sender.getOwnerBrowserWindow(), | ||
window: electron_1.BrowserWindow.fromWebContents(sender), | ||
}; | ||
clientInfos.push(clientInfo); | ||
clientMap[clientId] = clientInfo; | ||
_this.clientInfos.push(clientInfo); | ||
_this.clientMap[clientId] = clientInfo; | ||
// Add window first, then get window info, The window info should has prepared | ||
callbacks.addWin(clientId); | ||
if (!sender.isGuest()) { // For windowMap (not webviews) | ||
var browserWindow = sender.getOwnerBrowserWindow(); | ||
// Webcontents aren't automatically destroyed on window close | ||
browserWindow.on('closed', function () { return unregisterRenderer(clientId); }); | ||
} | ||
sender.send(mainInitName, callbacks.getStores(clientId), callbacks.getInitStates(clientId)); | ||
}); | ||
// Give renderers a way to sync the current state of the store, but be sure we don't | ||
// expose any remote objects. In other words, we need to rely exclusively on primitive | ||
// data types, Arrays, or Buffers. Refer to: | ||
// https://github.com/electron/electron/blob/master/docs/api/remote.md#remote-objects | ||
// global[mainInitName + 'Stores'] = function(clientId) { | ||
// return callbacks.getStores(clientId, clientMap[clientId].filter); | ||
// } | ||
// global[mainInitName] = (clientId) => { | ||
// return callbacks.getInitStates(clientId, clientMap[clientId].filter); | ||
// } | ||
ipcMain.on(renderDispatchName, function (event, clientId, invokeId, stringifiedAction) { | ||
if (!clientMap[clientId]) | ||
_this.mainClientCallbacks.addWin(clientId); | ||
var browserWindow = electron_1.BrowserWindow.fromWebContents(sender); | ||
// Webcontents aren't automatically destroyed on window close | ||
browserWindow.on('closed', function () { return _this.handleUnregisterRenderer(clientId); }); | ||
_this._sendForWebContents(sender, constants_1.mainInitName, _this.mainClientCallbacks.getStores(clientId), _this.mainClientCallbacks.getInitStates(clientId)); | ||
}; | ||
// When renderer process dispatch an action to main process, the handleRendererDispatch will invoke | ||
// The main process will invoke handleRendererMessage to handle the message and send the result back to renderer process | ||
this.handleRendererDispatch = function (event, clientId, invokeId, stringifiedAction) { | ||
if (!_this.clientMap[clientId]) | ||
return; | ||
var webContents = clientMap[clientId].webContents; | ||
callbacks.handleRendererMessage(stringifiedAction).then(function (result) { | ||
if (_this.checkWebContents(webContents)) { | ||
webContents.send(mainReturnName, invokeId, undefined, result); | ||
} | ||
var webContents = _this.clientMap[clientId].webContents; | ||
_this.mainClientCallbacks.handleRendererMessage(stringifiedAction).then(function (result) { | ||
_this._sendForWebContents(webContents, constants_1.mainReturnName, invokeId, undefined, result); | ||
}, function (err) { | ||
@@ -71,28 +66,54 @@ var errObj = null; | ||
errObj = { name: err.name, message: err.message }; | ||
Object.keys(err).forEach(function (key) { return errObj[key] = err[key]; }); | ||
if (errObj) { | ||
Object.keys(err).forEach(function (key) { return errObj[key] = err[key]; }); | ||
} | ||
} | ||
if (_this.checkWebContents(webContents)) { | ||
webContents.send(mainReturnName, invokeId, errObj, undefined); | ||
} | ||
_this._sendForWebContents(webContents, constants_1.mainReturnName, invokeId, errObj, undefined); | ||
}); | ||
}); | ||
ipcMain.on(winMessageName, function (event, clientId, data) { | ||
if (!clientMap[clientId]) | ||
}; | ||
this.handleWinMessage = function (event, clientId, data) { | ||
if (!_this.clientMap[clientId]) | ||
return; | ||
var webContents = clientMap[clientId].webContents; | ||
var existIndex = findIndex(clientInfos, function (item) { return item.webContents === event.sender; }); | ||
var webContents = _this.clientMap[clientId].webContents; | ||
var existIndex = findIndex_1.default(_this.clientInfos, function (item) { return item.webContents === event.sender; }); | ||
if (existIndex !== -1) { | ||
webContents.send(winMessageName, clientInfos[existIndex].clientId, data); | ||
_this._sendForWebContents(webContents, constants_1.winMessageName, _this.clientInfos[existIndex].clientId, data); | ||
} | ||
}); | ||
this.clientInfos = clientInfos; | ||
this.clientMap = clientMap; | ||
}; | ||
this.log = log; | ||
this.mainClientCallbacks = callbacks; | ||
// Need to keep track of windows, as when a window refreshes it creates a new | ||
// webContents, and the old one must be unregistered | ||
// Cannot delete data, as events could still be sent after close | ||
// events when a BrowserWindow is created using remote | ||
electron_1.ipcMain.once(constants_1.renderRegisterName, this.handleRegister); | ||
electron_1.ipcMain.on(constants_1.renderDispatchName, this.handleRendererDispatch); | ||
electron_1.ipcMain.on(constants_1.winMessageName, this.handleWinMessage); | ||
return this; | ||
} | ||
ElectronMainClient.prototype.getForwardClients = function () { | ||
return this.clientInfos; | ||
ElectronMainClient.prototype.handleUnregisterRenderer = function (clientId) { | ||
var existIndex = findIndex_1.default(this.clientInfos, function (item) { return item.clientId === clientId; }); | ||
if (existIndex !== -1) { | ||
this.clientInfos.splice(existIndex, 1); | ||
delete this.clientMap[clientId]; | ||
this.mainClientCallbacks.deleteWin(clientId); | ||
} | ||
}; | ||
; | ||
ElectronMainClient.prototype.checkWebContents = function (webContents) { | ||
return !webContents.isDestroyed() && !webContents.isCrashed(); | ||
}; | ||
ElectronMainClient.prototype.sendToRenderer = function (client, payload) { | ||
ElectronMainClient.prototype._sendForWebContents = function (webContents, channel) { | ||
var args = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
args[_i - 2] = arguments[_i]; | ||
} | ||
if (this.checkWebContents(webContents)) { | ||
webContents.send.apply(webContents, __spread([channel], args)); | ||
} | ||
}; | ||
ElectronMainClient.prototype.getForwardClients = function () { | ||
return this.clientInfos; | ||
}; | ||
ElectronMainClient.prototype.dispatchToRenderer = function (client, payload) { | ||
var webContents = client.webContents; | ||
@@ -103,32 +124,26 @@ // if (webContents.isDestroyed() || webContents.isCrashed()) { | ||
if (this.checkWebContents(webContents)) { | ||
webContents.send(mainDispatchName, payload); | ||
webContents.send(constants_1.mainDispatchName, payload); | ||
} | ||
}; | ||
ElectronMainClient.prototype.sendMessage = function (win, message) { | ||
if (this.checkWebContents(win.webContents)) { | ||
win.webContents.send(messageName, message); | ||
} | ||
}; | ||
ElectronMainClient.prototype.sendMessageByClientId = function (clientId, message) { | ||
ElectronMainClient.prototype.sendWinMsg = function (clientId, message) { | ||
var webContents = this.clientMap[clientId].webContents; | ||
if (this.checkWebContents(webContents)) { | ||
webContents.send(messageName, message); | ||
webContents.send(constants_1.messageName, message); | ||
} | ||
}; | ||
ElectronMainClient.prototype.closeAllWindows = function () { | ||
this.clientInfos.slice().forEach(function (client) { | ||
if (!client.window.isDestroyed()) { | ||
client.window.close(); | ||
} | ||
}); | ||
}; | ||
ElectronMainClient.prototype.getWindowByClientId = function (clientId) { | ||
// 通过clientId获取BrowserWindow | ||
ElectronMainClient.prototype.getWindow = function (clientId) { | ||
return this.clientMap[clientId].window; | ||
}; | ||
// 通过clientId获取WebContents | ||
ElectronMainClient.prototype.getWebContents = function (clientId) { | ||
return this.clientMap[clientId].webContents; | ||
}; | ||
ElectronMainClient.prototype.changeClientAction = function (clientId, params) { | ||
var _this = this; | ||
if (this.clientMap[clientId]) { | ||
var win = this.clientMap[clientId].window; | ||
var webContents = this.clientMap[clientId].webContents; | ||
// this.sendMessage(win, { action: 'change-props', url }); | ||
win.webContents.send("__INIT_WINDOW__", params); | ||
// win.webContents.send("__INIT_WINDOW__", params); | ||
this._sendForWebContents(webContents, "__INIT_WINDOW__", params); | ||
this.log(function (logger) { return logger("ElectronMainClient", "init Window", params); }); | ||
@@ -138,5 +153,4 @@ } | ||
// 还没有初始化,则监听注册事件,当初始化之后 开始初始化 | ||
ipcMain.on(renderRegisterName, function (_a, _b) { | ||
var sender = _a.sender; | ||
var filter = _b.filter, nowClientId = _b.clientId; | ||
electron_1.ipcMain.once(constants_1.renderRegisterName, function (event, _a) { | ||
var nowClientId = _a.clientId; | ||
if (nowClientId === clientId) { | ||
@@ -148,4 +162,21 @@ _this.changeClientAction(clientId, params); | ||
}; | ||
ElectronMainClient.prototype.isRegister = function (clientId) { | ||
return !!this.clientMap[clientId]; | ||
}; | ||
ElectronMainClient.prototype.whenRegister = function (clientId, callback) { | ||
if (this.isRegister(clientId)) { | ||
return callback(); | ||
} | ||
electron_1.ipcMain.once(constants_1.renderRegisterName, function (event, _a) { | ||
var nowClientId = _a.clientId; | ||
if (nowClientId === clientId) { | ||
callback(); | ||
} | ||
}); | ||
}; | ||
ElectronMainClient.prototype.isClose = function (clientId) { | ||
return !this.clientMap[clientId]; | ||
}; | ||
return ElectronMainClient; | ||
}()); | ||
exports.default = ElectronMainClient; |
@@ -0,8 +1,9 @@ | ||
import { IStoreCallback, IActionCallback, IResultCallback, IMessageCallback, IWinMessageCallback, IInitWindowCallback } from './IRendererClient'; | ||
export default class ElectronRendererClient { | ||
clientId: any; | ||
constructor(filter: any, callback: any, onGetAction: any, onGetResult: any, onGetMessage: any, onGetWinMessage: any, onInitWindow: any); | ||
forward(invokeId: any, action: any): void; | ||
constructor(callback: IStoreCallback, onGetAction: IActionCallback, onGetResult: IResultCallback, onGetMessage: IMessageCallback, onGetWinMessage: IWinMessageCallback, onInitWindow: IInitWindowCallback); | ||
forward(invokeId: string, action: any): void; | ||
sendMessage(args: any): void; | ||
sendWindowMessage(clientId: any, args: any): void; | ||
sendWindowMessage(clientId: string, args: any): void; | ||
} | ||
//# sourceMappingURL=ElectronRendererClient.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _a = require('./constants'), mainInitName = _a.mainInitName, renderRegisterName = _a.renderRegisterName, renderDispatchName = _a.renderDispatchName, mainDispatchName = _a.mainDispatchName, mainReturnName = _a.mainReturnName, winMessageName = _a.winMessageName, messageName = _a.messageName; | ||
var _b = require('electron'), ipcRenderer = _b.ipcRenderer, remote = _b.remote; | ||
var constants_1 = require("./constants"); | ||
var electron_1 = require("electron"); | ||
var ElectronRendererClient = /** @class */ (function () { | ||
function ElectronRendererClient(filter, callback, onGetAction, onGetResult, onGetMessage, onGetWinMessage, onInitWindow) { | ||
function ElectronRendererClient(callback, onGetAction, onGetResult, onGetMessage, onGetWinMessage, onInitWindow) { | ||
var clientId = window.clientId; | ||
if (!clientId) { | ||
var rendererId = process.guestInstanceId || remote.getCurrentWindow().id; | ||
var rendererId = process.guestInstanceId || electron_1.remote.getCurrentWindow().id; | ||
clientId = process.guestInstanceId ? "webview " + rendererId : "window " + rendererId; | ||
@@ -14,20 +14,20 @@ } | ||
// Allows the main process to forward updates to this renderer automatically | ||
ipcRenderer.send(renderRegisterName, { filter: filter, clientId: clientId }); | ||
electron_1.ipcRenderer.send(constants_1.renderRegisterName, { clientId: clientId }); | ||
// Dispatches from other processes are forwarded using this ipc message | ||
ipcRenderer.on(mainInitName, function (event, storeFilters, stateData) { | ||
electron_1.ipcRenderer.on(constants_1.mainInitName, function (event, storeFilters, stateData) { | ||
callback(stateData, storeFilters); | ||
}); | ||
ipcRenderer.on(mainDispatchName, function (event, stringifiedAction) { | ||
electron_1.ipcRenderer.on(constants_1.mainDispatchName, function (event, stringifiedAction) { | ||
onGetAction(stringifiedAction); | ||
}); | ||
ipcRenderer.on(mainReturnName, function (event, invokeId, error, result) { | ||
electron_1.ipcRenderer.on(constants_1.mainReturnName, function (event, invokeId, error, result) { | ||
onGetResult(invokeId, error, result); | ||
}); | ||
ipcRenderer.on(messageName, function (event, params) { | ||
electron_1.ipcRenderer.on(constants_1.messageName, function (event, params) { | ||
onGetMessage(params); | ||
}); | ||
ipcRenderer.on(winMessageName, function (event, senderId, params) { | ||
electron_1.ipcRenderer.on(constants_1.winMessageName, function (event, senderId, params) { | ||
onGetWinMessage(senderId, params); | ||
}); | ||
ipcRenderer.on("__INIT_WINDOW__", function (event, params) { | ||
electron_1.ipcRenderer.on("__INIT_WINDOW__", function (event, params) { | ||
onInitWindow(params); | ||
@@ -38,9 +38,9 @@ }); | ||
ElectronRendererClient.prototype.forward = function (invokeId, action) { | ||
ipcRenderer.send(renderDispatchName, this.clientId, invokeId, action); | ||
electron_1.ipcRenderer.send(constants_1.renderDispatchName, this.clientId, invokeId, action); | ||
}; | ||
ElectronRendererClient.prototype.sendMessage = function (args) { | ||
ipcRenderer.send(messageName, args); | ||
electron_1.ipcRenderer.send(constants_1.messageName, args); | ||
}; | ||
ElectronRendererClient.prototype.sendWindowMessage = function (clientId, args) { | ||
ipcRenderer.send(winMessageName, clientId, args); | ||
electron_1.ipcRenderer.send(constants_1.winMessageName, clientId, args); | ||
}; | ||
@@ -47,0 +47,0 @@ return ElectronRendererClient; |
@@ -0,10 +1,27 @@ | ||
import { BrowserWindow, Rectangle } from "electron"; | ||
declare type OnSave = (state: any) => void; | ||
export interface IWinState { | ||
x?: number; | ||
y?: number; | ||
width?: number; | ||
height?: number; | ||
useContentSize?: boolean; | ||
displayBounds?: Rectangle; | ||
isMaximized?: boolean; | ||
isFullScreen?: boolean; | ||
} | ||
interface IWinConfig { | ||
defaultWidth?: number; | ||
defaultHeight?: number; | ||
useContentSize?: boolean; | ||
} | ||
export default class ElectronWindowState { | ||
onSave: any; | ||
state: any; | ||
onSave: OnSave | null; | ||
state: IWinState; | ||
winRef: any; | ||
stateChangeTimer: any; | ||
constructor(config: any, state: any, onSave: any); | ||
loadState(state: any, config: any): void; | ||
normState(state: any): any; | ||
isNormal(win: any): boolean; | ||
constructor(config: any, state: any, onSave: OnSave | null); | ||
loadState(state: IWinState, config: IWinConfig): void; | ||
normState(state: IWinState): IWinState; | ||
isNormal(win: BrowserWindow): boolean; | ||
hasBounds(): boolean; | ||
@@ -16,5 +33,6 @@ validateState(): void; | ||
closedHandler(): void; | ||
manage(win: any): void; | ||
manage(win: BrowserWindow): void; | ||
unmanage(): void; | ||
} | ||
export {}; | ||
//# sourceMappingURL=ElectronWindowState.d.ts.map |
@@ -12,2 +12,3 @@ 'use strict'; | ||
function ElectronWindowState(config, state, onSave) { | ||
this.state = {}; | ||
this.onSave = onSave; | ||
@@ -120,3 +121,3 @@ this.stateChangeHandler = this.stateChangeHandler.bind(this); | ||
this.updateState(); | ||
this.onSave(this.state); | ||
this.onSave && this.onSave(this.state); | ||
}; | ||
@@ -123,0 +124,0 @@ ElectronWindowState.prototype.manage = function (win) { |
@@ -0,26 +1,64 @@ | ||
import AppStore from './AppStore'; | ||
import MultiWinManagerStore from './MultiWinManagerStore'; | ||
import ActionRecordStore from './ActionRecordStore'; | ||
import MultiWinStore from './MultiWinStore'; | ||
export default function buildMultiWinAppStore(stores: any, winStores: any, { WindowsManagerStore, ActionStore, WinHandleStore, }: { | ||
import { Logger } from './utils/loggerApply'; | ||
import { IStoresObjDeclarer } from './IStoresDeclarer'; | ||
import IExtendStoreBase, { IExtendStoreBaseConstructor } from './IExtendStoreBase'; | ||
export default function buildMultiWinAppStore(stores: IStoresObjDeclarer, winStores: IStoresObjDeclarer, { WindowsManagerStore, ActionStore, WinHandleStore, }: { | ||
WindowsManagerStore?: typeof MultiWinManagerStore; | ||
ActionStore?: typeof ActionRecordStore; | ||
WinHandleStore?: typeof MultiWinStore; | ||
}, logger: any): { | ||
[x: string]: any; | ||
_stateListeners: {}; | ||
_stateFilters: {}; | ||
}, logger: Logger): { | ||
_stateListeners: { | ||
[key: string]: any; | ||
}; | ||
_stateFilters: { | ||
[key: string]: any; | ||
}; | ||
_stateFiltersInit: boolean; | ||
appStores: any; | ||
getDefaultFilter(): { | ||
'*': boolean; | ||
[key: string]: any; | ||
}; | ||
_initForClientId: (clientId: any) => void; | ||
_initForClientId: (clientId: string) => void; | ||
_initStateFilters(): void; | ||
_initWrap(): void; | ||
_handleAddWin(clientId: any): void; | ||
_handleRemoveWin(clientId: any): void; | ||
_setFilter(clientId: any, newFilter: any): void; | ||
listen: (clientId: string) => void; | ||
unlisten: (clientId: string) => void; | ||
_handleAddWin(clientId: string): void; | ||
_handleRemoveWin(clientId: string): void; | ||
_setFilter(clientId: string, newFilter: any): void; | ||
listen: (this: any, clientId: string) => void; | ||
unlisten: (this: any, clientId: string) => void; | ||
buildStores(): void; | ||
initStores(parentStore?: IExtendStoreBase): void; | ||
startObserve(): void; | ||
disposeStores(): void; | ||
getSubStores(): IExtendStoreBase[]; | ||
getSubStoreInfos(): ["Item" | "List" | "Map", IExtendStoreBaseConstructor, string, string, any][]; | ||
setStore?(storeKey: string, store: IExtendStoreBase | import("./utils/StoreList").default | import("./utils/StoreMap").default): void; | ||
getStore?(storeKey: string): IExtendStoreBase | import("./utils/StoreList").default | import("./utils/StoreMap").default; | ||
getStores?(): IExtendStoreBase[]; | ||
handleFilterChange?(): void; | ||
_appStore: AppStore; | ||
options: any; | ||
parentStore: import("./StoreBase").default; | ||
state: any; | ||
emitter: import("event-kit").Emitter<{ | ||
[key: string]: any; | ||
}, {}>; | ||
inWillUpdate: boolean; | ||
willUpdateStates: any[]; | ||
_isInit: boolean; | ||
__FLUX_STORE__: boolean; | ||
willInit(): void; | ||
init(): void; | ||
buildStore(storeClass: IExtendStoreBaseConstructor, args: any[], options?: any): IExtendStoreBase; | ||
setState(state: any): void; | ||
onDidUpdate(callback: (state: any) => void): import("event-kit").Disposable; | ||
onWillUpdate(callback: (state: any) => void): import("event-kit").Disposable; | ||
observe(callback: (state: any) => void): import("event-kit").Disposable; | ||
dispose(): void; | ||
destroy(): void; | ||
getState(): any; | ||
}; | ||
//# sourceMappingURL=MainAppStore.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -44,3 +44,3 @@ extendStatics(d, b); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var AppStore_1 = require("event-flux/lib/AppStore"); | ||
var AppStore_1 = require("./AppStore"); | ||
var objectDifference_1 = require("./utils/objectDifference"); | ||
@@ -58,6 +58,5 @@ var filterDifference_1 = require("./utils/filterDifference"); | ||
var loggerApply_1 = require("./utils/loggerApply"); | ||
var isEmpty = require('lodash/isEmpty'); | ||
var isObject = require('lodash/isObject'); | ||
var _a = require('./constants'), winManagerStoreName = _a.winManagerStoreName, winManagerKey = _a.winManagerKey; | ||
var _b = require('json-immutable-bn'), serialize = _b.serialize, deserialize = _b.deserialize; | ||
var objUtils_1 = require("./utils/objUtils"); | ||
var constants_1 = require("./constants"); | ||
var json_immutable_bn_1 = require("json-immutable-bn"); | ||
function findStore(stores, storePath) { | ||
@@ -69,5 +68,5 @@ if (!storePath) | ||
return undefined; | ||
if (!isObject(entry)) | ||
if (!objUtils_1.isObject(entry)) | ||
return subStores[entry]; | ||
var name = entry.name, type = entry.type, index = entry.index; | ||
var _a = entry, name = _a.name, type = _a.type, index = _a.index; | ||
var storeCol = subStores[name]; | ||
@@ -82,12 +81,12 @@ if (type === 'List' || type === 'Map') { | ||
addWin: function (clientId) { | ||
stores[winManagerStoreName].addWin(clientId); | ||
stores[constants_1.winManagerStoreName].addWin(clientId); | ||
}, | ||
deleteWin: function (clientId) { | ||
stores[winManagerStoreName].deleteWin(clientId); | ||
stores[constants_1.winManagerStoreName].deleteWin(clientId); | ||
}, | ||
getStores: function (clientId, filter) { | ||
var stores = filterWindowStore_1.filterWindowStore(storeShape, winManagerStoreName, clientId); | ||
getStores: function (clientId) { | ||
var stores = filterWindowStore_1.filterWindowStore(storeShape, constants_1.winManagerStoreName, clientId); | ||
return JSON.stringify(stores); | ||
}, | ||
getInitStates: function (clientId, filter) { | ||
getInitStates: function (clientId) { | ||
if (process.env.NODE_ENV === 'development') { | ||
@@ -105,8 +104,8 @@ if (!appStore._prevStateFilters || !appStore._prevStateFilters[clientId]) { | ||
var updateState = filterApply_1.default(appStore.prevState, appStore._prevStateFilters[clientId], null); | ||
var filterState = filterWindowStore_1.filterWindowState(updateState, winManagerKey, clientId); | ||
return serialize(filterState); | ||
var filterState = filterWindowStore_1.filterWindowState(updateState, constants_1.winManagerKey, clientId); | ||
return json_immutable_bn_1.serialize(filterState); | ||
}, | ||
handleRendererMessage: function (payload) { | ||
try { | ||
var _a = deserialize(payload), storePath = _a.store, method = _a.method, args = _a.args; | ||
var _a = json_immutable_bn_1.deserialize(payload), storePath = _a.store, method = _a.method, args = _a.args; | ||
var store = findStore(stores, storePath); | ||
@@ -139,10 +138,10 @@ if (!store) { | ||
var updateState = filterApply_1.default(state, updated, deleted); | ||
updateState = filterWindowStore_1.filterWindowState(updateState, winManagerKey, clientId); | ||
if (isEmpty(updateState)) | ||
updateState = filterWindowStore_1.filterWindowState(updateState, constants_1.winManagerKey, clientId); | ||
if (objUtils_1.isEmpty(updateState)) | ||
return; | ||
mainClient.sendToRenderer(client, serialize({ payload: { updated: updateState } })); | ||
mainClient.dispatchToRenderer(client, json_immutable_bn_1.serialize({ payload: { updated: updateState } })); | ||
} | ||
}); | ||
var delta = objectDifference_1.default(prevState, state); | ||
if (isEmpty(delta.updated) && isEmpty(delta.deleted)) | ||
if (objUtils_1.isEmpty(delta.updated) && objUtils_1.isEmpty(delta.deleted)) | ||
return; | ||
@@ -153,8 +152,8 @@ clientInfo.forEach(function (client) { | ||
var filterDeleted = filterApply_1.default(delta.deleted, filters[clientId], null); | ||
var _a = __read(filterWindowStore_1.filterWindowDelta(filterUpdated, filterDeleted, winManagerKey, clientId), 2), updated = _a[0], deleted = _a[1]; | ||
if (isEmpty(updated) && isEmpty(deleted)) { | ||
var _a = __read(filterWindowStore_1.filterWindowDelta(filterUpdated, filterDeleted, constants_1.winManagerKey, clientId), 2), updated = _a[0], deleted = _a[1]; | ||
if (objUtils_1.isEmpty(updated) && objUtils_1.isEmpty(deleted)) { | ||
return; | ||
} | ||
var action = { payload: { updated: updated, deleted: deleted } }; | ||
mainClient.sendToRenderer(client, serialize(action)); | ||
mainClient.dispatchToRenderer(client, json_immutable_bn_1.serialize(action)); | ||
}); | ||
@@ -214,3 +213,3 @@ }; | ||
MultiWindowAppStore.prototype.getWinSpecificStore = function (clientId, storeName) { | ||
var winStores = this.stores[winManagerStoreName].winPackMap[clientId]; | ||
var winStores = this.stores[constants_1.winManagerStoreName].winPackMap[clientId]; | ||
if (winStores) | ||
@@ -231,7 +230,7 @@ return winStores[storeName]; | ||
function buildMultiWinAppStore(stores, winStores, _a, logger) { | ||
var _b = _a.WindowsManagerStore, WindowsManagerStore = _b === void 0 ? MultiWinManagerStore_1.default : _b, _c = _a.ActionStore, ActionStore = _c === void 0 ? ActionRecordStore_1.default : _c, _d = _a.WinHandleStore, WinHandleStore = _d === void 0 ? MultiWinStore_1.default : _d; | ||
var _e; | ||
var _b; | ||
var _c = _a.WindowsManagerStore, WindowsManagerStore = _c === void 0 ? MultiWinManagerStore_1.default : _c, _d = _a.ActionStore, ActionStore = _d === void 0 ? ActionRecordStore_1.default : _d, _e = _a.WinHandleStore, WinHandleStore = _e === void 0 ? MultiWinStore_1.default : _e; | ||
MultiWinManagerStore_1.WinPackStore.innerStores = __assign({}, winStores, { actionRecord: ActionStore }); | ||
var allStores = __assign({}, stores, (_e = { multiWin: WinHandleStore }, _e[winManagerKey] = StoreDeclarer_1.declareStore(WindowsManagerStore, { storeKey: winManagerStoreName }), _e)); | ||
var MultiWinAppStore = stateFilterDecorator_1.addStateFilter(MultiWindowAppStore); | ||
var allStores = __assign({}, stores, (_b = { multiWin: WinHandleStore }, _b[constants_1.winManagerKey] = StoreDeclarer_1.declareStore(WindowsManagerStore, { storeKey: constants_1.winManagerStoreName }), _b)); | ||
var MultiWinAppStore = stateFilterDecorator_1.default(MultiWindowAppStore); | ||
MultiWinAppStore.innerStores = allStores; | ||
@@ -238,0 +237,0 @@ var storeShape = filterStore_1.filterOneStore(MultiWinAppStore, { applyFilter: true }); |
@@ -0,24 +1,58 @@ | ||
import StoreBase from './StoreBase'; | ||
declare const _default: { | ||
new (...args: any[]): { | ||
[x: string]: any; | ||
_stateListeners: {}; | ||
_stateFilters: {}; | ||
_stateListeners: { | ||
[key: string]: any; | ||
}; | ||
_stateFilters: { | ||
[key: string]: any; | ||
}; | ||
_stateFiltersInit: boolean; | ||
appStores: any; | ||
getDefaultFilter(): { | ||
'*': boolean; | ||
[key: string]: any; | ||
}; | ||
_initForClientId: (clientId: any) => void; | ||
_initForClientId: (clientId: string) => void; | ||
_initStateFilters(): void; | ||
_initWrap(): void; | ||
_handleAddWin(clientId: any): void; | ||
_handleRemoveWin(clientId: any): void; | ||
_setFilter(clientId: any, newFilter: any): void; | ||
listen: (clientId: string) => void; | ||
unlisten: (clientId: string) => void; | ||
_handleAddWin(clientId: string): void; | ||
_handleRemoveWin(clientId: string): void; | ||
_setFilter(clientId: string, newFilter: any): void; | ||
listen: (this: any, clientId: string) => void; | ||
unlisten: (this: any, clientId: string) => void; | ||
buildStores(): void; | ||
initStores(parentStore?: import("./IExtendStoreBase").default): void; | ||
startObserve(): void; | ||
disposeStores(): void; | ||
getSubStores(): import("./IExtendStoreBase").default[]; | ||
getSubStoreInfos(): ["Item" | "List" | "Map", import("./IExtendStoreBase").IExtendStoreBaseConstructor, string, string, any][]; | ||
setStore?(storeKey: string, store: import("./IExtendStoreBase").default | import("./utils/StoreList").default | import("./utils/StoreMap").default): void; | ||
getStore?(storeKey: string): import("./IExtendStoreBase").default | import("./utils/StoreList").default | import("./utils/StoreMap").default; | ||
getStores?(): import("./IExtendStoreBase").default[]; | ||
handleFilterChange?(): void; | ||
_appStore: import("./AppStore").default; | ||
options: any; | ||
parentStore: StoreBase; | ||
state: any; | ||
emitter: import("event-kit").Emitter<{ | ||
[key: string]: any; | ||
}, {}>; | ||
inWillUpdate: boolean; | ||
willUpdateStates: any[]; | ||
_isInit: boolean; | ||
__FLUX_STORE__: boolean; | ||
willInit(): void; | ||
init(): void; | ||
buildStore(storeClass: import("./IExtendStoreBase").IExtendStoreBaseConstructor, args: any[], options?: any): import("./IExtendStoreBase").default; | ||
setState(state: any): void; | ||
onDidUpdate(callback: (state: any) => void): import("event-kit").Disposable; | ||
onWillUpdate(callback: (state: any) => void): import("event-kit").Disposable; | ||
observe(callback: (state: any) => void): import("event-kit").Disposable; | ||
dispose(): void; | ||
destroy(): void; | ||
getState(): any; | ||
}; | ||
[x: string]: any; | ||
innerStores: any; | ||
innerStores: import("./IStoresDeclarer").default; | ||
}; | ||
export default _default; | ||
//# sourceMappingURL=MainStoreBase.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var StoreBase_1 = require("event-flux/lib/StoreBase"); | ||
var StoreBase_1 = require("./StoreBase"); | ||
var stateFilterDecorator_1 = require("./utils/stateFilterDecorator"); | ||
exports.default = stateFilterDecorator_1.addStateFilter(StoreBase_1.default); | ||
exports.default = stateFilterDecorator_1.default(StoreBase_1.default); |
import MultiWinStore from './MultiWinStore'; | ||
import { IWinState } from './ElectronWindowState'; | ||
import { BrowserWindow } from 'electron'; | ||
import IStorage from './storage/IStorage'; | ||
import { IWinProps, IWinParams } from './IMultiWinStore'; | ||
export declare class WindowManager { | ||
windows: any[]; | ||
windows: { | ||
clientId: string; | ||
window: BrowserWindow; | ||
}[]; | ||
winHandler: any; | ||
winSize: number; | ||
constructor(winHandler: any); | ||
constructor(winHandler: MultiWinCacheStore); | ||
genClientId(): string; | ||
createWin(clientId: any): any; | ||
createWin(clientId: string): any; | ||
ensureWindows(): void; | ||
getWin(): any; | ||
getWin(): { | ||
clientId: string; | ||
window: BrowserWindow; | ||
} | undefined; | ||
dispose(): void; | ||
} | ||
interface IClientCacheInfo { | ||
parentId?: string; | ||
clientId: string; | ||
name?: string; | ||
groups?: string[]; | ||
url: string; | ||
winState?: IWinState; | ||
} | ||
declare class MultiWinCacheStore extends MultiWinStore { | ||
clientInfoMap: {}; | ||
clientStateMap: {}; | ||
clientIds: any[]; | ||
createWins: any[]; | ||
clientInfoMap: { | ||
[clientId: string]: IClientCacheInfo; | ||
}; | ||
willQuit: boolean; | ||
windowManager: WindowManager; | ||
static createWindow: any; | ||
windowManager?: WindowManager; | ||
init(): void; | ||
filterClients(clients: any): any; | ||
getDefaultClients(): { | ||
loadClients(): void; | ||
saveClients(): void; | ||
getDefaultClients(): IClientCacheInfo[]; | ||
getStorage(): IStorage | null; | ||
_removeClientId(clientId: string): void; | ||
onDidWinClose(clientId: string): void; | ||
closeWin(clientId: string): void; | ||
saveWinState(clientId: string, winState: IWinState): void; | ||
createWinForClientId(winProps: IWinProps, clientId: string, parentClientId: string | undefined, params: IWinParams): string | null; | ||
createWin(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string | null; | ||
_createWinForElectron(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string | null; | ||
_createElectronWin(url: string | IWinProps, clientId: string | null, parentId: string | undefined, params: IWinState): string | null; | ||
_getElectronWinFromCache(url: string, clientId: string | null, parentId: string | undefined, params: IWinParams): { | ||
clientId: string; | ||
url: string; | ||
winState: { | ||
isMaximized: boolean; | ||
}; | ||
}[]; | ||
getStorage(): any; | ||
onDidWinClose(clientId: any): void; | ||
closeAllWindows(): void; | ||
saveClients(clientIds: any): void; | ||
saveWinState(clientId: any, winState: any): void; | ||
createWin(url: any, parentClientId: any, params: any): any; | ||
createElectronWin(url: any, clientId: any, parentId: any, params: any): any; | ||
getElectronWin(url: any, clientId: any, parentId: any, params: any): { | ||
clientId: any; | ||
win: any; | ||
win: BrowserWindow; | ||
}; | ||
createWindow(clientId: any, url: string, parentId: string, params: any): BrowserWindow; | ||
createMainWindow(url: any, clientId: any, parentId: any, params?: any): BrowserWindow; | ||
actionChanged(clientId: any, action: any): void; | ||
activeWindow(clientId: any): void; | ||
createNativeWindow(clientId: string, url: string, parentId: string, params: IWinParams): BrowserWindow; | ||
createMainWindow(url: string, clientId: string, parentId: string | undefined, params?: any): BrowserWindow; | ||
onChangeAction(clientId: string, action: string): void; | ||
onCloseMainClient(): void; | ||
activeWin(clientId: string): void; | ||
} | ||
export default MultiWinCacheStore; | ||
//# sourceMappingURL=MultiWinCacheStore.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -36,3 +36,2 @@ extendStatics(d, b); | ||
function WindowManager(winHandler) { | ||
var _this = this; | ||
this.windows = []; | ||
@@ -42,3 +41,4 @@ this.winSize = 1; | ||
this.winHandler = winHandler; | ||
electron_1.app.whenReady().then(function () { return _this.ensureWindows(); }); | ||
// app.whenReady().then(() => this.ensureWindows()); | ||
this.ensureWindows(); | ||
} | ||
@@ -50,3 +50,3 @@ WindowManager.prototype.genClientId = function () { | ||
WindowManager.prototype.createWin = function (clientId) { | ||
return this.winHandler.createWindow(clientId); | ||
return this.winHandler.createNativeWindow(clientId); | ||
}; | ||
@@ -63,3 +63,3 @@ WindowManager.prototype.ensureWindows = function () { | ||
if (!this.windows) | ||
return null; | ||
return undefined; | ||
var winInfo = this.windows.shift(); | ||
@@ -76,3 +76,3 @@ this.ensureWindows(); | ||
}); | ||
this.windows = null; | ||
this.windows = []; | ||
}; | ||
@@ -87,5 +87,2 @@ return WindowManager; | ||
_this.clientInfoMap = {}; | ||
_this.clientStateMap = {}; | ||
_this.clientIds = []; | ||
_this.createWins = []; | ||
_this.willQuit = false; | ||
@@ -95,3 +92,7 @@ return _this; | ||
MultiWinCacheStore.prototype.init = function () { | ||
this.loadClients(); | ||
}; | ||
MultiWinCacheStore.prototype.loadClients = function () { | ||
var _this = this; | ||
this.willQuit = false; | ||
var clients = this.getStorage().get('clients'); | ||
@@ -101,20 +102,12 @@ if (!clients || clients.length === 0) { | ||
} | ||
else { | ||
clients = this.filterClients(clients); | ||
} | ||
this.windowManager = new WindowManager(this); | ||
electron_1.app.whenReady().then(function () { | ||
clients.forEach(function (item) { return _this.createElectronWin(item.url, item.clientId, item.parentId, item.winState); }); | ||
}); | ||
// app.whenReady().then(() => { | ||
// clients.forEach(item => this.createElectronWin(item.url, item.clientId, item.parentId, item.winState)); | ||
// }); | ||
clients.forEach(function (item) { return _this.createWinForClientId(item, item.clientId, item.parentId, item.winState); }); | ||
}; | ||
MultiWinCacheStore.prototype.filterClients = function (clients) { | ||
var clientIds = new Set(); | ||
clients = clients.filter(function (client) { | ||
if (!clientIds.has(client.clientId)) { | ||
clientIds.add(client.clientId); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
return clients; | ||
MultiWinCacheStore.prototype.saveClients = function () { | ||
var _this = this; | ||
var clients = this.clientIds.map(function (id) { return (__assign({ clientId: id }, _this.clientInfoMap[id], { name: _this.clientIdNameMap[id], groups: _this.groupsMap[id] })); }); | ||
this.getStorage().set('clients', clients); | ||
}; | ||
@@ -128,29 +121,34 @@ MultiWinCacheStore.prototype.getDefaultClients = function () { | ||
}; | ||
MultiWinCacheStore.prototype._removeClientId = function (clientId) { | ||
_super.prototype._removeClientId.call(this, clientId); | ||
delete this.clientInfoMap[clientId]; | ||
}; | ||
MultiWinCacheStore.prototype.onDidWinClose = function (clientId) { | ||
// this._appStore.mainClient.sendMessageByClientId(clientId, { action: 'did-close' }); | ||
this._appStore.mainClient.sendWinMsg(clientId, { action: 'did-close' }); | ||
}; | ||
MultiWinCacheStore.prototype.closeAllWindows = function () { | ||
// this._appStore.mainClient.closeAllWindows(); | ||
this.windowManager.dispose(); | ||
this.createWins.slice().forEach(function (window) { | ||
if (!window.isDestroyed()) { | ||
window.close(); | ||
} | ||
}); | ||
this.namedWinIdMap = {}; | ||
this.clientNamedWinIdMap = {}; | ||
MultiWinCacheStore.prototype.closeWin = function (clientId) { | ||
var win = this.clientWins[clientId]; | ||
if (win && !win.isDestroyed()) | ||
win.close(); | ||
}; | ||
MultiWinCacheStore.prototype.saveClients = function (clientIds) { | ||
var _this = this; | ||
var clients = clientIds.map(function (id) { return (__assign({ clientId: id }, _this.clientInfoMap[id], { winState: _this.clientStateMap[id] })); }); | ||
this.getStorage().set('clients', clients); | ||
}; | ||
MultiWinCacheStore.prototype.saveWinState = function (clientId, winState) { | ||
this.clientStateMap[clientId] = winState; | ||
this.saveClients(this.clientIds || []); | ||
this.clientInfoMap[clientId].winState = winState; | ||
this.saveClients(); | ||
}; | ||
MultiWinCacheStore.prototype.createWin = function (url, parentClientId, params) { | ||
// 当要创建的窗口有clientId时 | ||
MultiWinCacheStore.prototype.createWinForClientId = function (winProps, clientId, parentClientId, params) { | ||
return this._createElectronWin(winProps, clientId, parentClientId, params); | ||
}; | ||
// 创建一个全新的窗口,使用自生成的clientId | ||
MultiWinCacheStore.prototype.createWin = function (winProps, parentClientId, params) { | ||
return this._createWinForElectron(winProps, parentClientId, params); | ||
}; | ||
MultiWinCacheStore.prototype._createWinForElectron = function (winProps, parentClientId, params) { | ||
winProps = this._parseWinProps(winProps); | ||
if (params && params.x == null && params.y == null) { | ||
if (parentClientId) { | ||
var window_1 = this._appStore.mainClient.getWindowByClientId(parentClientId); | ||
params.width = params.width || 800; | ||
params.height = params.height || 600; | ||
if (parentClientId && this.clientWins[parentClientId]) { | ||
var window_1 = this.clientWins[parentClientId]; | ||
// let window = (this._appStore as any).mainClient.getWindowByClientId(parentClientId); | ||
var bounds = params.useContentSize ? window_1.getContentBounds() : window_1.getBounds(); | ||
@@ -166,5 +164,5 @@ params.x = bounds.x + bounds.width / 2 - params.width / 2; | ||
} | ||
var clientId; | ||
var clientId = null; | ||
try { | ||
clientId = this.createElectronWin(url, clientId, parentClientId, params); | ||
clientId = this._createElectronWin(winProps.path, null, parentClientId, params); | ||
} | ||
@@ -176,37 +174,27 @@ catch (err) { | ||
}; | ||
MultiWinCacheStore.prototype.createElectronWin = function (url, clientId, parentId, params) { | ||
MultiWinCacheStore.prototype._createElectronWin = function (url, clientId, parentId, params) { | ||
var _this = this; | ||
if (clientId && this.clientIds.indexOf(clientId) !== -1) { | ||
return; | ||
return null; | ||
} | ||
var winProps = this._parseWinProps(url); | ||
var winState = new ElectronWindowState_1.default(null, params, null); | ||
var winInfo = this.getElectronWin(url, clientId, parentId, winState.state); | ||
var winInfo = this._getElectronWinFromCache(winProps.path, clientId, parentId, winState.state); | ||
if (!clientId) | ||
clientId = winInfo.clientId; | ||
this.clientIds.push(clientId); | ||
this.clientInfoMap[clientId] = { url: url, parentId: parentId }; | ||
var win = winInfo.win; | ||
this._addWinProps(clientId, win, winProps); | ||
this.clientInfoMap[clientId] = { url: winProps.path, clientId: clientId, parentId: parentId, winState: winState.state }; | ||
winState.onSave = function (state) { | ||
_this.saveWinState(clientId, state); | ||
}; | ||
this.clientStateMap[clientId] = winState.state; | ||
// this.clientStateMap[clientId] = winState.state; | ||
winState.manage(win); | ||
this.createWins.push(win); | ||
this.saveClients(this.clientIds); // Save clients into Storage | ||
this.saveClients(); // Save clients into Storage | ||
win.on('closed', function () { | ||
var winIndex = _this.createWins.indexOf(win); | ||
_this.createWins.splice(winIndex, 1); | ||
var winId = _this.clientNamedWinIdMap[clientId]; | ||
if (winId) { | ||
_this.clientNamedWinIdMap[clientId] = undefined; | ||
_this.namedWinIdMap[winId] = undefined; | ||
} | ||
if (_this.willQuit) | ||
return; | ||
if (clientId === 'mainClient') { | ||
_this.willQuit = true; | ||
_this._appStore.willQuit = true; | ||
return _this.closeAllWindows(); | ||
_this.closeAllWindows(); | ||
} | ||
_this.onDidWinClose && _this.onDidWinClose({ clientId: clientId }); | ||
_this.onDidWinClose && _this.onDidWinClose(clientId); | ||
var index = _this.clientIds.indexOf(clientId); | ||
@@ -216,7 +204,9 @@ if (index !== -1) { | ||
} | ||
_this.saveClients(_this.clientIds); | ||
if (!_this.willQuit) | ||
_this.saveClients(); | ||
_this._removeClientId(clientId); | ||
}); | ||
return clientId; | ||
}; | ||
MultiWinCacheStore.prototype.getElectronWin = function (url, clientId, parentId, params) { | ||
MultiWinCacheStore.prototype._getElectronWinFromCache = function (url, clientId, parentId, params) { | ||
// return createMainWindow(url, clientId, params); | ||
@@ -230,3 +220,3 @@ var win; | ||
if (!winInfo) { | ||
clientId = this.genClientId(); | ||
clientId = this._genClientId(); | ||
win = this.createMainWindow(url, clientId, parentId, params); | ||
@@ -240,10 +230,10 @@ } | ||
var setBoundsFunc_1 = params.useContentSize ? 'setContentBounds' : 'setBounds'; | ||
var x_1 = parseInt(params.x) || 0; | ||
var y_1 = parseInt(params.y) || 0; | ||
var width_1 = parseInt(params.width), height_1 = parseInt(params.height); | ||
var x_1 = Math.floor(params.x || 0); | ||
var y_1 = Math.floor(params.y || 0); | ||
var width_1 = Math.floor(params.width || 800), height_1 = Math.floor(params.height || 600); | ||
if (params.minWidth && params.minHeight) { | ||
win.setMinimumSize(parseInt(params.minWidth), parseInt(params.minHeight)); | ||
win.setMinimumSize(Math.floor(params.minWidth), Math.floor(params.minHeight)); | ||
} | ||
if (params.maxWidth && params.maxHeight) { | ||
win.setMaximumSize(parseInt(params.maxWidth), parseInt(params.maxHeight)); | ||
win.setMaximumSize(Math.floor(params.maxWidth), Math.floor(params.maxHeight)); | ||
} | ||
@@ -267,3 +257,3 @@ if (params.title) { | ||
}; | ||
MultiWinCacheStore.prototype.createWindow = function (clientId, url, parentId, params) { | ||
MultiWinCacheStore.prototype.createNativeWindow = function (clientId, url, parentId, params) { | ||
if (url === void 0) { url = 'empty'; } | ||
@@ -280,4 +270,7 @@ if (parentId === void 0) { parentId = ''; } | ||
show: false, | ||
x: parseInt(params.x), y: parseInt(params.y), | ||
x: Math.floor(params.x || 0), y: Math.floor(params.y || 0), | ||
width: params.width, height: params.height, | ||
minWidth: params.minWidth || params.width, minHeight: params.minHeight || params.height, | ||
maxWidth: params.maxWidth, maxHeight: params.maxHeight, | ||
title: params.title, | ||
useContentSize: params.useContentSize, | ||
@@ -308,3 +301,3 @@ }); | ||
if (params === void 0) { params = {}; } | ||
var window = this.createWindow(clientId, url, parentId, params); | ||
var window = this.createNativeWindow(clientId, url, parentId, params); | ||
window.on('ready-to-show', function () { | ||
@@ -315,10 +308,12 @@ window.show(); | ||
}; | ||
MultiWinCacheStore.prototype.actionChanged = function (clientId, action) { | ||
MultiWinCacheStore.prototype.onChangeAction = function (clientId, action) { | ||
if (this.clientInfoMap[clientId]) { | ||
this.clientInfoMap[clientId].url = action; | ||
this.saveClients(this.clientIds); | ||
this.saveClients(); | ||
} | ||
}; | ||
MultiWinCacheStore.prototype.activeWindow = function (clientId) { | ||
var win = this._appStore.mainClient.getWindowByClientId(clientId); | ||
MultiWinCacheStore.prototype.onCloseMainClient = function () { | ||
}; | ||
MultiWinCacheStore.prototype.activeWin = function (clientId) { | ||
var win = this.clientWins[clientId]; | ||
if (win) { | ||
@@ -325,0 +320,0 @@ win.moveTop(); |
import StoreBase from './MainStoreBase'; | ||
export declare class WinPackStore extends StoreBase { | ||
getSubStores: Function; | ||
destroy(): void; | ||
@@ -9,8 +8,8 @@ } | ||
constructor(); | ||
addWin(winId: any): void; | ||
deleteWin(winId: any): void; | ||
addWin(winId: string): void; | ||
deleteWin(winId: string): void; | ||
getClienIds(): any; | ||
onDidAddWin(callback: any): any; | ||
onDidRemoveWin(callback: any): any; | ||
onDidAddWin(callback: (clientId: string) => void): import("event-kit").Disposable; | ||
onDidRemoveWin(callback: (clientId: string) => void): import("event-kit").Disposable; | ||
} | ||
//# sourceMappingURL=MultiWinManagerStore.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -11,0 +11,0 @@ extendStatics(d, b); |
@@ -1,24 +0,47 @@ | ||
import StoreBase from 'event-flux/lib/StoreBase'; | ||
export default class MultiWinStore extends StoreBase { | ||
namedWinIdMap: { | ||
[winId: string]: string; | ||
import StoreBase from './StoreBase'; | ||
import IMultiWinStore, { IWinProps, IWinParams } from './IMultiWinStore'; | ||
interface IWindow { | ||
close(): void; | ||
} | ||
export default class MultiWinStore extends StoreBase implements IMultiWinStore { | ||
clientIds: string[]; | ||
clientIdNameMap: { | ||
[clientId: string]: string; | ||
}; | ||
clientNamedWinIdMap: { | ||
[winId: string]: string; | ||
clientNameIdMap: { | ||
[winName: string]: string; | ||
}; | ||
groupsMap: { | ||
[clientId: string]: string[]; | ||
}; | ||
clientWins: { | ||
[clientId: string]: IWindow; | ||
}; | ||
init(): void; | ||
createWin(url: any, parentClientId: any, params: any): any; | ||
createOrOpenWin(winId: any, url: any, parentClientId: any, params: any): any; | ||
closeWin(clientId: any): void; | ||
closeWinByWinId(winId: any): void; | ||
sendWinMsg(winId: string, message: any): void; | ||
genClientId(): any; | ||
_addWinProps(clientId: string, win: IWindow | null, winProps: IWinProps): void; | ||
_parseWinProps(winProps: string | IWinProps): IWinProps; | ||
_removeClientId(clientId: string): void; | ||
_createWinForBrowser(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string; | ||
_createWinForElectron(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string | null; | ||
createWin(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string | null; | ||
createOrOpenWin(winName: string, url: string | IWinProps, parentClientId: string, params: any): string | null; | ||
closeWin(clientId: string): void; | ||
closeWinByName(name: string): void; | ||
closeWinByGroup(group: string): void; | ||
activeWin(clientId: string): void; | ||
activeWindow(clientId: string): void; | ||
activeWinByName(name: string): void; | ||
activeWinByGroup(group: string): void; | ||
sendWinMsg(clientId: string, message: any): void; | ||
sendWinMsgByName(name: string, message: any): void; | ||
sendWinMsgByGroup(group: string, message: any): void; | ||
_genClientId(): string; | ||
closeAllWindows(): void; | ||
createBrowserWin(url: any, clientId: any, params?: any): Window; | ||
createElectronWin(url: any, clientId: any, parentClientId: any, params: any): void; | ||
actionChanged(clientId: any, action: any): void; | ||
changeClientAction(clientId: any, url: any): void; | ||
getWinRootStore(clientId: any): any; | ||
activeWindow(clientId: any): void; | ||
createBrowserWin(url: string, clientId: string, params?: any): Window | null; | ||
createElectronWin(url: string, clientId: string, parentClientId: string, params: any): string | undefined; | ||
onChangeAction(clientId: string, action: string): void; | ||
changeClientAction(clientId: string, url: string): void; | ||
getWinRootStore(clientId: string): any; | ||
} | ||
export {}; | ||
//# sourceMappingURL=MultiWinStore.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -16,5 +16,26 @@ extendStatics(d, b); | ||
})(); | ||
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("event-flux/lib/StoreBase"); | ||
var winManagerStoreName = require('./constants').winManagerStoreName; | ||
var StoreBase_1 = require("./StoreBase"); | ||
var constants_1 = require("./constants"); | ||
function genBrowserUrl(url, clientId, parentId) { | ||
@@ -38,6 +59,9 @@ if (url === void 0) { url = ''; } | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.clientIds = []; | ||
// namedWinId to clientId map | ||
_this.namedWinIdMap = {}; | ||
_this.clientIdNameMap = {}; | ||
// clientId to namedWinId map | ||
_this.clientNamedWinIdMap = {}; | ||
_this.clientNameIdMap = {}; | ||
_this.groupsMap = {}; | ||
_this.clientWins = {}; | ||
return _this; | ||
@@ -47,5 +71,2 @@ } | ||
var _this = this; | ||
this.appStores[winManagerStoreName].observe(function (state) { | ||
_this.setState({ clientIds: state.clientIds }); | ||
}); | ||
if (typeof window === 'object') { | ||
@@ -55,7 +76,3 @@ window.addEventListener("message", function (event) { | ||
if (action === 'close') { | ||
var winId = _this.clientNamedWinIdMap[clientId]; | ||
if (winId) { | ||
_this.clientNamedWinIdMap[clientId] = undefined; | ||
_this.namedWinIdMap[winId] = undefined; | ||
} | ||
_this._removeClientId(clientId); | ||
} | ||
@@ -66,33 +83,73 @@ }); | ||
}); | ||
// 将main window加入到 新创建窗口列表中 | ||
this._addWinProps("mainClient", window, { name: "mainClient", groups: ["main"] }); | ||
} | ||
}; | ||
MultiWinStore.prototype.createWin = function (url, parentClientId, params) { | ||
var clientId; | ||
// 新增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') { | ||
clientId = this.genClientId(); | ||
var win = this.createBrowserWin(genBrowserUrl(url, clientId, parentClientId), clientId, params); | ||
this._appStore.mainClient.addWin(clientId, win); | ||
return this._createWinForBrowser(winProps, parentClientId, params); | ||
} | ||
else { | ||
try { | ||
clientId = this.createElectronWin(url, clientId, parentClientId, params); | ||
} | ||
catch (err) { | ||
console.error(err, err.stack); | ||
} | ||
return this._createWinForElectron(winProps, parentClientId, params); | ||
} | ||
return clientId; | ||
}; | ||
// Create new win if the specific winId is not exists | ||
MultiWinStore.prototype.createOrOpenWin = function (winId, url, parentClientId, params) { | ||
if (!this.namedWinIdMap[winId]) { | ||
var clientId = this.createWin(url, parentClientId, params); | ||
this.namedWinIdMap[winId] = clientId; | ||
this.clientNamedWinIdMap[clientId] = winId; | ||
return clientId; | ||
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.namedWinIdMap[winId]; | ||
this.changeClientAction(clientId, url); | ||
this.activeWindow(clientId); | ||
var clientId = this.clientNameIdMap[winName]; | ||
this.changeClientAction(clientId, typeof url === 'string' ? url : url.path); | ||
this.activeWin(clientId); | ||
return clientId; | ||
@@ -102,18 +159,89 @@ } | ||
MultiWinStore.prototype.closeWin = function (clientId) { | ||
if (typeof window === 'object') { | ||
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); | ||
} | ||
} | ||
} | ||
else { | ||
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.closeWinByWinId = function (winId) { | ||
var clientId = this.namedWinIdMap[winId]; | ||
clientId && this.closeWin(clientId); | ||
MultiWinStore.prototype.activeWin = function (clientId) { }; | ||
MultiWinStore.prototype.activeWindow = function (clientId) { | ||
this.activeWin(clientId); | ||
}; | ||
MultiWinStore.prototype.sendWinMsg = function (winId, message) { | ||
this._appStore.mainClient.sendMessageByClientId(winId, message); | ||
MultiWinStore.prototype.activeWinByName = function (name) { | ||
var clientId = this.clientNameIdMap[name]; | ||
if (clientId) | ||
this.activeWin(clientId); | ||
}; | ||
MultiWinStore.prototype.genClientId = function () { | ||
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.state.clientIds.indexOf(clientId) !== -1) { | ||
return this.genClientId(); | ||
return this._genClientId(); | ||
} | ||
@@ -123,7 +251,9 @@ return clientId; | ||
MultiWinStore.prototype.closeAllWindows = function () { | ||
this._appStore.mainClient.closeAllWindows(); | ||
this.namedWinIdMap = {}; | ||
this.clientNamedWinIdMap = {}; | ||
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 = {}; } | ||
@@ -136,2 +266,4 @@ if (!params.width) | ||
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; | ||
@@ -141,4 +273,5 @@ }; | ||
console.error('Please provide the createElectronWin'); | ||
return; | ||
}; | ||
MultiWinStore.prototype.actionChanged = function (clientId, action) { | ||
MultiWinStore.prototype.onChangeAction = function (clientId, action) { | ||
}; | ||
@@ -149,7 +282,6 @@ MultiWinStore.prototype.changeClientAction = function (clientId, url) { | ||
MultiWinStore.prototype.getWinRootStore = function (clientId) { | ||
return this.appStores[winManagerStoreName].winPackMapStore.get(clientId); | ||
return this.appStores[constants_1.winManagerStoreName].winPackMapStore.get(clientId); | ||
}; | ||
MultiWinStore.prototype.activeWindow = function (clientId) { }; | ||
return MultiWinStore; | ||
}(StoreBase_1.default)); | ||
exports.default = MultiWinStore; |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -11,0 +11,0 @@ extendStatics(d, b); |
@@ -1,9 +0,11 @@ | ||
import AppStore from 'event-flux/lib/AppStore'; | ||
import AppStore from './AppStore'; | ||
import StoreProxyHandler from './utils/StoreProxyHandler'; | ||
import { Emitter } from 'event-kit'; | ||
import { Log } from './utils/loggerApply'; | ||
import { Log, Logger } from './utils/loggerApply'; | ||
import IStoresDeclarer, { IStoresObjDeclarer } from './IStoresDeclarer'; | ||
import IExtendStoreBase from './IExtendStoreBase'; | ||
declare class IDGenerator { | ||
count: number; | ||
genID(): number; | ||
dispose(id: any): void; | ||
dispose(id: number): void; | ||
} | ||
@@ -14,32 +16,40 @@ export declare class RendererAppStore extends AppStore { | ||
idGenerator: IDGenerator; | ||
resolveMap: {}; | ||
resolveMap: { | ||
[invokeId: string]: { | ||
resolve: (res: any) => void; | ||
reject: (err: Error) => void; | ||
}; | ||
}; | ||
storeShape: any; | ||
storeProxyHandler: StoreProxyHandler; | ||
storeResolve: () => void; | ||
storeResolve?: () => void; | ||
winInitParams: any; | ||
log: Log; | ||
static innerStores: any; | ||
static innerStores: IStoresDeclarer; | ||
constructor(log: Log); | ||
asyncInit(): Promise<{}>; | ||
asyncInit(): Promise<unknown>; | ||
handleStorePromise: (state: any, store: any) => void; | ||
handleStore(resolve: any, filter: any, state: any, store: any): void; | ||
handleAction(action: any): void; | ||
handleResult(invokeId: any, error: any, result: any): void; | ||
handleStore(resolve: () => void, state: any, store: any): void; | ||
handleAction(actionStr: string): void; | ||
handleResult(invokeId: number, error: Error, result: any): void; | ||
handleMessage(message: any): void; | ||
handleWinMessage(senderId: any, message: any): void; | ||
handleWinMessage(senderId: string, message: any): void; | ||
handleInitWindow(params: any): void; | ||
observeInitWindow(callback: any): void; | ||
sendWindowMessage(clientId: any, args: any): void; | ||
onDidMessage(callback: any): any; | ||
onDidClose(callback: any): any; | ||
onDidWinMessage(callback: any): any; | ||
observeInitWindow(callback: (params: any) => void): void; | ||
sendWindowMessage(clientId: string, args: any): void; | ||
onDidMessage(callback: (message: any) => void): import("event-kit").Disposable; | ||
onDidClose(callback: () => void): import("event-kit").Disposable; | ||
onDidWinMessage(callback: ({ senderId, message }: { | ||
senderId: string; | ||
message: any; | ||
}) => void): import("event-kit").Disposable; | ||
initRenderStores(): void; | ||
getStore(key: any): any; | ||
setStore(key: any, store: any): any; | ||
getStore(key: string): any; | ||
setStore(key: string, store: IExtendStoreBase): IExtendStoreBase; | ||
buildStores(): void; | ||
initStores(parent: any): void; | ||
initStores(parent: AppStore): void; | ||
startObserve(): void; | ||
} | ||
export default function buildRendererAppStore(stores: any, onChange: any, logger: any): RendererAppStore; | ||
export default function buildRendererAppStore(stores: IStoresObjDeclarer, onChange: (state: any) => void, logger: Logger): RendererAppStore; | ||
export {}; | ||
//# sourceMappingURL=RendererAppStore.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -17,3 +17,3 @@ extendStatics(d, b); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var AppStore_1 = require("event-flux/lib/AppStore"); | ||
var AppStore_1 = require("./AppStore"); | ||
var objectMerge_1 = require("./utils/objectMerge"); | ||
@@ -41,2 +41,3 @@ var json_immutable_bn_1 = require("json-immutable-bn"); | ||
var _this = _super.call(this) || this; | ||
_this.emitter = new event_kit_1.Emitter(); | ||
_this.idGenerator = new IDGenerator(); | ||
@@ -46,3 +47,3 @@ _this.resolveMap = {}; | ||
_this.handleStorePromise = function (state, store) { | ||
_this.handleStore(_this.storeResolve, true, state, store); | ||
_this.handleStore(_this.storeResolve, state, store); | ||
}; | ||
@@ -55,6 +56,4 @@ _this.log = log; | ||
_super.prototype.init.call(this); | ||
this.emitter = new event_kit_1.Emitter(); | ||
var filter = true; | ||
// 先初始化,防止由于promise的异步 漏掉某些消息 | ||
this.client = new RendererClient_1.default(filter, this.handleStorePromise, this.handleAction.bind(this), this.handleResult.bind(this), this.handleMessage.bind(this), this.handleWinMessage.bind(this), this.handleInitWindow.bind(this)); | ||
this.client = new RendererClient_1.default(this.handleStorePromise, this.handleAction.bind(this), this.handleResult.bind(this), this.handleMessage.bind(this), this.handleWinMessage.bind(this), this.handleInitWindow.bind(this)); | ||
return new Promise(function (resolve) { | ||
@@ -64,3 +63,3 @@ _this.storeResolve = resolve; | ||
}; | ||
RendererAppStore.prototype.handleStore = function (resolve, filter, state, store) { | ||
RendererAppStore.prototype.handleStore = function (resolve, state, store) { | ||
var _this = this; | ||
@@ -80,4 +79,4 @@ var storeData = json_immutable_bn_1.deserialize(state); | ||
}; | ||
RendererAppStore.prototype.handleAction = function (action) { | ||
action = json_immutable_bn_1.deserialize(action); | ||
RendererAppStore.prototype.handleAction = function (actionStr) { | ||
var action = json_immutable_bn_1.deserialize(actionStr); | ||
var _a = action.payload, updated = _a.updated, deleted = _a.deleted; | ||
@@ -93,3 +92,3 @@ // const withDeletions = filterObject(this.state, deleted); | ||
var _a = this.resolveMap[invokeId], resolve = _a.resolve, reject = _a.reject; | ||
this.resolveMap[invokeId] = null; | ||
delete this.resolveMap[invokeId]; | ||
if (error) { | ||
@@ -96,0 +95,0 @@ reject(error); |
@@ -0,1 +1,4 @@ | ||
import { RendererAppStore } from './RendererAppStore'; | ||
import { IStoresObjDeclarer } from './IStoresDeclarer'; | ||
import { Logger } from './utils/loggerApply'; | ||
interface RenderOptions { | ||
@@ -5,4 +8,4 @@ renderHandler?: any; | ||
} | ||
export default function rendererInit(rendererStores: any, options: RenderOptions, logger: any): Promise<import("./RendererAppStore").RendererAppStore>; | ||
export default function rendererInit(rendererStores: IStoresObjDeclarer, options: RenderOptions, logger: Logger): Promise<RendererAppStore>; | ||
export {}; | ||
//# sourceMappingURL=rendererInitializer.d.ts.map |
@@ -1,9 +0,13 @@ | ||
export default class ElectronStore { | ||
import IStorage from "./IStorage"; | ||
import Store = require("electron-store"); | ||
export default class ElectronStore implements IStorage { | ||
store: any; | ||
ns: string; | ||
constructor(version: any, store: any, ns: any); | ||
getNSStore(ns: any): ElectronStore; | ||
set(key: any, value: any): any; | ||
get(key: any, defaultValue: any): any; | ||
delete(key: any): any; | ||
constructor(version: string | null, store: Store<string>, ns: string); | ||
getNSStore(ns: string): IStorage; | ||
set(key: string | { | ||
[key: string]: any; | ||
}, value: any): any; | ||
get(key: string, defaultValue?: any): any; | ||
delete(key: string): any; | ||
deleteAll(): any; | ||
@@ -10,0 +14,0 @@ clear(): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Store = require('electron-store'); | ||
var Store = require("electron-store"); | ||
var ElectronStore = /** @class */ (function () { | ||
@@ -5,0 +5,0 @@ function ElectronStore(version, store, ns) { |
@@ -1,10 +0,13 @@ | ||
export default class AsyncStorage { | ||
import IStorage from "./IStorage"; | ||
export default class AsyncStorage implements IStorage { | ||
ns: string; | ||
constructor(version: any, ns: any); | ||
init(version: any): void; | ||
set(key: any, value: any): void; | ||
get(key: any, defaultValue: any): any; | ||
delete(key: any): void; | ||
getNSStore(namespace: any): AsyncStorage; | ||
constructor(version: string | null, ns: string); | ||
init(version: string): void; | ||
set(key: string | { | ||
[key: string]: any; | ||
}, value: any): void; | ||
get(key: string, defaultValue?: any): any; | ||
delete(key: string): void; | ||
getNSStore(namespace: string): AsyncStorage; | ||
} | ||
//# sourceMappingURL=LocalStore.d.ts.map |
@@ -0,1 +1,2 @@ | ||
import { StoreBaseConstructor } from "./StoreBase"; | ||
interface StoreDeclarerOptions { | ||
@@ -6,7 +7,11 @@ args?: [any]; | ||
} | ||
declare function StoreDeclarer(Store: any, options?: StoreDeclarerOptions): void; | ||
declare namespace StoreDeclarer { | ||
var isStore: (maybeStore: any) => boolean; | ||
declare const IS_STORE = "@@__STORE_ITEM__@@"; | ||
declare class StoreDeclarer { | ||
Store: StoreBaseConstructor; | ||
options: StoreDeclarerOptions | undefined; | ||
constructor(Store: StoreBaseConstructor, options?: StoreDeclarerOptions); | ||
[IS_STORE]: true; | ||
static isStore(maybeStore: any): boolean; | ||
} | ||
declare function declareStore(Store: any, options?: StoreDeclarerOptions): any; | ||
declare function declareStore(Store: StoreBaseConstructor, options?: StoreDeclarerOptions): StoreDeclarer; | ||
interface StoreListDeclarerOptions { | ||
@@ -17,7 +22,11 @@ args?: [any]; | ||
} | ||
declare function StoreListDeclarer(Store: any, options?: StoreListDeclarerOptions): void; | ||
declare namespace StoreListDeclarer { | ||
var isStoreList: (maybeList: any) => boolean; | ||
declare const IS_STORE_LIST = "@@__STORE_LIST__@@"; | ||
declare class StoreListDeclarer { | ||
Store: StoreBaseConstructor; | ||
options: StoreListDeclarerOptions | undefined; | ||
constructor(Store: StoreBaseConstructor, options?: StoreListDeclarerOptions); | ||
[IS_STORE_LIST]: true; | ||
static isStoreList(maybeList: any): boolean; | ||
} | ||
declare function declareStoreList(Store: any, options?: StoreListDeclarerOptions): any; | ||
declare function declareStoreList(Store: StoreBaseConstructor, options?: StoreListDeclarerOptions): StoreListDeclarer; | ||
interface StoreMapDeclarerOptions { | ||
@@ -31,8 +40,12 @@ args?: [any]; | ||
} | ||
declare function StoreMapDeclarer(Store: any, options?: StoreMapDeclarerOptions): void; | ||
declare namespace StoreMapDeclarer { | ||
var isStoreMap: (maybeMap: any) => boolean; | ||
declare const IS_STORE_MAP = "@@__STORE_MAP__@@"; | ||
declare class StoreMapDeclarer { | ||
Store: StoreBaseConstructor; | ||
options: StoreMapDeclarerOptions | undefined; | ||
constructor(Store: StoreBaseConstructor, options?: StoreMapDeclarerOptions); | ||
[IS_STORE_MAP]: true; | ||
static isStoreMap(maybeMap: any): boolean; | ||
} | ||
declare function declareStoreMap(Store: any, options?: StoreMapDeclarerOptions): any; | ||
declare function declareStoreMap(Store: StoreBaseConstructor, options?: StoreMapDeclarerOptions): StoreMapDeclarer; | ||
export { StoreDeclarer, StoreListDeclarer, StoreMapDeclarer, declareStore, declareStoreList, declareStoreMap }; | ||
//# sourceMappingURL=StoreDeclarer.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var IS_STORE = '@@__STORE_ITEM__@@'; | ||
function StoreDeclarer(Store, options) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
var StoreDeclarer = /** @class */ (function () { | ||
function StoreDeclarer(Store, options) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
StoreDeclarer.isStore = function (maybeStore) { | ||
return !!(maybeStore && maybeStore[IS_STORE]); | ||
}; | ||
return StoreDeclarer; | ||
}()); | ||
exports.StoreDeclarer = StoreDeclarer; | ||
StoreDeclarer.prototype[IS_STORE] = true; | ||
StoreDeclarer.isStore = function (maybeStore) { | ||
return !!(maybeStore && maybeStore[IS_STORE]); | ||
}; | ||
function declareStore(Store, options) { | ||
@@ -18,11 +20,13 @@ return new StoreDeclarer(Store, options); | ||
var IS_STORE_LIST = '@@__STORE_LIST__@@'; | ||
function StoreListDeclarer(Store, options) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
var StoreListDeclarer = /** @class */ (function () { | ||
function StoreListDeclarer(Store, options) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
StoreListDeclarer.isStoreList = function (maybeList) { | ||
return !!(maybeList && maybeList[IS_STORE_LIST]); | ||
}; | ||
return StoreListDeclarer; | ||
}()); | ||
exports.StoreListDeclarer = StoreListDeclarer; | ||
StoreListDeclarer.prototype[IS_STORE_LIST] = true; | ||
StoreListDeclarer.isStoreList = function (maybeList) { | ||
return !!(maybeList && maybeList[IS_STORE_LIST]); | ||
}; | ||
function declareStoreList(Store, options) { | ||
@@ -33,11 +37,13 @@ return new StoreListDeclarer(Store, options); | ||
var IS_STORE_MAP = '@@__STORE_MAP__@@'; | ||
function StoreMapDeclarer(Store, options) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
var StoreMapDeclarer = /** @class */ (function () { | ||
function StoreMapDeclarer(Store, options) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
StoreMapDeclarer.isStoreMap = function (maybeMap) { | ||
return !!(maybeMap && maybeMap[IS_STORE_MAP]); | ||
}; | ||
return StoreMapDeclarer; | ||
}()); | ||
exports.StoreMapDeclarer = StoreMapDeclarer; | ||
StoreMapDeclarer.prototype[IS_STORE_MAP] = true; | ||
StoreMapDeclarer.isStoreMap = function (maybeMap) { | ||
return !!(maybeMap && maybeMap[IS_STORE_MAP]); | ||
}; | ||
function declareStoreMap(Store, options) { | ||
@@ -44,0 +50,0 @@ return new StoreMapDeclarer(Store, options); |
@@ -1,2 +0,3 @@ | ||
import StoreBase from 'event-flux/lib/StoreBase'; | ||
import StoreBase from './StoreBase'; | ||
import { Disposable } from 'event-kit'; | ||
declare class SubIdGenerator { | ||
@@ -8,6 +9,8 @@ count: number; | ||
export default class SubStoreBase extends StoreBase { | ||
subMap: {}; | ||
subMap: { | ||
[subId: string]: Disposable; | ||
}; | ||
idGenerator: SubIdGenerator; | ||
unsubscribe(subId: any): void; | ||
genSubId(dispose: any): string; | ||
unsubscribe(subId: string): void; | ||
genSubId(dispose: Disposable): string; | ||
dispose(): void; | ||
@@ -14,0 +17,0 @@ } |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -17,3 +17,3 @@ extendStatics(d, b); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var StoreBase_1 = require("event-flux/lib/StoreBase"); | ||
var StoreBase_1 = require("./StoreBase"); | ||
var SubIdGenerator = /** @class */ (function () { | ||
@@ -44,3 +44,3 @@ function SubIdGenerator() { | ||
dispose && dispose.dispose(); | ||
this.subMap[subId] = null; | ||
delete this.subMap[subId]; | ||
}; | ||
@@ -47,0 +47,0 @@ SubStoreBase.prototype.genSubId = function (dispose) { |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -18,2 +18,3 @@ extendStatics(d, b); | ||
var stateFilterDecorator_1 = require("../stateFilterDecorator"); | ||
var stateFilterMapDecorator_1 = require("../stateFilterMapDecorator"); | ||
var event_kit_1 = require("event-kit"); | ||
@@ -65,3 +66,3 @@ var MockManagerStore = /** @class */ (function () { | ||
test('addStateFilter', function () { | ||
var Base1DeriveClass = stateFilterDecorator_1.addStateFilter(Base1Class); | ||
var Base1DeriveClass = stateFilterDecorator_1.default(Base1Class); | ||
var base1Instance = new Base1DeriveClass(); | ||
@@ -78,3 +79,3 @@ var Base2Class = /** @class */ (function (_super) { | ||
}(Base1Class)); | ||
var StateFilterClass = stateFilterDecorator_1.addStateFilter(Base2Class); | ||
var StateFilterClass = stateFilterDecorator_1.default(Base2Class); | ||
var stateFilterInstance = new StateFilterClass(); | ||
@@ -103,3 +104,3 @@ base1Instance._initWrap(); | ||
test('addStateFilter for defaultFilter option', function () { | ||
var Base1DeriveClass = stateFilterDecorator_1.addStateFilter(Base1Class); | ||
var Base1DeriveClass = stateFilterDecorator_1.default(Base1Class); | ||
var base1Instance = new Base1DeriveClass(); | ||
@@ -117,3 +118,3 @@ var Base2Class = /** @class */ (function (_super) { | ||
}(Base1Class)); | ||
var StateFilterClass = stateFilterDecorator_1.addStateFilter(Base2Class); | ||
var StateFilterClass = stateFilterDecorator_1.default(Base2Class); | ||
var stateFilterInstance = new StateFilterClass(); | ||
@@ -128,3 +129,3 @@ base1Instance._initWrap(); | ||
test('addStateFilterForMap', function () { | ||
var Base1DeriveClass = stateFilterDecorator_1.addStateFilter(Base1Class); | ||
var Base1DeriveClass = stateFilterDecorator_1.default(Base1Class); | ||
var base1Instance = new Base1DeriveClass(); | ||
@@ -141,3 +142,3 @@ var base2Instance = new Base1DeriveClass(); | ||
}(Base1Class)); | ||
var StateFilterClass = stateFilterDecorator_1.addStateFilterForMap(Base2Class); | ||
var StateFilterClass = stateFilterMapDecorator_1.default(Base2Class); | ||
var stateFilterInstance = new StateFilterClass(); | ||
@@ -172,3 +173,3 @@ base1Instance._initWrap(); | ||
var _a, _b; | ||
var Base1DeriveClass = stateFilterDecorator_1.addStateFilter(Base1Class); | ||
var Base1DeriveClass = stateFilterDecorator_1.default(Base1Class); | ||
var base1Instance = new Base1DeriveClass(); | ||
@@ -185,3 +186,3 @@ var base2Instance = new Base1DeriveClass(); | ||
}(Base1Class)); | ||
var StateFilterClass = stateFilterDecorator_1.addStateFilterForMap(Base2Class); | ||
var StateFilterClass = stateFilterMapDecorator_1.default(Base2Class); | ||
var stateFilterInstance = new StateFilterClass(); | ||
@@ -230,3 +231,3 @@ base1Instance._initWrap(); | ||
test('addStateFilterForMap for defaultFilter option', function () { | ||
var Base1DeriveClass = stateFilterDecorator_1.addStateFilter(Base1Class); | ||
var Base1DeriveClass = stateFilterDecorator_1.default(Base1Class); | ||
var base1Instance = new Base1DeriveClass(); | ||
@@ -242,3 +243,3 @@ var base2Instance = new Base1DeriveClass(); | ||
} | ||
Base2Class.prototype.add = function (key, prevInit) { | ||
Base2Class.prototype.add = function (key) { | ||
var newStore = new Base1DeriveClass(); | ||
@@ -251,3 +252,3 @@ newStore._initWrap(); | ||
}(Base1Class)); | ||
var StateFilterClass = stateFilterDecorator_1.addStateFilterForMap(Base2Class); | ||
var StateFilterClass = stateFilterMapDecorator_1.default(Base2Class); | ||
var stateFilterInstance = new StateFilterClass(); | ||
@@ -254,0 +255,0 @@ base1Instance._initWrap(); |
@@ -1,2 +0,6 @@ | ||
export default function filterApply(origin: any, updated: any, deleted: any): {}; | ||
interface IFilterObject { | ||
[key: string]: any; | ||
} | ||
export default function filterApply(origin: IFilterObject, updated: IFilterObject, deleted: IFilterObject | null): IFilterObject; | ||
export {}; | ||
//# sourceMappingURL=filterApply.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* | ||
@@ -31,4 +32,3 @@ Apply the origin state with updated filter and deleted filter | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isObject = require('lodash/isObject'); | ||
var objUtils_1 = require("./objUtils"); | ||
function filterApply(origin, updated, deleted) { | ||
@@ -50,3 +50,3 @@ var merged = {}; | ||
// } | ||
if (isObject(updated)) { | ||
if (objUtils_1.isObject(updated)) { | ||
Object.keys(updated).forEach(function (key) { | ||
@@ -60,3 +60,3 @@ if (key === '*' || key === '*@exclude') | ||
} | ||
if (isObject(deleted)) { | ||
if (objUtils_1.isObject(deleted)) { | ||
Object.keys(deleted).forEach(function (key) { | ||
@@ -63,0 +63,0 @@ merged[key] = undefined; |
@@ -1,6 +0,9 @@ | ||
declare function filterDifference(old: any, curr: any): { | ||
updated: any; | ||
deleted: {}; | ||
interface IFilterObject { | ||
[key: string]: any; | ||
} | ||
declare function filterDifference(old: IFilterObject, curr: IFilterObject): { | ||
updated: IFilterObject; | ||
deleted: IFilterObject; | ||
}; | ||
export default filterDifference; | ||
//# sourceMappingURL=filterDifference.d.ts.map |
@@ -16,5 +16,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isObject = require('lodash/isObject'); | ||
var isEmpty = require('lodash/isEmpty'); | ||
var isShallow = function (val) { return Array.isArray(val) || !isObject(val); }; | ||
var objUtils_1 = require("./objUtils"); | ||
var isShallow = function (val) { return Array.isArray(val) || !objUtils_1.isObject(val); }; | ||
function checkUpdateVal(key, old, curr, updated, deleted) { | ||
@@ -37,4 +36,4 @@ var oldVal = old[key]; | ||
var diff = filterDifference(oldVal, currVal); | ||
!isEmpty(diff.updated) && (updated[key] = diff.updated); | ||
!isEmpty(diff.deleted) && (deleted[key] = diff.deleted); | ||
!objUtils_1.isEmpty(diff.updated) && (updated[key] = diff.updated); | ||
!objUtils_1.isEmpty(diff.deleted) && (deleted[key] = diff.deleted); | ||
} | ||
@@ -41,0 +40,0 @@ } |
@@ -1,3 +0,2 @@ | ||
declare const keys: any; | ||
declare const isObject: any; | ||
export {}; | ||
//# sourceMappingURL=filterObject.d.ts.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
/* | ||
@@ -11,4 +12,4 @@ Given an source object and a filter shape, remove all leaf elements in the shape | ||
*/ | ||
var keys = require('lodash/keys'); | ||
var isObject = require('lodash/isObject'); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var objUtils_1 = require("./objUtils"); | ||
module.exports = function filterObject(source, filter) { | ||
@@ -18,4 +19,4 @@ if (!source || filter === true) | ||
var filtered = {}; | ||
keys(source).forEach(function (key) { | ||
if (isObject(filter[key])) { | ||
Object.keys(source).forEach(function (key) { | ||
if (objUtils_1.isObject(filter[key])) { | ||
filtered[key] = filterObject(source[key], filter[key]); | ||
@@ -22,0 +23,0 @@ } |
@@ -0,6 +1,8 @@ | ||
import IStoreFilters from '../IStoreFilters'; | ||
import { IExtendStoreBaseConstructor } from '../IExtendStoreBase'; | ||
interface FilterStoreOptions { | ||
applyFilter: boolean; | ||
} | ||
declare function filterOneStore(StoreClass: any, filterOptions?: FilterStoreOptions): {}; | ||
export { filterOneStore, }; | ||
export declare function filterOneStore(StoreClass: IExtendStoreBaseConstructor, filterOptions?: FilterStoreOptions): IStoreFilters | null; | ||
export {}; | ||
//# sourceMappingURL=filterStore.d.ts.map |
@@ -33,2 +33,12 @@ "use strict"; | ||
}; | ||
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 }); | ||
@@ -46,3 +56,3 @@ /* | ||
var storeBuilder_1 = require("./storeBuilder"); | ||
var isFunction = require('lodash/isFunction'); | ||
var objUtils_1 = require("./objUtils"); | ||
var storeBuilders = { | ||
@@ -52,6 +62,9 @@ Item: function (StoreClass, storeKey, stateKey, options) { | ||
options = options ? { defaultFilter: options.defaultFilter } : null; | ||
if (this.setStore) { | ||
return this.setStore(storeKey, this.buildStore(StoreClass, args, options)); | ||
var store = this.buildStore(StoreClass, args, options); | ||
if (this.setStore && store) { | ||
return this.setStore(storeKey, store); | ||
} | ||
this[storeKey] = this.buildStore(StoreClass, args, options); | ||
else if (store) { | ||
this[storeKey] = store; | ||
} | ||
}, | ||
@@ -117,3 +130,3 @@ List: function (StoreClass, storeKey, stateKey, options) { | ||
var storeObservers = { | ||
Item: function (storeKey, stateKey) { | ||
Item: function (storeKey, stateKey, options) { | ||
var _this = this; | ||
@@ -149,2 +162,3 @@ var store = this.getStore ? this.getStore(storeKey) : this[storeKey]; | ||
function ExtendStoreClass() { | ||
var _newTarget = this && this instanceof ExtendStoreClass ? this.constructor : void 0; | ||
var args = []; | ||
@@ -154,3 +168,2 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
} | ||
var _newTarget = this && this instanceof ExtendStoreClass ? this.constructor : void 0; | ||
var obj = new (StoreClass.bind.apply(StoreClass, __spread([void 0], args)))(); | ||
@@ -166,28 +179,32 @@ Object.setPrototypeOf(obj, _newTarget.prototype); | ||
} | ||
function parseInnerStores(innerStores) { | ||
if (!innerStores) { | ||
return undefined; | ||
} | ||
if (Array.isArray(innerStores)) { | ||
return innerStores; | ||
} | ||
var storeDeclarer = []; | ||
for (var key in innerStores) { | ||
var value = innerStores[key]; | ||
storeDeclarer.push({ stateKey: key, declarer: value }); | ||
} | ||
return storeDeclarer; | ||
} | ||
function filterOneStore(StoreClass, filterOptions) { | ||
var e_1, _a; | ||
if (!StoreClass) | ||
return null; | ||
var innerStores = StoreClass.innerStores; | ||
if (!innerStores) | ||
var innerStoreArray = parseInnerStores(StoreClass.innerStores); | ||
if (!innerStoreArray) | ||
return null; | ||
var filters = {}; | ||
var subStoreInfos = []; | ||
for (var key in innerStores) { | ||
var value = innerStores[key]; | ||
if (isFunction(value)) { | ||
var storeName = key + 'Store'; | ||
var Store = extendClass(value); | ||
filters[storeName] = { | ||
type: 'Store', | ||
filters: filterOneStore(Store, filterOptions), | ||
}; | ||
subStoreInfos.push(['Item', Store, storeName, key, filterOptions]); | ||
} | ||
else { | ||
var options = value.options, Store = value.Store; | ||
if (filterOptions) | ||
options = __assign({}, options, filterOptions); | ||
var storeName = options && options.storeKey || key + 'Store'; | ||
Store = extendClass(Store); | ||
if (StoreDeclarer_1.StoreDeclarer.isStore(value)) { | ||
try { | ||
for (var innerStoreArray_1 = __values(innerStoreArray), innerStoreArray_1_1 = innerStoreArray_1.next(); !innerStoreArray_1_1.done; innerStoreArray_1_1 = innerStoreArray_1.next()) { | ||
var innerStore = innerStoreArray_1_1.value; | ||
var stateKey = innerStore.stateKey, declarer = innerStore.declarer; | ||
if (objUtils_1.isFunction(declarer)) { | ||
var storeName = stateKey + 'Store'; | ||
var Store = extendClass(declarer); | ||
filters[storeName] = { | ||
@@ -197,22 +214,43 @@ type: 'Store', | ||
}; | ||
subStoreInfos.push(['Item', Store, storeName, key, options]); | ||
subStoreInfos.push(['Item', Store, storeName, stateKey, filterOptions]); | ||
} | ||
else if (StoreDeclarer_1.StoreListDeclarer.isStoreList(value)) { | ||
filters[storeName] = { | ||
type: 'StoreList', | ||
filters: filterOneStore(Store, filterOptions), | ||
}; | ||
subStoreInfos.push(['List', Store, storeName, key, options]); | ||
else { | ||
var _b = declarer, options = _b.options, Store = _b.Store; | ||
var nowOptions = options; | ||
if (filterOptions) { | ||
nowOptions = __assign({}, options, filterOptions); | ||
} | ||
var storeName = nowOptions && nowOptions.storeKey || stateKey + 'Store'; | ||
var extendStore = extendClass(Store); | ||
if (StoreDeclarer_1.StoreDeclarer.isStore(declarer)) { | ||
filters[storeName] = { | ||
type: 'Store', | ||
filters: filterOneStore(extendStore, filterOptions), | ||
}; | ||
subStoreInfos.push(['Item', extendStore, storeName, stateKey, nowOptions]); | ||
} | ||
else if (StoreDeclarer_1.StoreListDeclarer.isStoreList(declarer)) { | ||
filters[storeName] = { | ||
type: 'StoreList', | ||
filters: filterOneStore(extendStore, filterOptions), | ||
}; | ||
subStoreInfos.push(['List', extendStore, storeName, stateKey, nowOptions]); | ||
} | ||
else if (StoreDeclarer_1.StoreMapDeclarer.isStoreMap(declarer)) { | ||
filters[storeName] = { | ||
type: 'StoreMap', | ||
filters: filterOneStore(extendStore, filterOptions), | ||
}; | ||
subStoreInfos.push(['Map', extendStore, storeName, nowOptions && nowOptions.directInsert ? null : stateKey, nowOptions]); | ||
} | ||
} | ||
else if (StoreDeclarer_1.StoreMapDeclarer.isStoreMap(value)) { | ||
filters[storeName] = { | ||
type: 'StoreMap', | ||
filters: filterOneStore(Store, filterOptions), | ||
}; | ||
if (options && options.directInsert) | ||
key = null; | ||
subStoreInfos.push(['Map', Store, storeName, key, options]); | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (innerStoreArray_1_1 && !innerStoreArray_1_1.done && (_a = innerStoreArray_1.return)) _a.call(innerStoreArray_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
StoreClass.prototype.buildStores = function () { | ||
@@ -271,10 +309,12 @@ var _this = this; | ||
exports.filterOneStore = filterOneStore; | ||
function filterStore(stores) { | ||
var storeFilters = {}; | ||
for (var key in stores) { | ||
var store = stores[key]; | ||
storeFilters[key] = { type: 'Store', filters: filterOneStore(store.constructor) }; | ||
} | ||
return storeFilters; | ||
} | ||
; | ||
// function filterStore(stores) { | ||
// let storeFilters = {}; | ||
// for (let key in stores) { | ||
// let store = stores[key]; | ||
// storeFilters[key] = { type: 'Store', filters: filterOneStore(store.constructor) }; | ||
// } | ||
// return storeFilters; | ||
// }; | ||
// export { | ||
// filterOneStore, | ||
// }; |
declare const _default: { | ||
new (...args: any[]): { | ||
[x: string]: any; | ||
_stateListeners: {}; | ||
_stateFilters: {}; | ||
_filterDisposables: {}; | ||
new (keys: string[], builder: () => import("../IExtendStoreBase").default, observer: (store: import("../IExtendStoreBase").default, index: string) => void, options: any): { | ||
_stateListeners: { | ||
[clientIdKey: string]: number; | ||
}; | ||
_stateFilters: { | ||
[clientId: string]: any; | ||
}; | ||
_filterDisposables: { | ||
[key: string]: import("event-kit").Disposable; | ||
}; | ||
_stateFiltersInit: boolean; | ||
getDefaultFilter(): { | ||
'*': boolean; | ||
[key: string]: any; | ||
}; | ||
_initForClientId: (clientId: any) => void; | ||
_initForClientId: (clientId: string) => void; | ||
_initStateFilters(): void; | ||
_initWrap(): void; | ||
_setFilter(clientId: any, newFilter: any): void; | ||
_handleAddWin(clientId: any): void; | ||
_handleRemoveWin(clientId: any): void; | ||
listenForKeys: (clientId: string, key: string | number | string[] | number[]) => void; | ||
unlistenForKeys: (clientId: string, key: string | number | string[] | number[]) => void; | ||
add(key: any, prevInit: any): any; | ||
deleteFilter(key: any): void; | ||
delete(key: any): void; | ||
_setFilter(clientId: string, newFilter: any): void; | ||
_handleAddWin(clientId: string): void; | ||
_handleRemoveWin(clientId: string): void; | ||
listenForKeys: (this: any, clientId: string, key: string | string[]) => void; | ||
unlistenForKeys: (this: any, clientId: string, key: string | string[]) => void; | ||
add(key: string, prevInit: (store: import("../IExtendStoreBase").default) => void): any; | ||
deleteFilter(key: string): void; | ||
delete(key: string): void; | ||
clear(): void; | ||
dispose(): void; | ||
storeMap: Map<string, any>; | ||
disposables: Map<any, any>; | ||
options: any; | ||
builder: () => import("../IExtendStoreBase").default; | ||
observer: any; | ||
parentStore: any; | ||
appStores: any; | ||
emitter: import("event-kit").Emitter<{ | ||
[key: string]: any; | ||
}, {}>; | ||
forEach(callback: (value: any, key: string, map: Map<string, any>) => void): void; | ||
get(key: string): any; | ||
has(key: string): boolean; | ||
keys(): IterableIterator<string>; | ||
values(): IterableIterator<any>; | ||
entries(): IterableIterator<[string, any]>; | ||
}; | ||
[x: string]: any; | ||
}; | ||
export default _default; | ||
//# sourceMappingURL=FilterStoreMap.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var stateFilterDecorator_1 = require("./stateFilterDecorator"); | ||
var stateFilterMapDecorator_1 = require("./stateFilterMapDecorator"); | ||
var StoreMap_1 = require("./StoreMap"); | ||
exports.default = stateFilterDecorator_1.addStateFilterForMap(StoreMap_1.default); | ||
exports.default = stateFilterMapDecorator_1.default(StoreMap_1.default); |
@@ -1,5 +0,6 @@ | ||
declare function filterWindowStore(storeFilters: any, winStoreKey: any, winId: any): any; | ||
declare function filterWindowState(allState: any, winStateKey: any, winId: any): any; | ||
declare function filterWindowDelta(updated: any, deleted: any, winStateKey: any, winId: any): any[]; | ||
import IStoreFilters from "../IStoreFilters"; | ||
declare function filterWindowStore(storeFilters: IStoreFilters, winStoreKey: string, winId: string): any; | ||
declare function filterWindowState(allState: any, winStateKey: string, winId: string): any; | ||
declare function filterWindowDelta(updated: any, deleted: any, winStateKey: string, winId: string): any[]; | ||
export { filterWindowStore, filterWindowState, filterWindowDelta, }; | ||
//# sourceMappingURL=filterWindowStore.d.ts.map |
@@ -0,0 +0,0 @@ "use strict"; |
declare function objectDifference(old: any, curr: any): { | ||
updated: {}; | ||
deleted: {}; | ||
updated: { | ||
[key: string]: any; | ||
}; | ||
deleted: { | ||
[key: string]: any; | ||
}; | ||
}; | ||
export default objectDifference; | ||
//# sourceMappingURL=objectDifference.d.ts.map |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -47,3 +47,4 @@ "use strict"; | ||
else { | ||
var merged_1, deleteKeys = void 0; | ||
var merged_1; | ||
var deleteKeys = void 0; | ||
if (isObject(deleted)) { | ||
@@ -50,0 +51,0 @@ merged_1 = {}; |
@@ -1,59 +0,60 @@ | ||
export declare const addStateFilter: (StoreClass: any) => { | ||
import { Emitter, Disposable } from 'event-kit'; | ||
import { StoreBaseConstructor } from '../StoreBase'; | ||
import IStoresDeclarer from '../IStoresDeclarer'; | ||
import IExtendStoreBase, { IExtendStoreBaseConstructor } from '../IExtendStoreBase'; | ||
export default function (StoreClass: StoreBaseConstructor): { | ||
new (...args: any[]): { | ||
[x: string]: any; | ||
_stateListeners: {}; | ||
_stateFilters: {}; | ||
_stateListeners: { | ||
[key: string]: any; | ||
}; | ||
_stateFilters: { | ||
[key: string]: any; | ||
}; | ||
_stateFiltersInit: boolean; | ||
appStores: any; | ||
getDefaultFilter(): { | ||
'*': boolean; | ||
[key: string]: any; | ||
}; | ||
_initForClientId: (clientId: any) => void; | ||
_initForClientId: (clientId: string) => void; | ||
_initStateFilters(): void; | ||
_initWrap(): void; | ||
_handleAddWin(clientId: any): void; | ||
_handleRemoveWin(clientId: any): void; | ||
_setFilter(clientId: any, newFilter: any): void; | ||
listen: (clientId: string) => void; | ||
unlisten: (clientId: string) => void; | ||
}; | ||
[x: string]: any; | ||
innerStores: any; | ||
}; | ||
declare type KeyType = string | number | string[] | number[]; | ||
export interface IFilterStoreMap { | ||
listenForKeys(clientId: string, key: KeyType): void; | ||
unlistenForKeys(clientId: string, key: KeyType): void; | ||
add(key: string, prevInit?: Function): any; | ||
delete(key: string): void; | ||
clear(): void; | ||
dispose(): void; | ||
} | ||
export declare function addStateFilterForMap(StoreClass: any): { | ||
new (...args: any[]): { | ||
[x: string]: any; | ||
_stateListeners: {}; | ||
_stateFilters: {}; | ||
_filterDisposables: {}; | ||
_stateFiltersInit: boolean; | ||
getDefaultFilter(): { | ||
'*': boolean; | ||
}; | ||
_initForClientId: (clientId: any) => void; | ||
_initStateFilters(): void; | ||
_initWrap(): void; | ||
_setFilter(clientId: any, newFilter: any): void; | ||
_handleAddWin(clientId: any): void; | ||
_handleRemoveWin(clientId: any): void; | ||
listenForKeys: (clientId: string, key: KeyType) => void; | ||
unlistenForKeys: (clientId: string, key: KeyType) => void; | ||
add(key: any, prevInit: any): any; | ||
deleteFilter(key: any): void; | ||
delete(key: any): void; | ||
clear(): void; | ||
_handleAddWin(clientId: string): void; | ||
_handleRemoveWin(clientId: string): void; | ||
_setFilter(clientId: string, newFilter: any): void; | ||
listen: (this: any, clientId: string) => void; | ||
unlisten: (this: any, clientId: string) => void; | ||
buildStores(): void; | ||
initStores(parentStore?: IExtendStoreBase): void; | ||
startObserve(): void; | ||
disposeStores(): void; | ||
getSubStores(): IExtendStoreBase[]; | ||
getSubStoreInfos(): ["Item" | "List" | "Map", IExtendStoreBaseConstructor, string, string, any][]; | ||
setStore?(storeKey: string, store: IExtendStoreBase | import("./StoreList").default | import("./StoreMap").default): void; | ||
getStore?(storeKey: string): IExtendStoreBase | import("./StoreList").default | import("./StoreMap").default; | ||
getStores?(): IExtendStoreBase[]; | ||
handleFilterChange?(): void; | ||
_appStore: import("../AppStore").default; | ||
options: any; | ||
parentStore: import("../StoreBase").default; | ||
state: any; | ||
emitter: Emitter<{ | ||
[key: string]: any; | ||
}, {}>; | ||
inWillUpdate: boolean; | ||
willUpdateStates: any[]; | ||
_isInit: boolean; | ||
__FLUX_STORE__: boolean; | ||
willInit(): void; | ||
init(): void; | ||
buildStore(storeClass: IExtendStoreBaseConstructor, args: any[], options?: any): IExtendStoreBase; | ||
setState(state: any): void; | ||
onDidUpdate(callback: (state: any) => void): Disposable; | ||
onWillUpdate(callback: (state: any) => void): Disposable; | ||
observe(callback: (state: any) => void): Disposable; | ||
dispose(): void; | ||
destroy(): void; | ||
getState(): any; | ||
}; | ||
[x: string]: any; | ||
innerStores: IStoresDeclarer; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=stateFilterDecorator.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -27,43 +27,10 @@ extendStatics(d, b); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
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 omit = require('lodash/omit'); | ||
exports.addStateFilter = function (StoreClass) { | ||
var objUtils_1 = require("./objUtils"); | ||
function default_1(StoreClass) { | ||
var ExtendStoreClass = StoreClass; | ||
return /** @class */ (function (_super) { | ||
__extends(MainStoreBase, _super); | ||
function MainStoreBase() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var _this = _super.apply(this, __spread(args)) || this; | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this._stateListeners = {}; | ||
@@ -83,3 +50,3 @@ _this._stateFilters = {}; | ||
else { | ||
clientFilters = Object.assign(clientFilters, omit(storeFilter, '*')); | ||
clientFilters = Object.assign(clientFilters, objUtils_1.omit(storeFilter, '*')); | ||
} | ||
@@ -119,3 +86,3 @@ }); | ||
// Init the state filters for the window with clientId | ||
var winManagerStore = (this.appStores || this.stores).winManagerStore; | ||
var winManagerStore = (this.appStores || this.getSubStores).winManagerStore; | ||
winManagerStore.getClienIds().forEach(this._initForClientId); | ||
@@ -131,4 +98,4 @@ this._stateFiltersInit = true; | ||
subStore.emitter.on('did-filter-update', function (_a) { | ||
var _b; | ||
var clientId = _a.clientId, filters = _a.filters; | ||
var _b; | ||
if (stateKey) { | ||
@@ -138,3 +105,3 @@ _this._setFilter(clientId, (_b = {}, _b[stateKey] = filters, _b)); | ||
else { | ||
_this._setFilter(clientId, omit(filters, '*')); | ||
_this._setFilter(clientId, objUtils_1.omit(filters, '*')); | ||
} | ||
@@ -187,241 +154,4 @@ }); | ||
return MainStoreBase; | ||
}(StoreClass)); | ||
}; | ||
function addStateFilterForMap(StoreClass) { | ||
return /** @class */ (function (_super) { | ||
__extends(MainStoreBase, _super); | ||
function MainStoreBase() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var _this = _super.apply(this, __spread(args)) || this; | ||
_this._stateListeners = {}; | ||
_this._stateFilters = {}; | ||
_this._filterDisposables = {}; | ||
_this._stateFiltersInit = false; // Check if or not the stateFilters has init | ||
_this._initForClientId = function (clientId) { | ||
var e_1, _a; | ||
var defaultFilter = _this.options && _this.options.defaultFilter; | ||
var clientFilters = _this.getDefaultFilter(); | ||
if (defaultFilter) { | ||
var entries = _this.storeMap.entries(); | ||
try { | ||
for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) { | ||
var _b = __read(entries_1_1.value, 2), key = _b[0], store = _b[1]; | ||
clientFilters[key] = store._stateFilters && store._stateFilters[clientId] || { '*': true }; | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
} | ||
_this._stateFilters[clientId] = clientFilters; | ||
}; | ||
_this.listenForKeys = function (clientId, key) { | ||
var _this = this; | ||
if (!clientId) | ||
return console.error('The clientId is not specify'); | ||
var keys = Array.isArray(key) ? key : [key]; | ||
var _stateListeners = this._stateListeners; | ||
keys.forEach(function (key) { | ||
var _a; | ||
var saveKey = clientId + key; | ||
if (_stateListeners[saveKey] == null) | ||
_stateListeners[saveKey] = 0; | ||
_stateListeners[saveKey] += 1; | ||
if (_stateListeners[saveKey] === 1) { | ||
var store = _this.storeMap.get(key); | ||
var storeFilter = store._stateFilters && store._stateFilters[clientId] || { '*': true }; | ||
_this._setFilter(clientId, (_a = {}, _a[key] = storeFilter, _a)); | ||
if (_this._filterDisposables[saveKey]) { | ||
console.error("The " + key + " for " + clientId + " has listened, This May be Bugs"); | ||
} | ||
_this._filterDisposables[saveKey] = store.emitter.on('did-filter-update', function (_a) { | ||
var nowId = _a.clientId, filters = _a.filters; | ||
var _b; | ||
if (nowId === clientId) | ||
_this._setFilter(clientId, (_b = {}, _b[key] = filters, _b)); | ||
}); | ||
} | ||
}); | ||
}; | ||
_this.unlistenForKeys = function (clientId, key) { | ||
var _this = this; | ||
if (!clientId) | ||
return console.error('The clientId is not specify'); | ||
var keys = Array.isArray(key) ? key : [key]; | ||
var _stateListeners = this._stateListeners; | ||
keys.forEach(function (key) { | ||
var _a; | ||
var saveKey = clientId + key; | ||
_stateListeners[saveKey] -= 1; | ||
if (_stateListeners[saveKey] === 0) { | ||
_this._setFilter(clientId, (_a = {}, _a[key] = false, _a)); | ||
_this._filterDisposables[saveKey].dispose(); | ||
_this._filterDisposables[saveKey] = null; | ||
} | ||
}); | ||
}; | ||
return _this; | ||
} | ||
MainStoreBase.prototype.getDefaultFilter = function () { | ||
// defaultFilter=true 表示默认的*为true,并且将observe所有key | ||
if (this.options && this.options.defaultFilter) | ||
return { '*': true }; | ||
return { '*': false }; | ||
}; | ||
MainStoreBase.prototype._initStateFilters = function () { | ||
var _this = this; | ||
var e_2, _a; | ||
var defaultFilter = this.options && this.options.defaultFilter; | ||
var winManagerStore = this.appStores.winManagerStore; | ||
winManagerStore.getClienIds().forEach(this._initForClientId); | ||
this._stateFiltersInit = true; | ||
// winManagerStore.onDidAddWin(initForClientId); | ||
// winManagerStore.onDidRemoveWin((clientId) => this._stateFilters[clientId] = null); | ||
if (defaultFilter) { | ||
var _loop_1 = function (key, store) { | ||
// clientFilters[key] = defaultFilter ? store._stateFilters && store._stateFilters[clientId] || { '*': true } : { '*': false }; | ||
this_1._filterDisposables[key] = store.emitter.on('did-filter-update', function (_a) { | ||
var clientId = _a.clientId, filters = _a.filters; | ||
var _b; | ||
_this._setFilter(clientId, (_b = {}, _b[key] = filters, _b)); | ||
}); | ||
}; | ||
var this_1 = this; | ||
try { | ||
for (var _b = __values(this.storeMap.entries()), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var _d = __read(_c.value, 2), key = _d[0], store = _d[1]; | ||
_loop_1(key, store); | ||
} | ||
} | ||
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; } | ||
} | ||
} | ||
}; | ||
MainStoreBase.prototype._initWrap = function () { | ||
this._initStateFilters(); | ||
_super.prototype._initWrap.call(this); | ||
}; | ||
MainStoreBase.prototype._setFilter = function (clientId, newFilter) { | ||
var oldFilters = this._stateFilters[clientId] || this.getDefaultFilter(); | ||
var nextFilters = __assign({}, oldFilters, newFilter); | ||
this._stateFilters[clientId] = nextFilters; | ||
this.emitter.emit('did-filter-update', { clientId: clientId, filters: nextFilters }); | ||
}; | ||
MainStoreBase.prototype._handleAddWin = function (clientId) { | ||
var e_3, _a; | ||
if (this._stateFiltersInit) { | ||
var entries = this.storeMap.entries(); | ||
try { | ||
for (var entries_2 = __values(entries), entries_2_1 = entries_2.next(); !entries_2_1.done; entries_2_1 = entries_2.next()) { | ||
var _b = __read(entries_2_1.value, 2), key = _b[0], store = _b[1]; | ||
store._handleAddWin && store._handleAddWin(clientId); | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (entries_2_1 && !entries_2_1.done && (_a = entries_2.return)) _a.call(entries_2); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
this._initForClientId(clientId); | ||
} | ||
}; | ||
MainStoreBase.prototype._handleRemoveWin = function (clientId) { | ||
var e_4, _a; | ||
if (this._stateFiltersInit) { | ||
this._stateFilters[clientId] = null; | ||
var entries = this.storeMap.entries(); | ||
try { | ||
for (var entries_3 = __values(entries), entries_3_1 = entries_3.next(); !entries_3_1.done; entries_3_1 = entries_3.next()) { | ||
var _b = __read(entries_3_1.value, 2), key = _b[0], store = _b[1]; | ||
this._stateListeners[clientId + key] = 0; | ||
store._handleRemoveWin && store._handleRemoveWin(clientId); | ||
} | ||
} | ||
catch (e_4_1) { e_4 = { error: e_4_1 }; } | ||
finally { | ||
try { | ||
if (entries_3_1 && !entries_3_1.done && (_a = entries_3.return)) _a.call(entries_3); | ||
} | ||
finally { if (e_4) throw e_4.error; } | ||
} | ||
} | ||
}; | ||
MainStoreBase.prototype.add = function (key, prevInit) { | ||
var _this = this; | ||
var newStore = _super.prototype.add.call(this, key, prevInit); | ||
var defaultFilter = this.options && this.options.defaultFilter; | ||
if (defaultFilter) { | ||
Object.keys(this._stateFilters).forEach(function (clientId) { | ||
var _a; | ||
var filters = newStore._stateFilters && newStore._stateFilters[clientId] || { '*': true }; | ||
_this._setFilter(clientId, (_a = {}, _a[key] = filters, _a)); | ||
}); | ||
if (this._filterDisposables[key]) | ||
console.error("The key " + key + " should NOT add twice"); | ||
this._filterDisposables[key] = newStore.emitter.on('did-filter-update', function (_a) { | ||
var clientId = _a.clientId, filters = _a.filters; | ||
var _b; | ||
_this._setFilter(clientId, (_b = {}, _b[key] = filters, _b)); | ||
}); | ||
} | ||
return newStore; | ||
}; | ||
MainStoreBase.prototype.deleteFilter = function (key) { | ||
var _this = this; | ||
var store = this.storeMap.get(key); | ||
Object.keys(this._stateFilters).forEach(function (clientId) { | ||
var _a; | ||
_this._setFilter(clientId, (_a = {}, _a[key] = null, _a)); | ||
}); | ||
if (this._filterDisposables[key]) { | ||
this._filterDisposables[key].dispose(); | ||
this._filterDisposables[key] = null; | ||
} | ||
}; | ||
MainStoreBase.prototype.delete = function (key) { | ||
this.deleteFilter(key); | ||
_super.prototype.delete.call(this, key); | ||
}; | ||
MainStoreBase.prototype.clear = function () { | ||
var e_5, _a; | ||
var keys = this.storeMap.keys(); | ||
try { | ||
for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) { | ||
var key = keys_1_1.value; | ||
this.deleteFilter(key); | ||
} | ||
} | ||
catch (e_5_1) { e_5 = { error: e_5_1 }; } | ||
finally { | ||
try { | ||
if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1); | ||
} | ||
finally { if (e_5) throw e_5.error; } | ||
} | ||
_super.prototype.clear.call(this); | ||
}; | ||
MainStoreBase.prototype.dispose = function () { | ||
_super.prototype.dispose.call(this); | ||
for (var key in this._filterDisposables) { | ||
var disposable = this._filterDisposables[key]; | ||
disposable && disposable.dispose(); | ||
} | ||
}; | ||
return MainStoreBase; | ||
}(StoreClass)); | ||
}(ExtendStoreClass)); | ||
} | ||
exports.addStateFilterForMap = addStateFilterForMap; | ||
exports.default = default_1; |
@@ -1,4 +0,5 @@ | ||
export declare function beforeInit(store: any, parentStore: any): void; | ||
export declare const initStore: (store: any, parentStore: any) => void; | ||
export declare const disposeStore: (store: any) => void; | ||
import IExtendStoreBase from "../IExtendStoreBase"; | ||
export declare function beforeInit(store: IExtendStoreBase, parentStore: IExtendStoreBase): void; | ||
export declare const initStore: (store: IExtendStoreBase, parentStore: IExtendStoreBase) => void; | ||
export declare const disposeStore: (store: IExtendStoreBase) => void; | ||
//# sourceMappingURL=storeBuilder.d.ts.map |
@@ -0,22 +1,29 @@ | ||
import { Emitter, Disposable } from 'event-kit'; | ||
import IExtendStoreBase from '../IExtendStoreBase'; | ||
declare type StoreBuilder = () => IExtendStoreBase; | ||
declare type StoreObserver = (store: IExtendStoreBase, i: number) => Disposable; | ||
export default class StoreList { | ||
length: number; | ||
storeArray: any[]; | ||
disposables: any[]; | ||
storeArray: IExtendStoreBase[]; | ||
disposables: Disposable[]; | ||
options: any; | ||
builder: any; | ||
observer: any; | ||
builder: StoreBuilder; | ||
observer: StoreObserver; | ||
parentStore: any; | ||
appStores: any; | ||
emitter: any; | ||
constructor(size: any, builder: any, observer: any, options: any); | ||
emitter: Emitter<{ | ||
[key: string]: any; | ||
}, {}>; | ||
constructor(size: number, builder: StoreBuilder, observer: StoreObserver, options: any); | ||
_initWrap(): void; | ||
setSize(count: any): void; | ||
setSize(count: number): void; | ||
dispose(): void; | ||
forEach(callback: any): void; | ||
map(callback: any): {}[]; | ||
filter(callback: any): {}[]; | ||
get(index: any): any; | ||
slice(begin: any, end: any): any[]; | ||
indexOf(item: any): number; | ||
forEach(callback: (value: IExtendStoreBase, index: number, array: IExtendStoreBase[]) => void): void; | ||
map(callback: (value: IExtendStoreBase, index: number, array: IExtendStoreBase[]) => any): any[]; | ||
filter(callback: (value: IExtendStoreBase, index: number, array: IExtendStoreBase[]) => boolean): IExtendStoreBase[]; | ||
get(index: number): IExtendStoreBase; | ||
slice(begin: number, end: number): IExtendStoreBase[]; | ||
indexOf(item: IExtendStoreBase): number; | ||
} | ||
export {}; | ||
//# sourceMappingURL=StoreList.d.ts.map |
@@ -5,3 +5,2 @@ "use strict"; | ||
var event_kit_1 = require("event-kit"); | ||
// import { addStateFilter } from './stateFilterDecorator'; | ||
var StoreList = /** @class */ (function () { | ||
@@ -49,5 +48,11 @@ function StoreList(size, builder, observer, options) { | ||
}; | ||
StoreList.prototype.forEach = function (callback) { return this.storeArray.forEach(callback); }; | ||
StoreList.prototype.map = function (callback) { return this.storeArray.map(callback); }; | ||
StoreList.prototype.filter = function (callback) { return this.storeArray.filter(callback); }; | ||
StoreList.prototype.forEach = function (callback) { | ||
return this.storeArray.forEach(callback); | ||
}; | ||
StoreList.prototype.map = function (callback) { | ||
return this.storeArray.map(callback); | ||
}; | ||
StoreList.prototype.filter = function (callback) { | ||
return this.storeArray.filter(callback); | ||
}; | ||
StoreList.prototype.get = function (index) { return this.storeArray[index]; }; | ||
@@ -54,0 +59,0 @@ StoreList.prototype.slice = function (begin, end) { return this.storeArray.slice(begin, end); }; |
@@ -0,23 +1,33 @@ | ||
import { Emitter } from 'event-kit'; | ||
import IExtendStoreBase from '../IExtendStoreBase'; | ||
declare type StoreBuilder = () => IExtendStoreBase | undefined; | ||
declare type StoreMapObserver = (store: IExtendStoreBase, index: string) => void; | ||
export interface StoreMapConstructor { | ||
new (keys: string[] | null, builder: StoreBuilder, observer: StoreMapObserver, options: any): StoreMap; | ||
} | ||
export default class StoreMap { | ||
storeMap: Map<any, any>; | ||
storeMap: Map<string, any>; | ||
disposables: Map<any, any>; | ||
options: any; | ||
builder: any; | ||
builder: StoreBuilder; | ||
observer: any; | ||
parentStore: any; | ||
appStores: any; | ||
emitter: any; | ||
constructor(keys: any, builder: any, observer: any, options: any); | ||
emitter: Emitter<{ | ||
[key: string]: any; | ||
}, {}>; | ||
constructor(keys: string[] | null, builder: StoreBuilder, observer: StoreMapObserver, options: any); | ||
_initWrap(): void; | ||
add(key: any, prevInit: any): any; | ||
delete(key: any): void; | ||
add(key: string, prevInit?: (store: IExtendStoreBase) => void): IExtendStoreBase | undefined; | ||
delete(key: string): void; | ||
clear(): void; | ||
dispose(): void; | ||
forEach(callback: any): void; | ||
get(key: any): any; | ||
has(key: any): boolean; | ||
keys(): IterableIterator<any>; | ||
forEach(callback: (value: any, key: string, map: Map<string, any>) => void): void; | ||
get(key: string): any; | ||
has(key: string): boolean; | ||
keys(): IterableIterator<string>; | ||
values(): IterableIterator<any>; | ||
entries(): IterableIterator<[any, any]>; | ||
entries(): IterableIterator<[string, any]>; | ||
} | ||
export {}; | ||
//# sourceMappingURL=StoreMap.d.ts.map |
@@ -26,3 +26,3 @@ "use strict"; | ||
if (Array.isArray(keys)) | ||
keys.forEach(function (key) { return _this.add(key, null); }); | ||
keys.forEach(function (key) { return _this.add(key); }); | ||
} | ||
@@ -36,9 +36,11 @@ StoreMap.prototype._initWrap = function () { | ||
var newStore = this.builder(); | ||
newStore.mapStoreKey = key; | ||
prevInit && prevInit(newStore); | ||
// if (this._isInit) initStore(newStore); | ||
storeBuilder_1.initStore(newStore, this.parentStore); | ||
this.storeMap.set(key, newStore); | ||
this.disposables.set(key, this.observer(newStore, key)); | ||
return newStore; | ||
if (newStore) { | ||
newStore.mapStoreKey = key; | ||
prevInit && prevInit(newStore); | ||
// if (this._isInit) initStore(newStore); | ||
storeBuilder_1.initStore(newStore, this.parentStore); | ||
this.storeMap.set(key, newStore); | ||
this.disposables.set(key, this.observer(newStore, key)); | ||
return newStore; | ||
} | ||
}; | ||
@@ -89,3 +91,5 @@ StoreMap.prototype.delete = function (key) { | ||
}; | ||
StoreMap.prototype.forEach = function (callback) { return this.storeMap.forEach(callback); }; | ||
StoreMap.prototype.forEach = function (callback) { | ||
return this.storeMap.forEach(callback); | ||
}; | ||
StoreMap.prototype.get = function (key) { return this.storeMap.get(key); }; | ||
@@ -92,0 +96,0 @@ StoreMap.prototype.has = function (key) { return this.storeMap.has(key); }; |
interface ListStorePathUnit { | ||
index: string; | ||
index?: string; | ||
name: string; | ||
@@ -18,7 +18,12 @@ type: 'Map' | 'List'; | ||
}; | ||
declare type Stores = { | ||
[storeKey: string]: any; | ||
}; | ||
export default class StoreProxyHandler { | ||
storeProxyCache: Map<any, any>; | ||
genProxy(storePath: any, forwardStore: any, forwarder: any): any; | ||
genIndexProxy(storePath: any, childFilters: any, forwarder: any): any; | ||
proxyStore(parentStore: StorePathUnit[], storeFilters: StoreFilters, forwarder: Function): {}; | ||
genProxy(storePath: StorePathUnit[], forwardStore: Stores | null, forwarder: Function): Function; | ||
genIndexProxy(storePath: StorePathUnit[], childFilters: { | ||
[key: string]: StoreShape; | ||
}, forwarder: Function): Function; | ||
proxyStore(parentStore: StorePathUnit[], storeFilters: StoreFilters, forwarder: Function): Stores | null; | ||
proxyStores(storeFilters: StoreFilters, forwarder: Function): any; | ||
@@ -25,0 +30,0 @@ } |
@@ -125,3 +125,3 @@ "use strict"; | ||
var type = storeInfo.type, filters = storeInfo.filters, _a = storeInfo.path, path = _a === void 0 ? parentStore : _a; | ||
var names = void 0; | ||
var names = []; | ||
if (type === 'Store') { | ||
@@ -128,0 +128,0 @@ names = __spread(path, [name_1]); |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -21,3 +21,5 @@ extendStatics(d, b); | ||
function WinSpecStoreBase() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.clientId = 'mainClient'; | ||
return _this; | ||
} | ||
@@ -24,0 +26,0 @@ return WinSpecStoreBase; |
import * as React from 'react'; | ||
export default function withState(stateSelector: any, storeSelector: any): (Component: any) => { | ||
declare type SelectorFunc = (state: any, props: any) => any; | ||
export default function withState(stateSelector: string[] | SelectorFunc, storeSelector: string[] | SelectorFunc): (Component: React.ComponentClass<{}, any>) => { | ||
new (props: Readonly<any>): { | ||
@@ -55,2 +56,3 @@ render(): JSX.Element; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=withState.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -30,3 +30,3 @@ extendStatics(d, b); | ||
var Provider_1 = require("./Provider"); | ||
var utils_1 = require("event-flux/lib/utils"); | ||
var objUtils_1 = require("./utils/objUtils"); | ||
var notUsed = function () { return null; }; | ||
@@ -39,3 +39,3 @@ function unifySelector(selector) { | ||
selector = [selector]; | ||
return function (state) { return utils_1.pick(state, selector); }; | ||
return function (state) { return objUtils_1.pick(state, selector); }; | ||
} | ||
@@ -45,4 +45,4 @@ return selector; | ||
function withState(stateSelector, storeSelector) { | ||
storeSelector = unifySelector(storeSelector); | ||
stateSelector = unifySelector(stateSelector); | ||
var storeSelectorFn = unifySelector(storeSelector); | ||
var stateSelectorFn = unifySelector(stateSelector); | ||
return function (Component) { | ||
@@ -56,3 +56,3 @@ return /** @class */ (function (_super) { | ||
var props = this.props; | ||
return (React.createElement(Provider_1.StoreContext.Consumer, null, function (value) { return React.createElement(Component, __assign({}, props, storeSelector(value.stores, props), stateSelector(value.state, props))); })); | ||
return (React.createElement(Provider_1.StoreContext.Consumer, null, function (value) { return React.createElement(Component, __assign({}, props, storeSelectorFn(value.stores, props), stateSelectorFn(value.state, props))); })); | ||
}; | ||
@@ -59,0 +59,0 @@ return Injector; |
@@ -0,0 +0,0 @@ The MIT License (MIT) |
{ | ||
"name": "electron-event-flux", | ||
"version": "1.3.9", | ||
"version": "2.0.1", | ||
"description": "Redux store which synchronizes between instances in multiple process", | ||
@@ -35,9 +35,9 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"electron-store": "^2.0.0", | ||
"event-flux": "^0.10.3", | ||
"electron-store": "^4.0.0", | ||
"event-kit": "^2.5.3", | ||
"immutable": "^3.8.2", | ||
"json-immutable-bn": "^0.4.3", | ||
"lodash": "^4.13.1" | ||
"json-immutable-bn": "^0.4.4" | ||
}, | ||
"devDependencies": { | ||
"@types/event-kit": "^2.4.0", | ||
"@types/jest": "^23.3.10", | ||
@@ -52,2 +52,3 @@ "@types/node": "^10.12.10", | ||
"del": "^3.0.0", | ||
"electron": "^4.0.5", | ||
"eslint": "^1.10.3", | ||
@@ -65,8 +66,8 @@ "eslint-config-airbnb": "^2.1.1", | ||
"ts-jest": "^23.10.5", | ||
"typescript": "^3.1.6", | ||
"electron": "^4.0.5" | ||
"typescript": "^3.5.3" | ||
}, | ||
"peerDependencies": { | ||
"electron-store": "^2.0.0", | ||
"react": ">16.0.0" | ||
} | ||
} |
@@ -0,0 +0,0 @@ # redux-electron-store |
@@ -1,10 +0,11 @@ | ||
import StoreBase from 'event-flux/lib/StoreBase'; | ||
import StoreBase from './StoreBase'; | ||
import IMultiWinStore from './IMultiWinStore'; | ||
const { winManagerStoreName, winManagerKey } = require('./constants'); | ||
export default class ActionRecordStore extends StoreBase { | ||
clientId: string; | ||
clientId: string = ''; | ||
setAction(action) { | ||
this.appStores.multiWinStore.actionChanged(this.clientId, action); | ||
setAction(action: string) { | ||
(this.appStores.multiWinStore as any).onChangeAction(this.clientId, action); | ||
} | ||
} |
const findIndex = require('lodash/findIndex'); | ||
const { | ||
import { Log } from "./utils/loggerApply"; | ||
import IMainClientCallbacks from "./IMainClientCallbacks"; | ||
import IErrorObj from "./IErrorObj"; | ||
import findIndex from './utils/findIndex'; | ||
import { | ||
renderRegisterName, renderDispatchName, mainDispatchName, mainInitName, mainReturnName, winMessageName, messageName | ||
} = require('./constants'); | ||
} from './constants'; | ||
import IMainClient, { IClientInfo } from './IMainClient'; | ||
export default class ElectronMainClient { | ||
callbacks: any; | ||
clients = {}; | ||
clientInfos = []; | ||
export default class BrowserMainClient implements IMainClient { | ||
callbacks: IMainClientCallbacks; | ||
clients: { [clientId: string]: Window } = {}; | ||
clientInfos: IClientInfo[] = []; | ||
constructor(callbacks) { | ||
constructor(callbacks: IMainClientCallbacks, log: Log) { | ||
this.callbacks = callbacks; | ||
(window as any).isMainClient = true; | ||
(window as any).isMainClient = true; | ||
window.addEventListener("message", (event) => { | ||
let callbacks = this.callbacks; | ||
let { action, data, invokeId, senderId, clientId } = event.data || {} as any; | ||
if (action === renderDispatchName) { | ||
callbacks.handleRendererMessage(data).then((result) => { | ||
this.clients[clientId].postMessage({ | ||
action: mainReturnName, | ||
invokeId, | ||
data: result, | ||
}, '*'); | ||
}, (err) => { | ||
let errObj = { name: err.name, message: err.message }; | ||
Object.keys(err).forEach(key => errObj[key] = err[key]); | ||
this.clients[clientId].postMessage({ | ||
action: mainReturnName, | ||
invokeId, | ||
error: errObj, | ||
}, '*'); | ||
}); | ||
} else if (action === winMessageName) { | ||
window.addEventListener("message", this.handleMessage); | ||
this.addWin('mainClient', window); | ||
} | ||
private handleMessage = (event: MessageEvent) => { | ||
let callbacks = this.callbacks; | ||
let { action, data, invokeId, senderId, clientId } = event.data || {} as any; | ||
if (action === renderDispatchName) { // Renderer register self | ||
callbacks.handleRendererMessage(data).then((result) => { | ||
this.clients[clientId].postMessage({ | ||
action: winMessageName, | ||
senderId, | ||
data: data, | ||
action: mainReturnName, | ||
invokeId, | ||
data: result, | ||
}, '*'); | ||
} else if (action === 'close') { // Child window has closed | ||
let index = findIndex(this.clientInfos, (item) => item.clientId === clientId); | ||
if (index !== -1) this.clientInfos.splice(index, 1); | ||
this.clients[clientId] = null; | ||
callbacks.deleteWin(clientId); | ||
} else if (action === renderRegisterName) { | ||
let { filter } = data; | ||
callbacks.addWin(clientId); | ||
this.clientInfos.push({ clientId, filter }); | ||
}, (err) => { | ||
let errObj: IErrorObj = { name: err.name, message: err.message }; | ||
Object.keys(err).forEach(key => errObj[key] = err[key]); | ||
this.clients[clientId].postMessage({ | ||
action: mainInitName, | ||
data: [ | ||
callbacks.getInitStates(clientId, filter), | ||
callbacks.getStores(clientId, filter) | ||
], | ||
action: mainReturnName, | ||
invokeId, | ||
error: errObj, | ||
}, '*'); | ||
} | ||
}); | ||
}); | ||
} else if (action === winMessageName) { | ||
this.clients[clientId].postMessage({ | ||
action: winMessageName, | ||
senderId, | ||
data: data, | ||
}, '*'); | ||
} else if (action === 'close') { // Child window has closed | ||
let index = findIndex(this.clientInfos, (item) => item.clientId === clientId); | ||
if (index !== -1) this.clientInfos.splice(index, 1); | ||
delete this.clients[clientId]; | ||
callbacks.deleteWin(clientId); | ||
} else if (action === renderRegisterName) { | ||
this.addWin('mainClient', window); | ||
} | ||
addWin(clientId, newWin) { | ||
callbacks.addWin(clientId); | ||
this.clientInfos.push({ clientId }); | ||
this.clients[clientId].postMessage({ | ||
action: mainInitName, | ||
data: [ | ||
callbacks.getInitStates(clientId), | ||
callbacks.getStores(clientId) | ||
], | ||
}, '*'); | ||
} | ||
}; | ||
addWin(clientId: string, newWin: Window) { | ||
this.clients[clientId] = newWin; | ||
@@ -71,3 +78,3 @@ } | ||
sendToRenderer(client, payload) { | ||
dispatchToRenderer(client: IClientInfo, payload: any) { | ||
let window = this.clients[client.clientId]; | ||
@@ -77,7 +84,7 @@ window && window.postMessage({ action: mainDispatchName, data: payload }, '*'); | ||
sendMessage(win, message) { | ||
win && win.postMessage({ action: messageName, data: message }, '*'); | ||
} | ||
// sendMessage(win, message) { | ||
// win && win.postMessage({ action: messageName, data: message }, '*'); | ||
// } | ||
sendMessageByClientId(clientId, message) { | ||
sendWinMsg(clientId: string, message: any) { | ||
let win = this.clients[clientId]; | ||
@@ -87,10 +94,10 @@ win && win.postMessage({ action: messageName, data: message }, '*'); | ||
closeAllWindows() { | ||
Object.keys(this.clients).forEach(clientId => { | ||
let window = this.clients[clientId]; | ||
window && window.close(); | ||
}); | ||
} | ||
// closeAllWindows() { | ||
// Object.keys(this.clients).forEach(clientId => { | ||
// let window = this.clients[clientId]; | ||
// window && window.close(); | ||
// }); | ||
// } | ||
changeClientAction(clientId, params) { | ||
changeClientAction(clientId: string, params: any) { | ||
let win = this.clients[clientId]; | ||
@@ -100,2 +107,24 @@ // this.sendMessage(win, { action: 'change-props', url }); | ||
} | ||
isRegister(clientId: string): boolean { | ||
return !!this.clients[clientId]; | ||
} | ||
whenRegister(clientId: string, callback: () => void) { | ||
if (this.isRegister(clientId)) { | ||
return callback(); | ||
} | ||
const whenMessage = (event: MessageEvent) => { | ||
let { action } = event.data || {} as any; | ||
if (action === renderDispatchName) { | ||
window.removeEventListener("message", whenMessage); | ||
callback(); | ||
} | ||
}; | ||
window.addEventListener("message", whenMessage); | ||
} | ||
isClose(clientId: string): boolean { | ||
return !this.clients[clientId]; | ||
} | ||
} |
@@ -1,4 +0,5 @@ | ||
let { | ||
import { | ||
renderDispatchName, renderRegisterName, mainDispatchName, mainInitName, mainReturnName, winMessageName, messageName | ||
} = require('./constants'); | ||
} from './constants'; | ||
import { IStoreCallback, IActionCallback, IResultCallback, IMessageCallback, IWinMessageCallback } from './IRendererClient'; | ||
@@ -8,8 +9,8 @@ export default class BrowserRendererClient { | ||
constructor(filter, callback, onGetAction, onGetResult, onGetMessage, onGetWinMessage) { | ||
let clientId = window['clientId'] || 'mainClient'; | ||
constructor(callback: IStoreCallback, onGetAction: IActionCallback, onGetResult: IResultCallback, onGetMessage: IMessageCallback, onGetWinMessage: IWinMessageCallback) { | ||
let clientId = (window as any)['clientId'] || 'mainClient'; | ||
this.clientId = clientId; | ||
let mainWin = window['isMainClient'] ? window : window.opener; | ||
mainWin.postMessage({ action: renderRegisterName, clientId, data: { filter } }, '*'); | ||
let mainWin = (window as any)['isMainClient'] ? window : window.opener; | ||
mainWin.postMessage({ action: renderRegisterName, clientId, data: {} }, '*'); | ||
window.addEventListener('message', (event) => { | ||
@@ -35,15 +36,15 @@ let { action, error, data, senderId, invokeId } = event.data || {} as any; | ||
// Forward update to the main process so that it can forward the update to all other renderers | ||
forward(invokeId, action) { | ||
forward(invokeId: number, action: any) { | ||
let clientId = this.clientId; | ||
let mainWin = window['isMainClient'] ? window : window.opener; | ||
let mainWin = (window as any)['isMainClient'] ? window : window.opener; | ||
mainWin.postMessage({ action: renderDispatchName, data: action, invokeId, clientId }, '*'); | ||
} | ||
sendMessage(args) { | ||
let mainWin = window['isMainClient'] ? window : window.opener; | ||
sendMessage(args: any) { | ||
let mainWin = (window as any)['isMainClient'] ? window : window.opener; | ||
mainWin.postMessage({ action: messageName, data: args }, '*'); | ||
} | ||
sendWindowMessage(clientId, args) { | ||
let mainWin = window['isMainClient'] ? window : window.opener; | ||
sendWindowMessage(clientId: string, args: any) { | ||
let mainWin = (window as any)['isMainClient'] ? window : window.opener; | ||
let senderId = this.clientId; | ||
@@ -50,0 +51,0 @@ mainWin.postMessage({ action: winMessageName, senderId, clientId, data: args }, '*'); |
@@ -1,11 +0,10 @@ | ||
module.exports = { | ||
renderRegisterName: 'event-flux-renderer-register', | ||
renderDispatchName: 'event-flux-renderer-dispatch', | ||
mainInitName: 'event-flux-main-init', | ||
mainDispatchName: 'event-flux-main-dispatch', | ||
mainReturnName: 'event-flux-main-return', | ||
winManagerStoreName: 'winManagerStore', | ||
winManagerKey: 'winManager', | ||
messageName: '__MESSAGE__', | ||
winMessageName: '__WIN_MESSAGE__', | ||
}; | ||
export const renderRegisterName = '__RENDERER_REGISTER__'; | ||
export const renderDispatchName = '__RENDERER_DISPATCH__'; | ||
export const mainInitName = '__MAIN_INIT__'; | ||
export const mainDispatchName = '__MAIN_DISPATCH__'; | ||
export const mainReturnName = '__MAIN_RETURN__'; | ||
export const winManagerStoreName = 'winManagerStore'; | ||
export const winManagerKey = 'winManager'; | ||
export const messageName = '__MESSAGE__'; | ||
export const winMessageName = '__WIN_MESSAGE__'; | ||
import { Log } from "./utils/loggerApply"; | ||
const { | ||
import IMainClientCallbacks from "./IMainClientCallbacks"; | ||
import IErrorObj from "./IErrorObj"; | ||
import { | ||
mainInitName, mainDispatchName, mainReturnName, renderDispatchName, renderRegisterName, messageName, winMessageName | ||
} = require('./constants'); | ||
const { ipcMain } = require('electron'); | ||
const findIndex = require('lodash/findIndex'); | ||
} from './constants'; | ||
import { ipcMain, WebContents, BrowserWindow, Event } from 'electron'; | ||
import findIndex from './utils/findIndex'; | ||
import IMainClient from './IMainClient'; | ||
export default class ElectronMainClient { | ||
unregisterRenderer: any; | ||
clientInfos: any; | ||
clientMap: any; | ||
interface IClientInfo { | ||
webContents: WebContents; | ||
window: BrowserWindow; | ||
clientId: string; | ||
}; | ||
export default class ElectronMainClient implements IMainClient { | ||
clientInfos: IClientInfo[] = []; | ||
clientMap: { [key: string]: IClientInfo } = {}; | ||
log: Log; | ||
mainClientCallbacks: IMainClientCallbacks; | ||
constructor(callbacks, log: Log) { | ||
constructor(callbacks: IMainClientCallbacks, log: Log) { | ||
this.log = log; | ||
this.mainClientCallbacks = callbacks; | ||
let clientInfos = []; // webContentsId -> {webContents, filter, clientId, windowId, active} | ||
let clientMap = {}; | ||
// Need to keep track of windows, as when a window refreshes it creates a new | ||
@@ -25,97 +32,99 @@ // webContents, and the old one must be unregistered | ||
// events when a BrowserWindow is created using remote | ||
let unregisterRenderer = (clientId) => { | ||
let existIndex = findIndex(clientInfos, (item) => item.clientId === clientId); | ||
if (existIndex !== -1) { | ||
clientInfos.splice(existIndex, 1); | ||
clientMap[clientId] = null; | ||
callbacks.deleteWin(clientId); | ||
} | ||
}; | ||
this.unregisterRenderer = unregisterRenderer; | ||
ipcMain.on(renderRegisterName, ({ sender }, { filter, clientId }) => { | ||
let existIndex = findIndex(clientInfos, (item) => item.clientId === clientId); | ||
if (existIndex !== -1) { | ||
unregisterRenderer(clientId); | ||
} | ||
ipcMain.once(renderRegisterName, this.handleRegister); | ||
let clientInfo = { | ||
webContents: sender, | ||
filter, | ||
clientId, | ||
window: sender.getOwnerBrowserWindow(), | ||
}; | ||
clientInfos.push(clientInfo); | ||
clientMap[clientId] = clientInfo; | ||
// Add window first, then get window info, The window info should has prepared | ||
callbacks.addWin(clientId); | ||
ipcMain.on(renderDispatchName, this.handleRendererDispatch); | ||
if (!sender.isGuest()) { // For windowMap (not webviews) | ||
let browserWindow = sender.getOwnerBrowserWindow(); | ||
// Webcontents aren't automatically destroyed on window close | ||
browserWindow.on('closed', () => unregisterRenderer(clientId)); | ||
} | ||
ipcMain.on(winMessageName, this.handleWinMessage); | ||
sender.send(mainInitName, callbacks.getStores(clientId), callbacks.getInitStates(clientId)); | ||
}); | ||
return this; | ||
} | ||
// Give renderers a way to sync the current state of the store, but be sure we don't | ||
// expose any remote objects. In other words, we need to rely exclusively on primitive | ||
// data types, Arrays, or Buffers. Refer to: | ||
// https://github.com/electron/electron/blob/master/docs/api/remote.md#remote-objects | ||
// global[mainInitName + 'Stores'] = function(clientId) { | ||
// return callbacks.getStores(clientId, clientMap[clientId].filter); | ||
// } | ||
// global[mainInitName] = (clientId) => { | ||
// return callbacks.getInitStates(clientId, clientMap[clientId].filter); | ||
// } | ||
ipcMain.on(renderDispatchName, (event, clientId, invokeId, stringifiedAction) => { | ||
if (!clientMap[clientId]) return; | ||
let webContents = clientMap[clientId].webContents; | ||
private handleUnregisterRenderer(clientId: string) { | ||
let existIndex: number = findIndex(this.clientInfos, (item: IClientInfo) => item.clientId === clientId); | ||
if (existIndex !== -1) { | ||
this.clientInfos.splice(existIndex, 1); | ||
delete this.clientMap[clientId]; | ||
this.mainClientCallbacks.deleteWin(clientId); | ||
} | ||
}; | ||
callbacks.handleRendererMessage(stringifiedAction).then(result => { | ||
if (this.checkWebContents(webContents)) { | ||
webContents.send(mainReturnName, invokeId, undefined, result); | ||
} | ||
}, (err) => { | ||
let errObj = null; | ||
// Renderer process register self, Then the main process will send the store the initial state to the renderer process | ||
private handleRegister = ({ sender }: { sender: WebContents }, { clientId }: { clientId: string }) => { | ||
let existIndex = findIndex(this.clientInfos, (item: IClientInfo) => item.clientId === clientId); | ||
if (existIndex !== -1) { | ||
this.handleUnregisterRenderer(clientId); | ||
} | ||
if (err) { | ||
errObj = { name: err.name, message: err.message }; | ||
Object.keys(err).forEach(key => errObj[key] = err[key]); | ||
let clientInfo: IClientInfo = { | ||
webContents: sender, | ||
clientId, | ||
window: BrowserWindow.fromWebContents(sender), // sender.getOwnerBrowserWindow(), | ||
}; | ||
this.clientInfos.push(clientInfo); | ||
this.clientMap[clientId] = clientInfo; | ||
// Add window first, then get window info, The window info should has prepared | ||
this.mainClientCallbacks.addWin(clientId); | ||
let browserWindow = BrowserWindow.fromWebContents(sender); | ||
// Webcontents aren't automatically destroyed on window close | ||
browserWindow.on('closed', () => this.handleUnregisterRenderer(clientId)); | ||
this._sendForWebContents( | ||
sender, | ||
mainInitName, | ||
this.mainClientCallbacks.getStores(clientId), | ||
this.mainClientCallbacks.getInitStates(clientId) | ||
); | ||
}; | ||
// When renderer process dispatch an action to main process, the handleRendererDispatch will invoke | ||
// The main process will invoke handleRendererMessage to handle the message and send the result back to renderer process | ||
private handleRendererDispatch = (event: Event, clientId: string, invokeId: string, stringifiedAction: string) => { | ||
if (!this.clientMap[clientId]) return; | ||
let webContents = this.clientMap[clientId].webContents; | ||
this.mainClientCallbacks.handleRendererMessage(stringifiedAction).then(result => { | ||
this._sendForWebContents(webContents, mainReturnName, invokeId, undefined, result); | ||
}, (err) => { | ||
let errObj: IErrorObj | null = null; | ||
if (err) { | ||
errObj = { name: err.name, message: err.message } as IErrorObj; | ||
if (errObj) { | ||
Object.keys(err).forEach(key => errObj![key] = err[key]); | ||
} | ||
if (this.checkWebContents(webContents)) { | ||
webContents.send(mainReturnName, invokeId, errObj, undefined); | ||
} | ||
}); | ||
} | ||
this._sendForWebContents(webContents, mainReturnName, invokeId, errObj, undefined); | ||
}); | ||
}; | ||
ipcMain.on(winMessageName, (event, clientId, data) => { | ||
if (!clientMap[clientId]) return; | ||
let webContents = clientMap[clientId].webContents; | ||
let existIndex = findIndex(clientInfos, (item) => item.webContents === event.sender); | ||
if (existIndex !== -1) { | ||
webContents.send(winMessageName, clientInfos[existIndex].clientId, data); | ||
} | ||
}); | ||
this.clientInfos = clientInfos; | ||
this.clientMap = clientMap; | ||
handleWinMessage = (event: Event, clientId: string, data: any) => { | ||
if (!this.clientMap[clientId]) return; | ||
let webContents = this.clientMap[clientId].webContents; | ||
let existIndex = findIndex(this.clientInfos, (item: IClientInfo) => item.webContents === event.sender); | ||
if (existIndex !== -1) { | ||
this._sendForWebContents(webContents, winMessageName, this.clientInfos[existIndex].clientId, data); | ||
} | ||
}; | ||
private checkWebContents(webContents: WebContents) { | ||
return !webContents.isDestroyed() && !webContents.isCrashed(); | ||
} | ||
getForwardClients() { | ||
return this.clientInfos; | ||
private _sendForWebContents(webContents: WebContents, channel: string, ...args: any[]) { | ||
if (this.checkWebContents(webContents)) { | ||
webContents.send(channel, ...args); | ||
} | ||
} | ||
checkWebContents(webContents) { | ||
return !webContents.isDestroyed() && !webContents.isCrashed(); | ||
getForwardClients(): IClientInfo[] { | ||
return this.clientInfos; | ||
} | ||
sendToRenderer(client, payload) { | ||
dispatchToRenderer(client: IClientInfo, payload: any) { | ||
let webContents = client.webContents; | ||
@@ -129,10 +138,4 @@ // if (webContents.isDestroyed() || webContents.isCrashed()) { | ||
} | ||
sendMessage(win, message) { | ||
if (this.checkWebContents(win.webContents)) { | ||
win.webContents.send(messageName, message); | ||
} | ||
} | ||
sendMessageByClientId(clientId, message) { | ||
sendWinMsg(clientId: string, message: any) { | ||
let webContents = this.clientMap[clientId].webContents; | ||
@@ -144,23 +147,25 @@ if (this.checkWebContents(webContents)) { | ||
closeAllWindows() { | ||
this.clientInfos.slice().forEach(client => { | ||
if (!client.window.isDestroyed()) { | ||
client.window.close() | ||
} | ||
}); | ||
// 通过clientId获取BrowserWindow | ||
getWindow(clientId: string): BrowserWindow { | ||
return this.clientMap[clientId].window; | ||
} | ||
getWindowByClientId(clientId) { | ||
return this.clientMap[clientId].window; | ||
// 通过clientId获取WebContents | ||
getWebContents(clientId: string): WebContents { | ||
return this.clientMap[clientId].webContents; | ||
} | ||
changeClientAction(clientId, params) { | ||
changeClientAction(clientId: string, params: any) { | ||
if (this.clientMap[clientId]) { | ||
let win = this.clientMap[clientId].window; | ||
let webContents = this.clientMap[clientId].webContents; | ||
// this.sendMessage(win, { action: 'change-props', url }); | ||
win.webContents.send("__INIT_WINDOW__", params); | ||
// win.webContents.send("__INIT_WINDOW__", params); | ||
this._sendForWebContents(webContents, "__INIT_WINDOW__", params); | ||
this.log((logger) => logger("ElectronMainClient", "init Window", params)); | ||
} else { | ||
// 还没有初始化,则监听注册事件,当初始化之后 开始初始化 | ||
ipcMain.on(renderRegisterName, ({ sender }, { filter, clientId: nowClientId }) => { | ||
ipcMain.once(renderRegisterName, (event: Event, { clientId: nowClientId }: { clientId: string }) => { | ||
if (nowClientId === clientId) { | ||
@@ -172,2 +177,21 @@ this.changeClientAction(clientId, params); | ||
} | ||
isRegister(clientId: string): boolean { | ||
return !!this.clientMap[clientId]; | ||
} | ||
whenRegister(clientId: string, callback: () => void): void { | ||
if (this.isRegister(clientId)) { | ||
return callback(); | ||
} | ||
ipcMain.once(renderRegisterName, (event: Event, { clientId: nowClientId }: { clientId: string }) => { | ||
if (nowClientId === clientId) { | ||
callback(); | ||
} | ||
}); | ||
} | ||
isClose(clientId: string): boolean { | ||
return !this.clientMap[clientId]; | ||
} | ||
} |
@@ -1,5 +0,8 @@ | ||
const { | ||
import { | ||
mainInitName, renderRegisterName, renderDispatchName, mainDispatchName, mainReturnName, winMessageName, messageName | ||
} = require('./constants'); | ||
const { ipcRenderer, remote } = require('electron'); | ||
} from './constants'; | ||
import { ipcRenderer, remote } from 'electron'; | ||
import { | ||
IStoreCallback, IActionCallback, IResultCallback, IMessageCallback, IWinMessageCallback, IInitWindowCallback | ||
} from './IRendererClient'; | ||
@@ -9,3 +12,10 @@ export default class ElectronRendererClient { | ||
constructor(filter, callback, onGetAction, onGetResult, onGetMessage, onGetWinMessage, onInitWindow) { | ||
constructor( | ||
callback: IStoreCallback, | ||
onGetAction: IActionCallback, | ||
onGetResult: IResultCallback, | ||
onGetMessage: IMessageCallback, | ||
onGetWinMessage: IWinMessageCallback, | ||
onInitWindow: IInitWindowCallback | ||
) { | ||
let clientId = (window as any).clientId; | ||
@@ -19,22 +29,22 @@ if (!clientId) { | ||
// Allows the main process to forward updates to this renderer automatically | ||
ipcRenderer.send(renderRegisterName, { filter: filter, clientId }); | ||
ipcRenderer.send(renderRegisterName, { clientId }); | ||
// Dispatches from other processes are forwarded using this ipc message | ||
ipcRenderer.on(mainInitName, (event, storeFilters, stateData) => { | ||
ipcRenderer.on(mainInitName, (event: Event, storeFilters: any, stateData: any) => { | ||
callback(stateData, storeFilters); | ||
}); | ||
ipcRenderer.on(mainDispatchName, (event, stringifiedAction) => { | ||
ipcRenderer.on(mainDispatchName, (event: Event, stringifiedAction: string) => { | ||
onGetAction(stringifiedAction); | ||
}); | ||
ipcRenderer.on(mainReturnName, (event, invokeId, error, result) => { | ||
ipcRenderer.on(mainReturnName, (event: Event, invokeId: number, error: Error, result: any) => { | ||
onGetResult(invokeId, error, result); | ||
}); | ||
ipcRenderer.on(messageName, (event, params) => { | ||
ipcRenderer.on(messageName, (event: Event, params: any) => { | ||
onGetMessage(params); | ||
}); | ||
ipcRenderer.on(winMessageName, (event, senderId, params) => { | ||
ipcRenderer.on(winMessageName, (event: Event, senderId: string, params: any) => { | ||
onGetWinMessage(senderId, params); | ||
}); | ||
ipcRenderer.on("__INIT_WINDOW__", (event, params) => { | ||
ipcRenderer.on("__INIT_WINDOW__", (event: Event, params: any) => { | ||
onInitWindow(params); | ||
@@ -45,13 +55,13 @@ }); | ||
// Forward update to the main process so that it can forward the update to all other renderers | ||
forward(invokeId, action) { | ||
forward(invokeId: string, action: any) { | ||
ipcRenderer.send(renderDispatchName, this.clientId, invokeId, action); | ||
} | ||
sendMessage(args) { | ||
sendMessage(args: any) { | ||
ipcRenderer.send(messageName, args); | ||
} | ||
sendWindowMessage(clientId, args) { | ||
sendWindowMessage(clientId: string, args: any) { | ||
ipcRenderer.send(winMessageName, clientId, args); | ||
} | ||
} |
'use strict'; | ||
import { BrowserWindow, Rectangle } from "electron"; | ||
var electron = require('electron'); | ||
@@ -9,13 +11,32 @@ var deepEqual = require('deep-equal'); | ||
function isNumber(num) { | ||
function isNumber(num: any) { | ||
return typeof typeof(num) === 'number' && !isNaN(num); | ||
} | ||
type OnSave = (state: any) => void; | ||
export interface IWinState { | ||
x?: number; | ||
y?: number; | ||
width?: number; | ||
height?: number; | ||
useContentSize?: boolean; | ||
displayBounds?: Rectangle; | ||
isMaximized?: boolean; | ||
isFullScreen?: boolean; | ||
} | ||
interface IWinConfig { | ||
defaultWidth?: number; | ||
defaultHeight?: number; | ||
useContentSize?: boolean; | ||
} | ||
export default class ElectronWindowState { | ||
onSave: any; | ||
state: any; | ||
onSave: OnSave | null; | ||
state: IWinState = {}; | ||
winRef: any; | ||
stateChangeTimer: any; | ||
constructor(config, state, onSave) { | ||
constructor(config: any, state: any, onSave: OnSave | null) { | ||
this.onSave = onSave; | ||
@@ -28,3 +49,3 @@ this.stateChangeHandler = this.stateChangeHandler.bind(this); | ||
loadState(state, config) { | ||
loadState(state: IWinState, config: IWinConfig) { | ||
this.state = this.normState(state); | ||
@@ -41,14 +62,14 @@ // Check state validity | ||
normState(state) { | ||
normState(state: IWinState) { | ||
if (isNumber(state.x)) { | ||
state.x = Math.floor(state.x); | ||
state.x = Math.floor(state.x as number); | ||
} | ||
if (isNumber(state.y)) { | ||
state.y = Math.floor(state.y); | ||
state.y = Math.floor(state.y as number); | ||
} | ||
if (isNumber(state.width)) { | ||
state.width = Math.floor(state.width); | ||
state.width = Math.floor(state.width as number); | ||
} | ||
if (isNumber(state.height)) { | ||
state.height = Math.floor(state.height); | ||
state.height = Math.floor(state.height as number); | ||
} | ||
@@ -58,3 +79,3 @@ return state; | ||
isNormal(win) { | ||
isNormal(win: BrowserWindow) { | ||
return !win.isMaximized() && !win.isMinimized() && !win.isFullScreen(); | ||
@@ -68,4 +89,4 @@ } | ||
isInteger(state.y) && | ||
isInteger(state.width) && state.width > 0 && | ||
isInteger(state.height) && state.height > 0; | ||
isInteger(state.width) && (state.width as number) > 0 && | ||
isInteger(state.height) && (state.height as number) > 0; | ||
} | ||
@@ -79,11 +100,11 @@ | ||
// Check if the display where the window was last open is still available | ||
var displayBounds = electron.screen.getDisplayMatching(state).bounds; | ||
var displayBounds = electron.screen.getDisplayMatching(state as Rectangle).bounds; | ||
var sameBounds = deepEqual(state.displayBounds, displayBounds, {strict: true}); | ||
if (!sameBounds) { | ||
if (displayBounds.width < state.displayBounds.width) { | ||
if (state.x > displayBounds.width) { | ||
if ((state.x as number) > displayBounds.width) { | ||
state.x = 0; | ||
} | ||
if (state.width > displayBounds.width) { | ||
if ((state.width as number) > displayBounds.width) { | ||
state.width = displayBounds.width; | ||
@@ -94,7 +115,7 @@ } | ||
if (displayBounds.height < state.displayBounds.height) { | ||
if (state.y > displayBounds.height) { | ||
if ((state.y as number) > displayBounds.height) { | ||
state.y = 0; | ||
} | ||
if (state.height > displayBounds.height) { | ||
if ((state.height as number) > displayBounds.height) { | ||
state.height = displayBounds.height; | ||
@@ -144,6 +165,6 @@ } | ||
this.updateState(); | ||
this.onSave(this.state); | ||
this.onSave && this.onSave(this.state); | ||
} | ||
manage(win) { | ||
manage(win: BrowserWindow) { | ||
let state = this.state; | ||
@@ -150,0 +171,0 @@ if (state.isMaximized) { |
@@ -1,2 +0,2 @@ | ||
import AppStore from 'event-flux/lib/AppStore'; | ||
import AppStore from './AppStore'; | ||
import objectDifference from './utils/objectDifference'; | ||
@@ -7,3 +7,3 @@ import filterDifference from './utils/filterDifference'; | ||
import { filterWindowStore, filterWindowState, filterWindowDelta } from './utils/filterWindowStore'; | ||
import { declareStore } from './StoreDeclarer'; | ||
import { declareStore, StoreDeclarer, StoreListDeclarer, StoreMapDeclarer } from './StoreDeclarer'; | ||
import MainClient from './MainClient'; | ||
@@ -13,16 +13,20 @@ import MultiWinManagerStore, { WinPackStore } from './MultiWinManagerStore'; | ||
import MultiWinStore from './MultiWinStore'; | ||
import { addStateFilter } from './utils/stateFilterDecorator'; | ||
import loggerApply, { Log } from './utils/loggerApply'; | ||
import stateFilterDecorator from './utils/stateFilterDecorator'; | ||
import loggerApply, { Log, Logger } from './utils/loggerApply'; | ||
const isEmpty = require('lodash/isEmpty'); | ||
const isObject = require('lodash/isObject'); | ||
const { winManagerStoreName, winManagerKey } = require('./constants'); | ||
const { serialize, deserialize } = require('json-immutable-bn'); | ||
import { isEmpty, isObject } from './utils/objUtils'; | ||
import { winManagerStoreName, winManagerKey } from './constants'; | ||
import { serialize, deserialize } from 'json-immutable-bn'; | ||
import IStoresDeclarer, { IStoresObjDeclarer } from './IStoresDeclarer'; | ||
import IExtendStoreBase, { IExtendStoreBaseConstructor } from './IExtendStoreBase'; | ||
import IMainClient, { IClientInfo } from './IMainClient'; | ||
import IStoreFilters from './IStoreFilters'; | ||
function findStore(stores, storePath) { | ||
type OneStorePath = { name: string, type: string, index: string } | string; | ||
function findStore(stores: { [storeKey: string]: any }, storePath: OneStorePath[] | undefined) { | ||
if (!storePath) return; | ||
return storePath.reduce((subStores, entry) => { | ||
if (!subStores) return undefined; | ||
if (!isObject(entry)) return subStores[entry]; | ||
let { name, type, index } = entry; | ||
if (!isObject(entry)) return subStores[entry as string]; | ||
let { name, type, index } = entry as { name: string, type: string, index: string }; | ||
let storeCol = subStores[name]; | ||
@@ -35,15 +39,15 @@ if (type === 'List' || type === 'Map') { | ||
function storeEnhancer(appStore: MultiWindowAppStore, stores, storeShape, log: Log) { | ||
function storeEnhancer(appStore: MultiWindowAppStore, stores: { [storeKey: string]: any }, storeShape: IStoreFilters, log: Log) { | ||
const callbacks = { | ||
addWin(clientId) { | ||
addWin(clientId: string) { | ||
stores[winManagerStoreName].addWin(clientId); | ||
}, | ||
deleteWin(clientId) { | ||
deleteWin(clientId: string) { | ||
stores[winManagerStoreName].deleteWin(clientId); | ||
}, | ||
getStores(clientId, filter) { | ||
getStores(clientId: string) { | ||
let stores = filterWindowStore(storeShape, winManagerStoreName, clientId); | ||
return JSON.stringify(stores); | ||
}, | ||
getInitStates(clientId, filter) { | ||
getInitStates(clientId: string) { | ||
if (process.env.NODE_ENV === 'development') { | ||
@@ -65,3 +69,3 @@ if (!appStore._prevStateFilters || !appStore._prevStateFilters[clientId]) { | ||
}, | ||
handleRendererMessage(payload) { | ||
handleRendererMessage(payload: any) { | ||
try { | ||
@@ -85,5 +89,5 @@ const { store: storePath, method, args } = deserialize(payload); | ||
const mainClient = new MainClient(callbacks, log); | ||
const mainClient: IMainClient = new MainClient(callbacks, log); | ||
appStore.mainClient = mainClient; | ||
const forwarder = (prevState, state, prevFilters, filters) => { | ||
const forwarder = (prevState: any, state: any, prevFilters: any, filters: any) => { | ||
// Forward all actions to the listening renderers | ||
@@ -102,3 +106,3 @@ let clientInfo = mainClient.getForwardClients(); | ||
if (isEmpty(updateState)) return; | ||
mainClient.sendToRenderer(client, serialize({ payload: { updated: updateState } })); | ||
mainClient.dispatchToRenderer(client, serialize({ payload: { updated: updateState } })); | ||
} | ||
@@ -110,3 +114,3 @@ }); | ||
clientInfo.forEach(client => { | ||
clientInfo.forEach((client: IClientInfo) => { | ||
let { clientId } = client; | ||
@@ -127,3 +131,3 @@ | ||
mainClient.sendToRenderer(client, serialize(action)); | ||
mainClient.dispatchToRenderer(client, serialize(action)); | ||
}); | ||
@@ -137,11 +141,10 @@ }; | ||
forwarder: any; | ||
willQuit: boolean; | ||
_stateFilters: any; | ||
_prevStateFilters: any; | ||
filterCallbacks = []; | ||
filterCallbacks: ((filter: any) => void)[] = []; | ||
mainClient: any; | ||
log: Log; | ||
static innerStores; | ||
static innerStores: { [storeKey: string]: any }; | ||
@@ -159,7 +162,7 @@ constructor(log: Log) { | ||
let winManagerStore = this.stores.winManagerStore; | ||
winManagerStore.onDidAddWin((clientId) => { | ||
winManagerStore.onDidAddWin((clientId: string) => { | ||
this._handleAddWin(clientId); | ||
this._sendUpdate(); | ||
}); | ||
winManagerStore.onDidRemoveWin((clientId) => this._handleRemoveWin(clientId)); | ||
winManagerStore.onDidRemoveWin((clientId: string) => this._handleRemoveWin(clientId)); | ||
this._prevStateFilters = Object.assign({}, this._stateFilters); | ||
@@ -173,7 +176,7 @@ | ||
handleWillFilterChange(prevState, state, prevFilters, filters) { | ||
handleWillFilterChange(prevState: any, state: any, prevFilters: any, filters: any) { | ||
return this.forwarder(prevState, state, prevFilters, filters); | ||
}; | ||
onDidFilterChange(callback) { | ||
onDidFilterChange(callback: (stateFilter: any) => void) { | ||
this.filterCallbacks.push(callback); | ||
@@ -194,11 +197,11 @@ } | ||
getStore(key) { | ||
getStore(key: string) { | ||
return this.stores[key] | ||
} | ||
setStore(key, store) { | ||
setStore(key: string, store: any) { | ||
return this.stores[key] = store; | ||
} | ||
getWinSpecificStore(clientId, storeName) { | ||
getWinSpecificStore(clientId: string, storeName: string) { | ||
let winStores = this.stores[winManagerStoreName].winPackMap[clientId]; | ||
@@ -211,3 +214,3 @@ if (winStores) return winStores[storeName]; | ||
// 初始化子Stores | ||
initStores(parent) {} | ||
initStores(parent: MultiWindowAppStore) {} | ||
// 开始监听子Store改变 | ||
@@ -217,9 +220,9 @@ startObserve() {} | ||
_initStateFilters() {} | ||
_handleAddWin(clientId) {} | ||
_handleRemoveWin(clientId) {} | ||
_handleAddWin(clientId: string) {} | ||
_handleRemoveWin(clientId: string) {} | ||
} | ||
export default function buildMultiWinAppStore( | ||
stores, | ||
winStores, | ||
stores: IStoresObjDeclarer, | ||
winStores: IStoresObjDeclarer, | ||
{ | ||
@@ -230,6 +233,6 @@ WindowsManagerStore = MultiWinManagerStore, | ||
}, | ||
logger | ||
logger: Logger | ||
) { | ||
WinPackStore.innerStores = { ...winStores, actionRecord: ActionStore }; | ||
let allStores = { | ||
let allStores: IStoresObjDeclarer = { | ||
...stores, | ||
@@ -239,9 +242,9 @@ multiWin: WinHandleStore, | ||
}; | ||
let MultiWinAppStore = addStateFilter(MultiWindowAppStore); | ||
let MultiWinAppStore = stateFilterDecorator(MultiWindowAppStore as any as IExtendStoreBaseConstructor); | ||
MultiWinAppStore.innerStores = allStores; | ||
const storeShape = filterOneStore(MultiWinAppStore, { applyFilter: true }); | ||
const appStore = new MultiWinAppStore(loggerApply(logger)); | ||
appStore.storeShape = storeShape; | ||
(appStore as any).storeShape = storeShape; | ||
appStore.init(); | ||
return appStore; | ||
} |
export default typeof window !== 'object' ? | ||
require('./ElectronMainClient').default : | ||
require('./BrowserMainClient').default; |
@@ -1,4 +0,4 @@ | ||
import StoreBase from 'event-flux/lib/StoreBase'; | ||
import { addStateFilter } from './utils/stateFilterDecorator'; | ||
import StoreBase, { IS_STORE } from './StoreBase'; | ||
import stateFilterDecorator from './utils/stateFilterDecorator'; | ||
export default addStateFilter(StoreBase); | ||
export default stateFilterDecorator(StoreBase); |
import MultiWinStore from './MultiWinStore'; | ||
import ElectronWindowState from './ElectronWindowState'; | ||
import ElectronWindowState, { IWinState } from './ElectronWindowState'; | ||
import { format as formatUrl } from 'url'; | ||
import * as path from 'path'; | ||
import { app, BrowserWindow, screen } from 'electron'; | ||
import IStorage from './storage/IStorage'; | ||
import { IWinProps, IWinParams } from './IMultiWinStore'; | ||
@@ -10,10 +12,11 @@ const isDevelopment = process.env.NODE_ENV !== 'production' | ||
export class WindowManager { | ||
windows = []; | ||
windows: { clientId: string, window: BrowserWindow }[] = []; | ||
winHandler: any; | ||
winSize = 1; | ||
constructor(winHandler) { | ||
constructor(winHandler: MultiWinCacheStore) { | ||
this.windows = []; | ||
this.winHandler = winHandler; | ||
app.whenReady().then(() => this.ensureWindows()); | ||
// app.whenReady().then(() => this.ensureWindows()); | ||
this.ensureWindows(); | ||
} | ||
@@ -26,4 +29,4 @@ | ||
createWin(clientId) { | ||
return this.winHandler.createWindow(clientId); | ||
createWin(clientId: string) { | ||
return this.winHandler.createNativeWindow(clientId); | ||
} | ||
@@ -34,3 +37,3 @@ | ||
while (this.windows.length < this.winSize) { | ||
let clientId = this.genClientId() | ||
let clientId = this.genClientId(); | ||
this.windows.push({ clientId, window: this.createWin(clientId) }); | ||
@@ -40,4 +43,4 @@ } | ||
getWin() { | ||
if (!this.windows) return null; | ||
getWin(): { clientId: string, window: BrowserWindow } | undefined { | ||
if (!this.windows) return undefined; | ||
let winInfo = this.windows.shift(); | ||
@@ -54,47 +57,50 @@ this.ensureWindows(); | ||
}); | ||
this.windows = null; | ||
this.windows = []; | ||
} | ||
} | ||
interface IClientCacheInfo { | ||
parentId?: string; | ||
clientId: string; | ||
name?: string; | ||
groups?: string[]; | ||
url: string; | ||
winState?: IWinState; | ||
} | ||
class MultiWinCacheStore extends MultiWinStore { | ||
clientInfoMap = {}; | ||
clientStateMap = {}; | ||
clientIds = []; | ||
createWins = []; | ||
clientInfoMap: { [clientId: string]: IClientCacheInfo } = {}; | ||
willQuit = false; | ||
windowManager: WindowManager; | ||
windowManager?: WindowManager; | ||
static createWindow; | ||
init() { | ||
this.loadClients(); | ||
} | ||
init() { | ||
let clients = this.getStorage().get('clients'); | ||
loadClients() { | ||
this.willQuit = false; | ||
let clients = this.getStorage()!.get('clients'); | ||
if (!clients || clients.length === 0) { | ||
clients = this.getDefaultClients(); | ||
} else { | ||
clients = this.filterClients(clients); | ||
} | ||
} | ||
this.windowManager = new WindowManager(this); | ||
app.whenReady().then(() => { | ||
clients.forEach(item => this.createElectronWin(item.url, item.clientId, item.parentId, item.winState)); | ||
}); | ||
// app.whenReady().then(() => { | ||
// clients.forEach(item => this.createElectronWin(item.url, item.clientId, item.parentId, item.winState)); | ||
// }); | ||
clients.forEach((item: IClientCacheInfo) => this.createWinForClientId(item, item.clientId, item.parentId, item.winState!)); | ||
} | ||
filterClients(clients) { | ||
let clientIds = new Set(); | ||
clients = clients.filter(client => { | ||
if (!clientIds.has(client.clientId)) { | ||
clientIds.add(client.clientId); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
return clients; | ||
saveClients() { | ||
let clients = this.clientIds.map(id => ({ | ||
clientId: id, ...this.clientInfoMap[id], name: this.clientIdNameMap[id], groups: this.groupsMap[id], | ||
})); | ||
this.getStorage()!.set('clients', clients); | ||
} | ||
getDefaultClients() { | ||
getDefaultClients(): IClientCacheInfo[] { | ||
return [{ clientId: 'mainClient', url: '/', winState: { isMaximized: true } }]; | ||
} | ||
getStorage() { | ||
getStorage(): IStorage | null { | ||
console.error('You need inherit MultiWinCacheStore and implement getStorage'); | ||
@@ -104,34 +110,39 @@ return null; | ||
onDidWinClose(clientId) { | ||
// this._appStore.mainClient.sendMessageByClientId(clientId, { action: 'did-close' }); | ||
_removeClientId(clientId: string) { | ||
super._removeClientId(clientId); | ||
delete this.clientInfoMap[clientId]; | ||
} | ||
closeAllWindows() { | ||
// this._appStore.mainClient.closeAllWindows(); | ||
this.windowManager.dispose(); | ||
this.createWins.slice().forEach(window => { | ||
if (!window.isDestroyed()) { | ||
window.close(); | ||
} | ||
}); | ||
this.namedWinIdMap = {}; | ||
this.clientNamedWinIdMap = {}; | ||
onDidWinClose(clientId: string) { | ||
(this._appStore as any).mainClient.sendWinMsg(clientId, { action: 'did-close' }); | ||
} | ||
saveClients(clientIds) { | ||
let clients = clientIds.map(id => ({ | ||
clientId: id, ...this.clientInfoMap[id], winState: this.clientStateMap[id], | ||
})); | ||
this.getStorage().set('clients', clients); | ||
closeWin(clientId: string) { | ||
let win = this.clientWins[clientId] as BrowserWindow; | ||
if (win && !win.isDestroyed()) win.close(); | ||
} | ||
saveWinState(clientId, winState) { | ||
this.clientStateMap[clientId] = winState; | ||
this.saveClients(this.clientIds || []); | ||
saveWinState(clientId: string, winState: IWinState) { | ||
this.clientInfoMap[clientId].winState = winState; | ||
this.saveClients(); | ||
} | ||
createWin(url, parentClientId, params) { | ||
// 当要创建的窗口有clientId时 | ||
createWinForClientId(winProps: IWinProps, clientId: string, parentClientId: string | undefined, params: IWinParams): string | null { | ||
return this._createElectronWin(winProps, clientId, parentClientId, params); | ||
} | ||
// 创建一个全新的窗口,使用自生成的clientId | ||
createWin(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string | null { | ||
return this._createWinForElectron(winProps, parentClientId, params); | ||
} | ||
_createWinForElectron(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string | null { | ||
winProps = this._parseWinProps(winProps); | ||
if (params && params.x == null && params.y == null) { | ||
if (parentClientId) { | ||
let window = this._appStore.mainClient.getWindowByClientId(parentClientId); | ||
params.width = params.width || 800; | ||
params.height = params.height || 600; | ||
if (parentClientId && this.clientWins[parentClientId]) { | ||
let window = this.clientWins[parentClientId] as BrowserWindow; | ||
// let window = (this._appStore as any).mainClient.getWindowByClientId(parentClientId); | ||
let bounds = params.useContentSize ? window.getContentBounds() : window.getBounds(); | ||
@@ -146,56 +157,47 @@ params.x = bounds.x + bounds.width / 2 - params.width / 2; | ||
} | ||
let clientId; | ||
let clientId = null; | ||
try { | ||
clientId = this.createElectronWin(url, clientId, parentClientId, params); | ||
clientId = this._createElectronWin(winProps.path!, null, parentClientId, params); | ||
} catch(err) { | ||
console.error(err, err.stack); | ||
} | ||
return clientId; | ||
return clientId; | ||
} | ||
createElectronWin(url, clientId, parentId, params) { | ||
_createElectronWin(url: string | IWinProps, clientId: string | null, parentId: string | undefined, params: IWinState): string | null { | ||
if (clientId && this.clientIds.indexOf(clientId) !== -1) { | ||
return; | ||
return null; | ||
} | ||
let winProps = this._parseWinProps(url); | ||
let winState = new ElectronWindowState(null, params, null); | ||
let winInfo = this.getElectronWin(url, clientId, parentId, winState.state); | ||
let winInfo = this._getElectronWinFromCache(winProps.path!, clientId, parentId, winState.state); | ||
if (!clientId) clientId = winInfo.clientId; | ||
this.clientIds.push(clientId); | ||
this.clientInfoMap[clientId] = {url, parentId}; | ||
let win = winInfo.win; | ||
this._addWinProps(clientId, win, winProps); | ||
this.clientInfoMap[clientId] = { url: winProps.path!, clientId, parentId, winState: winState.state }; | ||
winState.onSave = (state) => { | ||
this.saveWinState(clientId, state); | ||
this.saveWinState(clientId!, state); | ||
}; | ||
this.clientStateMap[clientId] = winState.state; | ||
// this.clientStateMap[clientId] = winState.state; | ||
winState.manage(win); | ||
this.createWins.push(win); | ||
this.saveClients(this.clientIds); // Save clients into Storage | ||
this.saveClients(); // Save clients into Storage | ||
win.on('closed', () => { | ||
let winIndex = this.createWins.indexOf(win); | ||
this.createWins.splice(winIndex, 1); | ||
let winId = this.clientNamedWinIdMap[clientId]; | ||
if (winId) { | ||
this.clientNamedWinIdMap[clientId] = undefined; | ||
this.namedWinIdMap[winId] = undefined; | ||
} | ||
if (this.willQuit) return; | ||
if (clientId === 'mainClient') { | ||
this.willQuit = true; | ||
this._appStore.willQuit = true; | ||
return this.closeAllWindows(); | ||
this.closeAllWindows(); | ||
} | ||
this.onDidWinClose && this.onDidWinClose({ clientId }); | ||
let index = this.clientIds.indexOf(clientId); | ||
this.onDidWinClose && this.onDidWinClose(clientId!); | ||
let index = this.clientIds.indexOf(clientId!); | ||
if (index !== -1) { | ||
this.clientIds.splice(index, 1); | ||
} | ||
this.saveClients(this.clientIds); | ||
if (!this.willQuit) this.saveClients(); | ||
this._removeClientId(clientId!); | ||
}); | ||
@@ -205,11 +207,11 @@ return clientId; | ||
getElectronWin(url, clientId, parentId, params) { | ||
_getElectronWinFromCache(url: string, clientId: string | null, parentId: string | undefined, params: IWinParams) { | ||
// return createMainWindow(url, clientId, params); | ||
let win; | ||
let win: BrowserWindow; | ||
if (clientId) { | ||
win = this.createMainWindow(url, clientId, parentId, params); | ||
} else { | ||
let winInfo = this.windowManager.getWin(); | ||
let winInfo = this.windowManager!.getWin(); | ||
if (!winInfo) { | ||
clientId = this.genClientId(); | ||
clientId = this._genClientId(); | ||
win = this.createMainWindow(url, clientId, parentId, params); | ||
@@ -221,14 +223,14 @@ } else { | ||
// this._appStore.mainClient.sendMessage(win, { action: 'change-props', url, parentId }); | ||
this._appStore.mainClient.changeClientAction(clientId, { url, parentId }); | ||
(this._appStore as any).mainClient.changeClientAction(clientId, { url, parentId }); | ||
let setBoundsFunc = params.useContentSize ? 'setContentBounds' : 'setBounds'; | ||
let setBoundsFunc: 'setContentBounds' | 'setBounds' = params.useContentSize ? 'setContentBounds' : 'setBounds'; | ||
let x = parseInt(params.x) || 0; | ||
let y = parseInt(params.y) || 0; | ||
let width = parseInt(params.width), height = parseInt(params.height); | ||
let x = Math.floor(params.x || 0); | ||
let y = Math.floor(params.y || 0); | ||
let width = Math.floor(params.width || 800), height = Math.floor(params.height || 600); | ||
if (params.minWidth && params.minHeight) { | ||
win.setMinimumSize(parseInt(params.minWidth), parseInt(params.minHeight)); | ||
win.setMinimumSize(Math.floor(params.minWidth), Math.floor(params.minHeight)); | ||
} | ||
if (params.maxWidth && params.maxHeight) { | ||
win.setMaximumSize(parseInt(params.maxWidth), parseInt(params.maxHeight)); | ||
win.setMaximumSize(Math.floor(params.maxWidth), Math.floor(params.maxHeight)); | ||
} | ||
@@ -238,3 +240,3 @@ if (params.title) { | ||
} | ||
win[setBoundsFunc]({ | ||
win[setBoundsFunc]({ | ||
x, y, width, height, | ||
@@ -256,3 +258,3 @@ }); | ||
createWindow(clientId, url = 'empty', parentId = '', params) { | ||
createNativeWindow(clientId: string, url = 'empty', parentId = '', params: IWinParams) { | ||
if (params == null) { | ||
@@ -265,6 +267,9 @@ params = { | ||
} | ||
const window = new BrowserWindow({ | ||
const window = new BrowserWindow({ | ||
show: false, | ||
x: parseInt(params.x), y: parseInt(params.y), | ||
x: Math.floor(params.x || 0), y: Math.floor(params.y || 0), | ||
width: params.width, height: params.height, | ||
minWidth: params.minWidth || params.width, minHeight: params.minHeight || params.height, | ||
maxWidth: params.maxWidth, maxHeight: params.maxHeight, | ||
title: params.title, | ||
useContentSize: params.useContentSize, | ||
@@ -297,4 +302,4 @@ }); | ||
createMainWindow(url, clientId, parentId, params: any = {}) { | ||
let window = this.createWindow(clientId, url, parentId, params); | ||
createMainWindow(url: string, clientId: string, parentId: string | undefined, params: any = {}) { | ||
let window = this.createNativeWindow(clientId, url, parentId, params); | ||
window.on('ready-to-show', function() { | ||
@@ -306,11 +311,15 @@ window.show(); | ||
actionChanged(clientId, action) { | ||
onChangeAction(clientId: string, action: string) { | ||
if (this.clientInfoMap[clientId]) { | ||
this.clientInfoMap[clientId].url = action; | ||
this.saveClients(this.clientIds); | ||
this.saveClients(); | ||
} | ||
} | ||
activeWindow(clientId) { | ||
const win = this._appStore.mainClient.getWindowByClientId(clientId); | ||
onCloseMainClient() { | ||
} | ||
activeWin(clientId: string) { | ||
const win = this.clientWins[clientId] as BrowserWindow; | ||
if (win) { | ||
@@ -317,0 +326,0 @@ win.moveTop(); |
import StoreBase from './MainStoreBase'; | ||
import { declareStoreMap } from './StoreDeclarer'; | ||
import IExtendStoreBase from './IExtendStoreBase'; | ||
export class WinPackStore extends StoreBase { | ||
getSubStores: Function; | ||
@@ -22,8 +22,8 @@ destroy() { | ||
addWin(winId) { | ||
addWin(winId: string) { | ||
let { clientIds } = this.state; | ||
if (clientIds.indexOf(winId) === -1) { | ||
this.setState({ clientIds: [ ...clientIds, winId ] }); | ||
this.winPackMapStore.add(winId, (store) => { | ||
store.clientId = winId; | ||
this.winPackMapStore.add(winId, (store: IExtendStoreBase) => { | ||
(store as any).clientId = winId; | ||
}); | ||
@@ -34,3 +34,3 @@ this.emitter.emit('did-add-win', winId); | ||
deleteWin(winId) { | ||
deleteWin(winId: string) { | ||
let { clientIds } = this.state; | ||
@@ -44,3 +44,3 @@ let index = clientIds.indexOf(winId); | ||
} | ||
if (!this._appStore.willQuit) { | ||
if (!(this._appStore as any).willQuit) { | ||
this.winPackMapStore.get(winId).destroy(); | ||
@@ -55,7 +55,7 @@ } | ||
onDidAddWin(callback) { | ||
onDidAddWin(callback: (clientId: string) => void) { | ||
return this.emitter.on('did-add-win', callback); | ||
} | ||
onDidRemoveWin(callback) { | ||
onDidRemoveWin(callback: (clientId: string) => void) { | ||
return this.emitter.on('did-remove-win', callback); | ||
@@ -62,0 +62,0 @@ } |
@@ -1,5 +0,6 @@ | ||
import StoreBase from 'event-flux/lib/StoreBase'; | ||
const { winManagerStoreName } = require('./constants'); | ||
import StoreBase from './StoreBase'; | ||
import IMultiWinStore, { IWinProps, IWinParams } from './IMultiWinStore'; | ||
import { winManagerStoreName } from './constants'; | ||
function genBrowserUrl(url = '', clientId, parentId) { | ||
function genBrowserUrl(url = '', clientId: string, parentId: string) { | ||
let genUrl = new URL(url, location.href); | ||
@@ -16,13 +17,19 @@ if (genUrl.search) { | ||
export default class MultiWinStore extends StoreBase { | ||
interface IWindow { | ||
close(): void; | ||
} | ||
export default class MultiWinStore extends StoreBase implements IMultiWinStore { | ||
clientIds: string[] = []; | ||
// namedWinId to clientId map | ||
namedWinIdMap: { [winId: string]: string } = {}; | ||
clientIdNameMap: { [clientId: string]: string } = {}; | ||
// clientId to namedWinId map | ||
clientNamedWinIdMap: { [winId: string]: string } = {}; | ||
clientNameIdMap: { [winName: string]: string } = {}; | ||
groupsMap: { [clientId: string]: string[] } = {}; | ||
clientWins: { [clientId: string]: IWindow } = {}; | ||
init() { | ||
this.appStores[winManagerStoreName].observe((state) => { | ||
this.setState({ clientIds: state.clientIds }); | ||
}); | ||
if (typeof window === 'object') { | ||
@@ -32,7 +39,3 @@ window.addEventListener("message", (event) => { | ||
if (action === 'close') { | ||
let winId = this.clientNamedWinIdMap[clientId]; | ||
if (winId) { | ||
this.clientNamedWinIdMap[clientId] = undefined; | ||
this.namedWinIdMap[winId] = undefined; | ||
} | ||
this._removeClientId(clientId); | ||
} | ||
@@ -43,56 +46,145 @@ }); | ||
}); | ||
// 将main window加入到 新创建窗口列表中 | ||
this._addWinProps("mainClient", window, { name: "mainClient", groups: ["main"] }); | ||
} | ||
} | ||
createWin(url, parentClientId, params) { | ||
let clientId; | ||
// 新增clientId对应的 winProps | ||
_addWinProps(clientId: string, win: IWindow | null, winProps: IWinProps) { | ||
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; | ||
} | ||
} | ||
_parseWinProps(winProps: string | IWinProps): IWinProps { | ||
let parseProps: IWinProps = typeof winProps === 'string' ? { path: winProps } : winProps; | ||
// 默认窗口被分组到main分组 | ||
if (parseProps.groups === undefined) { | ||
parseProps.groups = ['main']; | ||
} | ||
return parseProps; | ||
} | ||
// 删除窗口的clientId | ||
_removeClientId(clientId: string) { | ||
let name = this.clientIdNameMap[clientId]; | ||
delete this.clientWins[clientId]; | ||
if (name) { | ||
delete this.clientNameIdMap[name] | ||
delete this.clientIdNameMap[clientId]; | ||
} | ||
delete this.groupsMap[clientId]; | ||
let index = this.clientIds.indexOf(clientId); | ||
if (index !== -1) this.clientIds.splice(index, 1); | ||
} | ||
_createWinForBrowser(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string { | ||
let clientId = this._genClientId(); | ||
// get url from winProps | ||
winProps = this._parseWinProps(winProps); | ||
// create new browser window | ||
let win = this.createBrowserWin(genBrowserUrl(winProps.path, clientId, parentClientId), clientId, params); | ||
this._addWinProps(clientId, win, winProps); | ||
(this._appStore! as any).mainClient.addWin(clientId, win); | ||
return clientId; | ||
} | ||
_createWinForElectron(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string | null { | ||
return ""; | ||
} | ||
createWin(winProps: IWinProps | string, parentClientId: string, params: IWinParams): string | null { | ||
if (typeof window === 'object') { | ||
clientId = this.genClientId(); | ||
let win = this.createBrowserWin(genBrowserUrl(url, clientId, parentClientId), clientId, params); | ||
this._appStore.mainClient.addWin(clientId, win); | ||
return this._createWinForBrowser(winProps, parentClientId, params); | ||
} else { | ||
try { | ||
clientId = this.createElectronWin(url, clientId, parentClientId, params); | ||
} catch(err) { | ||
console.error(err, err.stack); | ||
} | ||
return this._createWinForElectron(winProps, parentClientId, params); | ||
} | ||
return clientId; | ||
} | ||
// Create new win if the specific winId is not exists | ||
createOrOpenWin(winId, url, parentClientId, params) { | ||
if (!this.namedWinIdMap[winId]) { | ||
let clientId = this.createWin(url, parentClientId, params); | ||
this.namedWinIdMap[winId] = clientId; | ||
this.clientNamedWinIdMap[clientId] = winId; | ||
return clientId; | ||
createOrOpenWin(winName: string, url: string | IWinProps, parentClientId: string, params: any): string | null { | ||
// winName对应的窗口未存在,则创建新窗口 | ||
if (!this.clientNameIdMap[winName]) { | ||
let winProps: IWinProps = typeof url === 'string' ? { path: url, name: winName } : { ...url, name: winName }; | ||
return this.createWin(winProps, parentClientId, params); | ||
} else { | ||
let clientId = this.namedWinIdMap[winId]; | ||
this.changeClientAction(clientId, url); | ||
this.activeWindow(clientId); | ||
let clientId = this.clientNameIdMap[winName]; | ||
this.changeClientAction(clientId, typeof url === 'string' ? url : url.path!); | ||
this.activeWin(clientId); | ||
return clientId; | ||
} | ||
} | ||
closeWin(clientId: string) { | ||
let win: IWindow | undefined = this.clientWins[clientId]; | ||
if (win) win.close(); | ||
} | ||
closeWin(clientId) { | ||
if (typeof window === 'object') { | ||
} else { | ||
closeWinByName(name: string) { | ||
let clientId = this.clientNameIdMap[name]; | ||
if (clientId) this.closeWin(clientId); | ||
} | ||
closeWinByGroup(group: string) { | ||
for (let clientId of this.clientIds) { | ||
let groups = this.groupsMap[clientId]; | ||
if (groups && groups.indexOf(group) !== -1) { | ||
this.closeWin(clientId); | ||
} | ||
} | ||
} | ||
closeWinByWinId(winId) { | ||
let clientId = this.namedWinIdMap[winId]; | ||
clientId && this.closeWin(clientId); | ||
activeWin(clientId: string) {} | ||
activeWindow(clientId: string) { | ||
this.activeWin(clientId); | ||
} | ||
sendWinMsg(winId: string, message: any) { | ||
this._appStore.mainClient.sendMessageByClientId(winId, message); | ||
activeWinByName(name: string) { | ||
let clientId = this.clientNameIdMap[name]; | ||
if (clientId) this.activeWin(clientId); | ||
} | ||
genClientId() { | ||
activeWinByGroup(group: string) { | ||
for (let clientId of this.clientIds) { | ||
let groups = this.groupsMap[clientId]; | ||
if (groups && groups.indexOf(group) !== -1) { | ||
this.activeWin(clientId); | ||
} | ||
} | ||
} | ||
sendWinMsg(clientId: string, message: any) { | ||
(this._appStore as any).mainClient.sendWinMsg(clientId, message); | ||
} | ||
sendWinMsgByName(name: string, message: any) { | ||
let clientId = this.clientNameIdMap[name]; | ||
if (clientId) this.sendWinMsg(clientId, message); | ||
} | ||
sendWinMsgByGroup(group: string, message: any) { | ||
for (let clientId of this.clientIds) { | ||
let groups = this.groupsMap[clientId]; | ||
if (groups && groups.indexOf(group) !== -1) { | ||
this.sendWinMsg(clientId, message); | ||
} | ||
} | ||
} | ||
_genClientId(): string { | ||
let clientId = 'win' + Math.floor(Math.random() * 10000); | ||
if (this.state.clientIds.indexOf(clientId) !== -1) { | ||
return this.genClientId(); | ||
return this._genClientId(); | ||
} | ||
@@ -103,8 +195,8 @@ return clientId; | ||
closeAllWindows() { | ||
this._appStore.mainClient.closeAllWindows(); | ||
this.namedWinIdMap = {}; | ||
this.clientNamedWinIdMap = {}; | ||
this.clientIds.slice(0).forEach(clientId => { | ||
this.closeWin(clientId); | ||
}); | ||
} | ||
createBrowserWin(url, clientId, params: any = {}) { | ||
createBrowserWin(url: string, clientId: string, params: any = {}): Window | null { | ||
if (!params.width) params.width = 400; | ||
@@ -114,22 +206,22 @@ if (!params.height) params.height = 400; | ||
let childWin = window.open(url, "newwindow", featureStr + ", toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no, titlebar=no"); | ||
if (childWin) childWin.onbeforeunload = () => this._removeClientId(clientId); | ||
return childWin; | ||
} | ||
createElectronWin(url, clientId, parentClientId, params) { | ||
createElectronWin(url: string, clientId: string, parentClientId: string, params: any): string | undefined { | ||
console.error('Please provide the createElectronWin'); | ||
return; | ||
} | ||
actionChanged(clientId, action) { | ||
onChangeAction(clientId: string, action: string) { | ||
} | ||
changeClientAction(clientId, url) { | ||
this._appStore.mainClient.changeClientAction(clientId, { url }); | ||
changeClientAction(clientId: string, url: string) { | ||
((this._appStore as any).mainClient as any).changeClientAction(clientId, { url }); | ||
} | ||
getWinRootStore(clientId) { | ||
return this.appStores[winManagerStoreName].winPackMapStore.get(clientId); | ||
getWinRootStore(clientId: string) { | ||
return (this.appStores[winManagerStoreName] as any).winPackMapStore.get(clientId); | ||
} | ||
activeWindow(clientId) {} | ||
} |
@@ -1,2 +0,2 @@ | ||
import AppStore from 'event-flux/lib/AppStore'; | ||
import AppStore from './AppStore'; | ||
import objectMerge from './utils/objectMerge'; | ||
@@ -8,3 +8,6 @@ import { serialize, deserialize } from 'json-immutable-bn'; | ||
import { filterOneStore } from './utils/filterStore'; | ||
import loggerApply, { Log } from './utils/loggerApply'; | ||
import loggerApply, { Log, Logger } from './utils/loggerApply'; | ||
import IStoresDeclarer, { IStoresObjDeclarer } from './IStoresDeclarer'; | ||
import IExtendStoreBase, { IExtendStoreBaseConstructor } from './IExtendStoreBase'; | ||
import { StoreBaseConstructor } from './StoreBase'; | ||
@@ -18,3 +21,3 @@ class IDGenerator { | ||
dispose(id) { | ||
dispose(id: number) { | ||
} | ||
@@ -24,14 +27,14 @@ } | ||
export class RendererAppStore extends AppStore { | ||
emitter: Emitter; | ||
emitter: Emitter = new Emitter(); | ||
client: any; | ||
idGenerator = new IDGenerator(); | ||
resolveMap = {}; | ||
resolveMap: { [invokeId: string]: { resolve: (res: any) => void, reject: (err: Error) => void } } = {}; | ||
storeShape: any; | ||
storeProxyHandler = new StoreProxyHandler(); | ||
storeResolve: () => void; | ||
storeResolve?: () => void; | ||
winInitParams: any; | ||
log: Log; | ||
static innerStores; | ||
static innerStores: IStoresDeclarer; | ||
@@ -45,9 +48,5 @@ constructor(log: Log) { | ||
super.init(); | ||
this.emitter = new Emitter(); | ||
let filter = true; | ||
// 先初始化,防止由于promise的异步 漏掉某些消息 | ||
this.client = new RendererClient( | ||
filter, | ||
this.handleStorePromise, | ||
@@ -65,7 +64,7 @@ this.handleAction.bind(this), | ||
handleStorePromise = (state, store) => { | ||
this.handleStore(this.storeResolve, true, state, store); | ||
handleStorePromise = (state: any, store: any) => { | ||
this.handleStore(this.storeResolve!, state, store); | ||
}; | ||
handleStore(resolve, filter, state, store) { | ||
handleStore(resolve: () => void, state: any, store: any) { | ||
const storeData = deserialize(state); | ||
@@ -76,3 +75,3 @@ const initialState = storeData; | ||
const storeFilters = JSON.parse(store); | ||
let stores = this.storeProxyHandler.proxyStores(storeFilters, (action) => { | ||
let stores = this.storeProxyHandler.proxyStores(storeFilters, (action: any) => { | ||
let invokeId = this.idGenerator.genID(); | ||
@@ -87,4 +86,4 @@ this.client.forward(invokeId, serialize(action)); | ||
handleAction(action) { | ||
action = deserialize(action); | ||
handleAction(actionStr: string) { | ||
let action = deserialize(actionStr); | ||
const { updated, deleted } = action.payload; | ||
@@ -97,6 +96,6 @@ // const withDeletions = filterObject(this.state, deleted); | ||
handleResult(invokeId, error, result) { | ||
handleResult(invokeId: number, error: Error, result: any) { | ||
this.idGenerator.dispose(invokeId); | ||
let {resolve, reject} = this.resolveMap[invokeId]; | ||
this.resolveMap[invokeId] = null; | ||
delete this.resolveMap[invokeId]; | ||
if (error) { | ||
@@ -110,11 +109,11 @@ reject(error); | ||
handleMessage(message) { | ||
handleMessage(message: any) { | ||
this.emitter.emit('did-message', message); | ||
} | ||
handleWinMessage(senderId, message) { | ||
handleWinMessage(senderId: string, message: any) { | ||
this.emitter.emit('did-win-message', {senderId, message}); | ||
} | ||
handleInitWindow(params) { | ||
handleInitWindow(params: any) { | ||
this.winInitParams = params; | ||
@@ -125,3 +124,3 @@ this.emitter.emit('did-init-window', params); | ||
observeInitWindow(callback) { | ||
observeInitWindow(callback: (params: any) => void) { | ||
if (this.winInitParams) { | ||
@@ -134,11 +133,11 @@ callback(this.winInitParams); | ||
sendWindowMessage(clientId, args) { | ||
sendWindowMessage(clientId: string, args: any) { | ||
this.client.sendWindowMessage(clientId, args); | ||
} | ||
onDidMessage(callback) { | ||
onDidMessage(callback: (message: any) => void) { | ||
return this.emitter.on('did-message', callback); | ||
} | ||
onDidClose(callback) { | ||
onDidClose(callback: () => void) { | ||
return this.emitter.on('did-message', (message) => { | ||
@@ -149,3 +148,3 @@ if (message && message.action === 'did-close') callback(); | ||
onDidWinMessage(callback) { | ||
onDidWinMessage(callback: ({ senderId, message }: { senderId: string, message: any }) => void) { | ||
return this.emitter.on('did-win-message', callback); | ||
@@ -160,7 +159,7 @@ } | ||
getStore(key) { | ||
getStore(key: string) { | ||
return this.stores[key] | ||
} | ||
setStore(key, store) { | ||
setStore(key: string, store: IExtendStoreBase) { | ||
return this.stores[key] = store; | ||
@@ -172,3 +171,3 @@ } | ||
// 初始化子Stores | ||
initStores(parent) {} | ||
initStores(parent: AppStore) {} | ||
// 开始监听子Store改变 | ||
@@ -178,5 +177,5 @@ startObserve() {} | ||
export default function buildRendererAppStore(stores, onChange, logger) { | ||
export default function buildRendererAppStore(stores: IStoresObjDeclarer, onChange: (state: any) => void, logger: Logger) { | ||
RendererAppStore.innerStores = stores; | ||
const storeShape = filterOneStore(RendererAppStore); | ||
const storeShape = filterOneStore(RendererAppStore as any as IExtendStoreBaseConstructor); | ||
const appStore = new RendererAppStore(loggerApply(logger)); | ||
@@ -183,0 +182,0 @@ appStore.onDidChange(onChange); |
@@ -1,3 +0,3 @@ | ||
export default window['process'] ? | ||
export default (window as any)['process'] ? | ||
require('./ElectronRendererClient').default : | ||
require('./BrowserRendererClient').default; |
@@ -1,6 +0,9 @@ | ||
import buildRendererAppStore from './RendererAppStore'; | ||
import buildRendererAppStore, { RendererAppStore } from './RendererAppStore'; | ||
import { Emitter } from 'event-kit'; | ||
import IExtendStoreBase from './IExtendStoreBase'; | ||
import IStoresDeclarer, { IStoresObjDeclarer } from './IStoresDeclarer'; | ||
import { Logger } from './utils/loggerApply'; | ||
function getQuery() { | ||
let query = {}; | ||
let query: { [key: string]: string } = {}; | ||
window.location.search.slice(1).split('&').forEach(item => { | ||
@@ -15,7 +18,7 @@ let [key, val] = item.split('='); | ||
emitter = new Emitter(); | ||
store: any; | ||
messages = []; | ||
store: RendererAppStore; | ||
messages: any[] = []; | ||
childId: any; | ||
constructor(store, createPromise) { | ||
constructor(store: RendererAppStore, createPromise: Promise<string>) { | ||
this.emitter = new Emitter(); | ||
@@ -30,14 +33,14 @@ this.store = store; | ||
}); | ||
this.store.onDidWinMessage(this.handleWinMessage.bind(this)); | ||
(this.store as any).onDidWinMessage(this.handleWinMessage.bind(this)); | ||
} | ||
send(message) { | ||
send(message: any) { | ||
if (!this.childId) { | ||
this.messages.push(message); | ||
} else { | ||
this.store.sendWindowMessage(this.childId, message); | ||
(this.store as any).sendWindowMessage(this.childId, message); | ||
} | ||
} | ||
handleWinMessage({senderId, message}) { | ||
handleWinMessage({ senderId, message }: { senderId: string, message: string }) { | ||
if (senderId === this.childId) { | ||
@@ -48,3 +51,3 @@ this.emitter.emit('message', message); | ||
onDidReceiveMsg(callback) { | ||
onDidReceiveMsg(callback: (message: any) => void) { | ||
return this.emitter.on('message', callback); | ||
@@ -55,7 +58,7 @@ } | ||
class ParentWindowProxy { | ||
store: any; | ||
parentId: any; | ||
store: RendererAppStore; | ||
parentId: string; | ||
emitter: Emitter; | ||
constructor(store, parentId) { | ||
constructor(store: RendererAppStore, parentId: string) { | ||
this.store = store; | ||
@@ -68,7 +71,7 @@ this.parentId = parentId; | ||
send(message) { | ||
send(message: any) { | ||
this.store.sendWindowMessage(this.parentId, message); | ||
} | ||
handleWinMessage({senderId, message}) { | ||
handleWinMessage({ senderId, message }: { senderId: string, message: any }) { | ||
if (senderId === this.parentId) { | ||
@@ -79,3 +82,3 @@ this.emitter.emit('message', message); | ||
onDidReceiveMsg(callback) { | ||
onDidReceiveMsg(callback: (message: any) => void) { | ||
return this.emitter.on('message', callback); | ||
@@ -90,6 +93,6 @@ } | ||
export default function rendererInit(rendererStores, options: RenderOptions = {}, logger) { | ||
export default function rendererInit(rendererStores: IStoresObjDeclarer, options: RenderOptions = {}, logger: Logger) { | ||
let query = getQuery(); | ||
window['clientId'] = query['clientId'] || 'mainClient'; | ||
window['parentId'] = query['parentId']; | ||
(window as any)['clientId'] = query['clientId'] || 'mainClient'; | ||
(window as any)['parentId'] = query['parentId']; | ||
@@ -100,16 +103,16 @@ // if (!window['clientId']) { // Not a renderer window | ||
function getAction() { | ||
if (window['process']) return query['url'] || '/'; | ||
if ((window as any)['process']) return query['url'] || '/'; | ||
return window.location.pathname + window.location.search + window.location.hash; | ||
} | ||
window['action'] = getAction(); | ||
(window as any)['action'] = getAction(); | ||
const store = buildRendererAppStore(rendererStores, options.renderHandler, logger); | ||
const genProxy = (store, multiWinStore) => { | ||
const genProxy = (store: RendererAppStore, multiWinStore: IExtendStoreBase) => { | ||
return new Proxy(multiWinStore, { | ||
get: function(target, propName) { | ||
get: function(target: any, propName: string) { | ||
if (!propName) return; | ||
if (propName === 'createWin') { | ||
return function(url, params) { | ||
return new ChildWindowProxy(store, target[propName](url, window['clientId'], params)); | ||
return function(url: string, params: any) { | ||
return new ChildWindowProxy(store, target[propName](url, (window as any)['clientId'], params)); | ||
} | ||
@@ -125,4 +128,4 @@ } else { | ||
store.stores['multiWinStore'] = genProxy(store, store.stores['multiWinStore']); | ||
if (window['parentId']) { | ||
window['parentWin'] = new ParentWindowProxy(store, window['parentId']); | ||
if ((window as any)['parentId']) { | ||
(window as any)['parentWin'] = new ParentWindowProxy(store, (window as any)['parentId']); | ||
} | ||
@@ -133,10 +136,10 @@ store.observeInitWindow((message) => { | ||
// change-props | ||
window['action'] = url; | ||
window['parentId'] = parentId; | ||
if (!window['parentWin']) { | ||
window['parentWin'] = new ParentWindowProxy(store, window['parentId']); | ||
(window as any)['action'] = url; | ||
(window as any)['parentId'] = parentId; | ||
if (!(window as any)['parentWin']) { | ||
(window as any)['parentWin'] = new ParentWindowProxy(store, (window as any)['parentId']); | ||
} else { | ||
window['parentWin']['parentId'] = parentId; | ||
(window as any)['parentWin']['parentId'] = parentId; | ||
} | ||
options.actionHandler && options.actionHandler(window['action'], window['parentWin']); | ||
options.actionHandler && options.actionHandler((window as any)['action'], (window as any)['parentWin']); | ||
}); | ||
@@ -143,0 +146,0 @@ return store; |
@@ -1,8 +0,9 @@ | ||
const Store = require('electron-store'); | ||
import IStorage from "./IStorage"; | ||
import Store = require("electron-store"); | ||
export default class ElectronStore { | ||
export default class ElectronStore implements IStorage { | ||
store: any; | ||
ns: string; | ||
constructor(version, store, ns) { | ||
constructor(version: string | null, store: Store<string>, ns: string) { | ||
this.store = store || new Store(); | ||
@@ -19,3 +20,3 @@ this.ns = ns; | ||
getNSStore(ns) { | ||
getNSStore(ns: string): IStorage { | ||
ns = this.ns ? this.ns + '.' + ns : ns; | ||
@@ -25,7 +26,7 @@ return new ElectronStore(null, this.store, ns); | ||
set(key, value) { | ||
set(key: string | { [key: string]: any }, value: any) { | ||
if (!this.ns) return this.store.set(key, value); | ||
let ns = this.ns; | ||
if (typeof key === 'object') { | ||
let newObj = {}; | ||
let newObj: { [key: string]: any } = {}; | ||
for (let k in key) { | ||
@@ -39,3 +40,3 @@ newObj[ns + '.' + k] = key[k]; | ||
get(key, defaultValue) { | ||
get(key: string, defaultValue?: any) { | ||
let ns = this.ns; | ||
@@ -46,3 +47,3 @@ if (!ns) return this.store.get(key, defaultValue); | ||
delete(key) { | ||
delete(key: string) { | ||
let ns = this.ns; | ||
@@ -49,0 +50,0 @@ if (!ns) return this.store.delete(key); |
export default typeof window === 'object' ? | ||
require('./LocalStore').default : | ||
require('./ElectronStore').default; |
@@ -1,5 +0,7 @@ | ||
export default class AsyncStorage { | ||
import IStorage from "./IStorage"; | ||
export default class AsyncStorage implements IStorage { | ||
// Update the version when the storage is obsolete | ||
ns: string; | ||
constructor(version, ns) { | ||
constructor(version: string | null, ns: string) { | ||
if (version) this.init(version); | ||
@@ -9,3 +11,3 @@ this.ns = ns; | ||
init(version) { | ||
init(version: string) { | ||
if (version) { | ||
@@ -20,3 +22,3 @@ const curVersion = localStorage.getItem('version'); | ||
set(key, value) { | ||
set(key: string | { [key: string]: any }, value: any) { | ||
if (typeof key === 'object') { | ||
@@ -36,3 +38,3 @@ for (let k in key) { | ||
get(key, defaultValue) { | ||
get(key: string, defaultValue?: any) { | ||
key = this.ns ? this.ns + '.' + key : key; | ||
@@ -43,3 +45,3 @@ let value = localStorage.getItem(key); | ||
delete(key) { | ||
delete(key: string) { | ||
key = this.ns ? this.ns + '.' + key : key; | ||
@@ -49,3 +51,3 @@ localStorage.removeItem(key); | ||
getNSStore(namespace) { | ||
getNSStore(namespace: string) { | ||
namespace = this.ns ? this.ns + '.' + namespace : namespace; | ||
@@ -52,0 +54,0 @@ return new AsyncStorage(null, namespace); |
@@ -0,1 +1,3 @@ | ||
import StoreBase, { StoreBaseConstructor } from "./StoreBase"; | ||
interface StoreDeclarerOptions { | ||
@@ -8,12 +10,20 @@ args?: [any]; | ||
const IS_STORE = '@@__STORE_ITEM__@@'; | ||
function StoreDeclarer(Store, options?: StoreDeclarerOptions) { | ||
this.Store = Store; | ||
this.options = options; | ||
class StoreDeclarer { | ||
Store: StoreBaseConstructor; | ||
options: StoreDeclarerOptions | undefined; | ||
constructor(Store: StoreBaseConstructor, options?: StoreDeclarerOptions) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
[IS_STORE]: true; | ||
static isStore(maybeStore: any) { | ||
return !!(maybeStore && maybeStore[IS_STORE]); | ||
} | ||
} | ||
StoreDeclarer.prototype[IS_STORE] = true; | ||
StoreDeclarer.isStore = function(maybeStore) { | ||
return !!(maybeStore && maybeStore[IS_STORE]); | ||
} | ||
function declareStore(Store, options?: StoreDeclarerOptions) { | ||
function declareStore(Store: StoreBaseConstructor, options?: StoreDeclarerOptions) { | ||
return new StoreDeclarer(Store, options); | ||
@@ -28,13 +38,20 @@ } | ||
const IS_STORE_LIST = '@@__STORE_LIST__@@'; | ||
function StoreListDeclarer(Store, options?: StoreListDeclarerOptions) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
StoreListDeclarer.prototype[IS_STORE_LIST] = true; | ||
StoreListDeclarer.isStoreList = function(maybeList) { | ||
return !!(maybeList && maybeList[IS_STORE_LIST]); | ||
class StoreListDeclarer { | ||
Store: StoreBaseConstructor; | ||
options: StoreListDeclarerOptions | undefined; | ||
constructor(Store: StoreBaseConstructor, options?: StoreListDeclarerOptions) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
[IS_STORE_LIST]: true; | ||
static isStoreList(maybeList: any) { | ||
return !!(maybeList && maybeList[IS_STORE_LIST]); | ||
} | ||
} | ||
function declareStoreList(Store, options?: StoreListDeclarerOptions) { | ||
function declareStoreList(Store: StoreBaseConstructor, options?: StoreListDeclarerOptions) { | ||
return new StoreListDeclarer(Store, options); | ||
@@ -53,13 +70,20 @@ } | ||
const IS_STORE_MAP = '@@__STORE_MAP__@@'; | ||
function StoreMapDeclarer(Store, options?: StoreMapDeclarerOptions) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
StoreMapDeclarer.prototype[IS_STORE_MAP] = true; | ||
StoreMapDeclarer.isStoreMap = function(maybeMap) { | ||
return !!(maybeMap && maybeMap[IS_STORE_MAP]); | ||
class StoreMapDeclarer { | ||
Store: StoreBaseConstructor; | ||
options: StoreMapDeclarerOptions | undefined; | ||
constructor(Store: StoreBaseConstructor, options?: StoreMapDeclarerOptions) { | ||
this.Store = Store; | ||
this.options = options; | ||
} | ||
[IS_STORE_MAP]: true; | ||
static isStoreMap(maybeMap: any) { | ||
return !!(maybeMap && maybeMap[IS_STORE_MAP]); | ||
} | ||
} | ||
function declareStoreMap(Store, options?: StoreMapDeclarerOptions) { | ||
function declareStoreMap(Store: StoreBaseConstructor, options?: StoreMapDeclarerOptions) { | ||
return new StoreMapDeclarer(Store, options); | ||
@@ -66,0 +90,0 @@ } |
@@ -1,2 +0,3 @@ | ||
import StoreBase from 'event-flux/lib/StoreBase'; | ||
import StoreBase from './StoreBase'; | ||
import { Disposable } from 'event-kit'; | ||
@@ -15,13 +16,13 @@ class SubIdGenerator { | ||
export default class SubStoreBase extends StoreBase { | ||
subMap = {}; | ||
subMap: { [subId: string]: Disposable } = {}; | ||
idGenerator = new SubIdGenerator(); | ||
unsubscribe(subId) { | ||
unsubscribe(subId: string) { | ||
let dispose = this.subMap[subId]; | ||
if (!dispose) console.error(`The subId ${subId} isnot subscribed`); | ||
dispose && dispose.dispose(); | ||
this.subMap[subId] = null; | ||
delete this.subMap[subId]; | ||
} | ||
genSubId(dispose) { | ||
genSubId(dispose: Disposable) { | ||
let id = this.idGenerator.genId(); | ||
@@ -28,0 +29,0 @@ this.subMap[id] = dispose; |
@@ -0,0 +0,0 @@ import filterApply from '../filterApply'; |
@@ -0,0 +0,0 @@ import filterDifference from '../filterDifference'; |
@@ -0,0 +0,0 @@ import { filterWindowStore, filterWindowState, filterWindowDelta } from '../filterWindowStore'; |
@@ -0,0 +0,0 @@ import objectDifference from '../objectDifference'; |
@@ -0,0 +0,0 @@ import objectDifference from '../objectDifference'; |
@@ -0,0 +0,0 @@ import StoreProxyHandler from '../StoreProxyHandler'; |
@@ -1,2 +0,3 @@ | ||
import { addStateFilter, addStateFilterForMap } from '../stateFilterDecorator'; | ||
import addStateFilter from '../stateFilterDecorator'; | ||
import addStateFilterForMap from '../stateFilterMapDecorator'; | ||
import { Emitter } from 'event-kit'; | ||
@@ -6,3 +7,3 @@ | ||
emitter = new Emitter(); | ||
clientIds = []; | ||
clientIds: string[] = []; | ||
@@ -13,3 +14,3 @@ getClienIds() { | ||
addWin(clientId) { | ||
addWin(clientId: string) { | ||
this.clientIds.push(clientId); | ||
@@ -19,3 +20,3 @@ this.emitter.emit('add-win', clientId); | ||
removeWin(clientId) { | ||
removeWin(clientId: string) { | ||
let index = this.clientIds.indexOf(clientId); | ||
@@ -26,7 +27,7 @@ if (index !== -1) this.clientIds.splice(index, 1); | ||
onDidAddWin(callback) { | ||
onDidAddWin(callback: (win: any) => void) { | ||
return this.emitter.on('add-win', callback); | ||
} | ||
onDidRemoveWin(callback) { | ||
onDidRemoveWin(callback: (clientId: string) => void) { | ||
return this.emitter.on('remove-win', callback); | ||
@@ -36,3 +37,3 @@ } | ||
let managerStore, Base1Class; | ||
let managerStore: MockManagerStore, Base1Class: any; | ||
@@ -47,3 +48,3 @@ beforeEach(() => { | ||
batchUpdater = { | ||
addTask: (fn) => { fn() } | ||
addTask: (fn: () => void) => { fn() } | ||
}; | ||
@@ -64,3 +65,3 @@ | ||
let StateFilterClass = addStateFilter(Base2Class); | ||
let StateFilterClass = addStateFilter(Base2Class as any); | ||
let stateFilterInstance = new StateFilterClass(); | ||
@@ -112,3 +113,3 @@ | ||
let StateFilterClass = addStateFilter(Base2Class); | ||
let StateFilterClass = addStateFilter(Base2Class as any); | ||
let stateFilterInstance = new StateFilterClass(); | ||
@@ -134,3 +135,3 @@ base1Instance._initWrap(); | ||
let StateFilterClass = addStateFilterForMap(Base2Class); | ||
let StateFilterClass: any = addStateFilterForMap(Base2Class as any); | ||
let stateFilterInstance = new StateFilterClass(); | ||
@@ -179,3 +180,3 @@ | ||
let StateFilterClass = addStateFilterForMap(Base2Class); | ||
let StateFilterClass: any = addStateFilterForMap(Base2Class as any); | ||
let stateFilterInstance = new StateFilterClass(); | ||
@@ -238,3 +239,3 @@ | ||
storeMap = new Map([['item1', base1Instance], ['item2', base2Instance]]); | ||
add(key, prevInit) { | ||
add(key: string) { | ||
let newStore = new Base1DeriveClass(); | ||
@@ -244,6 +245,6 @@ newStore._initWrap(); | ||
} | ||
delete(key) { } | ||
delete(key: string) { } | ||
} | ||
let StateFilterClass = addStateFilterForMap(Base2Class); | ||
let StateFilterClass: any = addStateFilterForMap(Base2Class as any); | ||
let stateFilterInstance = new StateFilterClass(); | ||
@@ -250,0 +251,0 @@ |
@@ -0,0 +0,0 @@ /** |
@@ -30,7 +30,10 @@ /* | ||
*/ | ||
import { isObject } from './objUtils'; | ||
const isObject = require('lodash/isObject'); | ||
interface IFilterObject { | ||
[key: string]: any; | ||
} | ||
export default function filterApply(origin, updated, deleted) { | ||
let merged = {}; | ||
export default function filterApply(origin: IFilterObject, updated: IFilterObject, deleted: IFilterObject | null) { | ||
let merged: IFilterObject = {}; | ||
if (updated['*']) { | ||
@@ -60,3 +63,3 @@ if (Array.isArray(origin)) { | ||
if (isObject(deleted)) { | ||
Object.keys(deleted).forEach(key => { | ||
Object.keys(deleted!).forEach(key => { | ||
merged[key] = undefined; | ||
@@ -63,0 +66,0 @@ }); |
@@ -15,8 +15,11 @@ /* | ||
const isObject = require('lodash/isObject'); | ||
const isEmpty = require('lodash/isEmpty'); | ||
import { isObject, isEmpty } from './objUtils'; | ||
const isShallow = val => Array.isArray(val) || !isObject(val); | ||
const isShallow = (val: any) => Array.isArray(val) || !isObject(val); | ||
function checkUpdateVal(key, old, curr, updated, deleted) { | ||
interface IFilterObject { | ||
[key: string]: any; | ||
} | ||
function checkUpdateVal(key: string, old: IFilterObject, curr: IFilterObject, updated: IFilterObject, deleted: IFilterObject) { | ||
let oldVal = old[key]; | ||
@@ -43,7 +46,7 @@ let currVal = curr[key]; | ||
function filterDifference(old, curr) { | ||
function filterDifference(old: IFilterObject, curr: IFilterObject) { | ||
if (old == null || curr == null) return { updated: curr, deleted: {} }; | ||
const updated = {}; | ||
const deleted = {}; | ||
const updated: IFilterObject = {}; | ||
const deleted: IFilterObject = {}; | ||
@@ -50,0 +53,0 @@ Object.keys(curr).forEach(key => checkUpdateVal(key, old, curr, updated, deleted)); |
@@ -12,9 +12,8 @@ /* | ||
const keys = require('lodash/keys'); | ||
const isObject = require('lodash/isObject'); | ||
import { isObject } from './objUtils'; | ||
module.exports = function filterObject(source, filter) { | ||
module.exports = function filterObject(source: { [key: string]: any }, filter: any) { | ||
if (!source || filter === true) return {}; | ||
let filtered = {}; | ||
keys(source).forEach((key) => { | ||
let filtered: { [key: string]: any } = {}; | ||
Object.keys(source).forEach((key) => { | ||
if (isObject(filter[key])) { | ||
@@ -21,0 +20,0 @@ filtered[key] = filterObject(source[key], filter[key]); |
@@ -12,18 +12,24 @@ /* | ||
import { beforeInit } from './storeBuilder'; | ||
const isFunction = require('lodash/isFunction'); | ||
import StoreBase, { StoreBaseConstructor } from '../StoreBase'; | ||
import IStoreFilters from '../IStoreFilters'; | ||
import IExtendStoreBase, { IExtendStoreBaseConstructor, ISubStoreInfo } from '../IExtendStoreBase'; | ||
import IStoresDeclarer, { IStoresArrayDeclarer } from '../IStoresDeclarer'; | ||
import { isFunction } from './objUtils'; | ||
const storeBuilders = { | ||
Item: function (StoreClass, storeKey, stateKey, options) { | ||
Item: function (this: IExtendStoreBase, StoreClass: IExtendStoreBaseConstructor, storeKey: string, stateKey: string, options: any): void { | ||
let args = options && options.args; | ||
options = options ? { defaultFilter: options.defaultFilter } : null; | ||
if (this.setStore) { | ||
return this.setStore(storeKey, this.buildStore(StoreClass, args, options)); | ||
let store = this.buildStore(StoreClass, args, options); | ||
if (this.setStore && store) { | ||
return this.setStore(storeKey, store); | ||
} else if (store) { | ||
(this as any)[storeKey] = store; | ||
} | ||
this[storeKey] = this.buildStore(StoreClass, args, options); | ||
}, | ||
List: function (StoreClass, storeKey, stateKey, options) { | ||
List: function (this: IExtendStoreBase, StoreClass: IExtendStoreBaseConstructor, storeKey: string, stateKey: string, options: any): void { | ||
let args = options && options.args; | ||
let storeOptions = options ? { defaultFilter: options.storeDefaultFilter } : null; | ||
let storeBuilder = () => this.buildStore(StoreClass, args, storeOptions); | ||
let storeObserver = (store, index) => { | ||
let storeObserver = (store: IExtendStoreBase, index: number) => { | ||
return store.observe(state => { | ||
@@ -42,11 +48,11 @@ let oldStates = this.state[stateKey] || []; | ||
let newStore = new StoreList(0, storeBuilder, storeObserver, listOptions); | ||
newStore.appStores = this._appStore.stores; | ||
newStore.appStores = this._appStore!.stores; | ||
if (this.setStore) return this.setStore(storeKey, newStore); | ||
this[storeKey] = newStore; | ||
(this as any)[storeKey] = newStore; | ||
}, | ||
Map: function (StoreClass, storeKey, stateKey, options) { | ||
Map: function (this: IExtendStoreBase, StoreClass: IExtendStoreBaseConstructor, storeKey: string, stateKey: string, options: any) { | ||
let args = options && options.args; | ||
let storeOptions = options ? { defaultFilter: options.storeDefaultFilter } : null; | ||
let storeBuilder = () => this.buildStore(StoreClass, args, storeOptions); | ||
let storeObserver = (store, index) => { | ||
let storeObserver = (store: IExtendStoreBase, index: string) => { | ||
if (!stateKey) { | ||
@@ -70,5 +76,5 @@ return store.observe(state => | ||
} | ||
newStore.appStores = this._appStore.stores; | ||
newStore.appStores = this._appStore!.stores; | ||
if (this.setStore) return this.setStore(storeKey, newStore); | ||
this[storeKey] = newStore; | ||
(this as any)[storeKey] = newStore; | ||
} | ||
@@ -78,4 +84,4 @@ } | ||
const storeObservers = { | ||
Item: function (storeKey, stateKey) { | ||
let store = this.getStore ? this.getStore(storeKey) : this[storeKey]; | ||
Item: function (this: IExtendStoreBase, storeKey: string, stateKey: string, options: any) { | ||
let store: IExtendStoreBase = this.getStore ? this.getStore(storeKey) as IExtendStoreBase : (this as any)[storeKey] as IExtendStoreBase; | ||
let disposable = store.observe((state) => { | ||
@@ -90,13 +96,13 @@ this.setState({ [stateKey]: state }); | ||
}, | ||
List: function (storeKey, stateKey, options) { | ||
List: function (this: IExtendStoreBase, storeKey: string, stateKey: string, options: any) { | ||
let count = options && options.size || 0; | ||
if (count > 0) { | ||
let store = this.getStore ? this.getStore(storeKey) : this[storeKey]; | ||
let store = this.getStore ? this.getStore(storeKey) as StoreList : (this as any)[storeKey] as StoreList; | ||
store.setSize(count); | ||
} | ||
}, | ||
Map: function (storeKey, stateKey, options) { | ||
Map: function (this: IExtendStoreBase, storeKey: string, stateKey: string, options: any) { | ||
let keys = options && options.keys; | ||
if (Array.isArray(keys)) { | ||
let store = this.getStore ? this.getStore(storeKey) : this[storeKey]; | ||
let store = this.getStore ? this.getStore(storeKey) as StoreMap : (this as any)[storeKey] as StoreMap; | ||
keys.forEach(key => store.add(key)); | ||
@@ -107,5 +113,5 @@ } | ||
function extendClass(StoreClass) { | ||
function extendClass(StoreClass: StoreBaseConstructor): IExtendStoreBaseConstructor { | ||
// return class ExtendStoreClass extends StoreClass {}; | ||
function ExtendStoreClass(...args) { | ||
function ExtendStoreClass(...args: any[]) { | ||
const obj = new StoreClass(...args); | ||
@@ -120,3 +126,3 @@ Object.setPrototypeOf(obj, new.target.prototype); | ||
Object.setPrototypeOf(ExtendStoreClass, StoreClass); | ||
return ExtendStoreClass; | ||
return ExtendStoreClass as any; | ||
} | ||
@@ -128,14 +134,29 @@ | ||
function filterOneStore(StoreClass, filterOptions?: FilterStoreOptions) { | ||
function parseInnerStores(innerStores: IStoresDeclarer): IStoresArrayDeclarer | undefined { | ||
if (!innerStores) { | ||
return undefined; | ||
} | ||
if (Array.isArray(innerStores)) { | ||
return innerStores; | ||
} | ||
let storeDeclarer: IStoresArrayDeclarer = []; | ||
for (let key in innerStores) { | ||
let value = innerStores[key]; | ||
storeDeclarer.push({ stateKey: key, declarer: value }); | ||
} | ||
return storeDeclarer; | ||
} | ||
export function filterOneStore(StoreClass: IExtendStoreBaseConstructor, filterOptions?: FilterStoreOptions): IStoreFilters | null { | ||
if (!StoreClass) return null; | ||
let innerStores = StoreClass.innerStores; | ||
if (!innerStores) return null; | ||
let innerStoreArray = parseInnerStores(StoreClass.innerStores); | ||
if (!innerStoreArray) return null; | ||
let filters = {}; | ||
let subStoreInfos = []; | ||
for (let key in innerStores) { | ||
let value = innerStores[key]; | ||
if (isFunction(value)) { | ||
let storeName = key + 'Store'; | ||
let Store = extendClass(value); | ||
let filters: IStoreFilters = {}; | ||
let subStoreInfos: ISubStoreInfo[] = []; | ||
for (let innerStore of innerStoreArray) { | ||
let { stateKey, declarer } = innerStore; | ||
if (isFunction(declarer)) { | ||
let storeName = stateKey + 'Store'; | ||
let Store = extendClass(declarer as StoreBaseConstructor); | ||
filters[storeName] = { | ||
@@ -145,28 +166,30 @@ type: 'Store', | ||
}; | ||
subStoreInfos.push(['Item', Store, storeName, key, filterOptions]); | ||
subStoreInfos.push(['Item', Store, storeName, stateKey, filterOptions]); | ||
} else { | ||
let { options, Store } = value; | ||
if (filterOptions) options = { ...options, ...filterOptions }; | ||
let storeName = options && options.storeKey || key + 'Store'; | ||
Store = extendClass(Store); | ||
let { options, Store } = declarer as StoreDeclarer | StoreListDeclarer | StoreMapDeclarer; | ||
let nowOptions: { [key: string]: any } | undefined = options; | ||
if (filterOptions) { | ||
nowOptions = { ...options, ...filterOptions }; | ||
} | ||
let storeName = nowOptions && nowOptions.storeKey || stateKey + 'Store'; | ||
let extendStore = extendClass(Store); | ||
if (StoreDeclarer.isStore(value)) { | ||
if (StoreDeclarer.isStore(declarer)) { | ||
filters[storeName] = { | ||
type: 'Store', | ||
filters: filterOneStore(Store, filterOptions), | ||
filters: filterOneStore(extendStore, filterOptions), | ||
}; | ||
subStoreInfos.push(['Item', Store, storeName, key, options]); | ||
} else if (StoreListDeclarer.isStoreList(value)) { | ||
subStoreInfos.push(['Item', extendStore, storeName, stateKey, nowOptions]); | ||
} else if (StoreListDeclarer.isStoreList(declarer)) { | ||
filters[storeName] = { | ||
type: 'StoreList', | ||
filters: filterOneStore(Store, filterOptions), | ||
filters: filterOneStore(extendStore, filterOptions), | ||
} | ||
subStoreInfos.push(['List', Store, storeName, key, options]); | ||
} else if (StoreMapDeclarer.isStoreMap(value)) { | ||
subStoreInfos.push(['List', extendStore, storeName, stateKey, nowOptions]); | ||
} else if (StoreMapDeclarer.isStoreMap(declarer)) { | ||
filters[storeName] = { | ||
type: 'StoreMap', | ||
filters: filterOneStore(Store, filterOptions), | ||
filters: filterOneStore(extendStore, filterOptions), | ||
}; | ||
if (options && options.directInsert) key = null; | ||
subStoreInfos.push(['Map', Store, storeName, key, options]); | ||
subStoreInfos.push(['Map', extendStore, storeName, nowOptions && nowOptions.directInsert ? null : stateKey, nowOptions]); | ||
} | ||
@@ -177,3 +200,3 @@ } | ||
subStoreInfos.forEach(([type, StoreClass, storeKey, stateKey, options]) => { | ||
storeBuilders[type].call(this, StoreClass, storeKey, stateKey, options); | ||
storeBuilders[type].call(this, StoreClass, storeKey, stateKey!, options); | ||
let store = this.getStore ? this.getStore(storeKey) : this[storeKey]; | ||
@@ -183,3 +206,3 @@ store.buildStores && store.buildStores(); | ||
}; | ||
StoreClass.prototype.initStores = function(parentStore) { | ||
StoreClass.prototype.initStores = function(parentStore: IExtendStoreBase) { | ||
subStoreInfos.forEach((info) => { | ||
@@ -200,3 +223,3 @@ let storeKey = info[2]; | ||
store.startObserve && store.startObserve(); | ||
storeObservers[type].call(this, storeKey, stateKey, options); | ||
storeObservers[type].call(this, storeKey, stateKey!, options); | ||
}); | ||
@@ -227,13 +250,13 @@ }; | ||
function filterStore(stores) { | ||
let storeFilters = {}; | ||
for (let key in stores) { | ||
let store = stores[key]; | ||
storeFilters[key] = { type: 'Store', filters: filterOneStore(store.constructor) }; | ||
} | ||
return storeFilters; | ||
}; | ||
// function filterStore(stores) { | ||
// let storeFilters = {}; | ||
// for (let key in stores) { | ||
// let store = stores[key]; | ||
// storeFilters[key] = { type: 'Store', filters: filterOneStore(store.constructor) }; | ||
// } | ||
// return storeFilters; | ||
// }; | ||
export { | ||
filterOneStore, | ||
}; | ||
// export { | ||
// filterOneStore, | ||
// }; |
@@ -1,4 +0,4 @@ | ||
import { addStateFilterForMap } from './stateFilterDecorator'; | ||
import stateFilterDecorator from './stateFilterMapDecorator'; | ||
import StoreMap from './StoreMap'; | ||
export default addStateFilterForMap(StoreMap); | ||
export default stateFilterDecorator(StoreMap); |
@@ -0,1 +1,3 @@ | ||
import IStoreFilters from "../IStoreFilters"; | ||
/* | ||
@@ -27,3 +29,3 @@ 1. filterWindowStore({ | ||
function filterWindowStore(storeFilters, winStoreKey, winId) { | ||
function filterWindowStore(storeFilters: IStoreFilters, winStoreKey: string, winId: string) { | ||
let winFilters = storeFilters[winStoreKey].filters; | ||
@@ -33,6 +35,6 @@ if (!winFilters) return storeFilters; | ||
if (!winFilters) return omit(storeFilters, [winStoreKey]); | ||
let winOnlyShape = {}; | ||
let winOnlyShape: { [storeKey: string]: any } = {}; | ||
let path = [winStoreKey, { type: 'Map', name: 'winPackMapStore', index: winId }]; | ||
Object.keys(winFilters).forEach(storeKey => { | ||
winOnlyShape[storeKey] = { ...winFilters[storeKey], path }; | ||
winOnlyShape[storeKey] = { ...winFilters![storeKey], path }; | ||
}); | ||
@@ -42,3 +44,3 @@ return { ...omit(storeFilters, [winStoreKey]), ...winOnlyShape }; | ||
function filterWindowState(allState, winStateKey, winId) { | ||
function filterWindowState(allState: any, winStateKey: string, winId: string) { | ||
if (!allState[winStateKey]) return allState; | ||
@@ -52,3 +54,3 @@ let { winPackMap } = allState[winStateKey]; | ||
function filterWindowDelta(updated, deleted, winStateKey, winId) { | ||
function filterWindowDelta(updated: any, deleted: any, winStateKey: string, winId: string) { | ||
return [ | ||
@@ -55,0 +57,0 @@ filterWindowState(updated, winStateKey, winId), |
@@ -0,0 +0,0 @@ export type Logger = (...args: any[]) => void; |
@@ -16,7 +16,7 @@ /* | ||
const isShallow = val => Array.isArray(val) || !isObject(val) || List.isList(val); | ||
const isShallow = (val: any) => Array.isArray(val) || !isObject(val) || List.isList(val); | ||
const isDiffType = (val1, val2) => (Map.isMap(val1) && !Map.isMap(val2)) || (!Map.isMap(val1) && Map.isMap(val2)); | ||
const isDiffType = (val1: any, val2: any) => (Map.isMap(val1) && !Map.isMap(val2)) || (!Map.isMap(val1) && Map.isMap(val2)); | ||
function checkUpdateVal(key, oldVal, currVal, updated, deleted) { | ||
function checkUpdateVal(key: string, oldVal: any, currVal: any, updated: { [key: string]: any }, deleted: { [key: string]: any }) { | ||
if (currVal === oldVal) return; | ||
@@ -35,15 +35,15 @@ | ||
function objectDifference(old, curr) { | ||
const updated = {}; | ||
const deleted = {}; | ||
function objectDifference(old: any, curr: any) { | ||
const updated: { [key: string]: any } = {}; | ||
const deleted: { [key: string]: any } = {}; | ||
if (Map.isMap(old) && Map.isMap(curr)) { | ||
curr.forEach((val, key) => checkUpdateVal(JSON.stringify(key), old.get(key), val, updated, deleted)); | ||
curr.forEach((val: any, key: string) => checkUpdateVal(JSON.stringify(key), old.get(key), val, updated, deleted)); | ||
old.forEach((val, key) => { | ||
old.forEach((val: any, key: string) => { | ||
if (curr.get(key) === undefined) deleted[JSON.stringify(key)] = true; | ||
}); | ||
} else { | ||
keys(curr).forEach(key => checkUpdateVal(key, old[key], curr[key], updated, deleted)); | ||
keys(old).forEach(key => { | ||
keys(curr).forEach((key: string) => checkUpdateVal(key, old[key], curr[key], updated, deleted)); | ||
keys(old).forEach((key: string) => { | ||
if (curr[key] === undefined) deleted[key] = true; | ||
@@ -50,0 +50,0 @@ }); |
@@ -6,5 +6,5 @@ import { List, Map } from 'immutable'; | ||
const isShallow = (val) => Array.isArray(val) || !isObject(val) || List.isList(val); | ||
const isShallow = (val: any) => Array.isArray(val) || !isObject(val) || List.isList(val); | ||
export default function objectMerge(origin, updated, deleted) { | ||
export default function objectMerge(origin: any, updated: any, deleted: any) { | ||
/** | ||
@@ -19,6 +19,6 @@ * origin is shallow | ||
if (Map.isMap(origin)) { | ||
let merged, deleteKeys = []; | ||
let merged, deleteKeys: string[] = []; | ||
// deleted = isObject(deleted) ? deleted : {}; | ||
merged = origin.withMutations(map => { | ||
keys(deleted).forEach(key => { | ||
merged = origin.withMutations((map: Map<any, any>) => { | ||
keys(deleted).forEach((key: string) => { | ||
if (deleted[key] === true) { | ||
@@ -30,3 +30,3 @@ map.delete(JSON.parse(key)) | ||
}); | ||
union(keys(updated), deleteKeys).forEach(key => { | ||
union(keys(updated), deleteKeys).forEach((key: string) => { | ||
let originKey = JSON.parse(key); | ||
@@ -38,14 +38,15 @@ map.set(originKey, objectMerge(origin.get(originKey), updated && updated[key], deleted && deleted[key])); | ||
} else { | ||
let merged, deleteKeys; | ||
let merged: { [key: string]: any } | undefined; | ||
let deleteKeys; | ||
if (isObject(deleted)) { | ||
merged = {}; | ||
keys(origin).forEach(key => { | ||
if (deleted[key] !== true) merged[key] = origin[key]; | ||
keys(origin).forEach((key: string) => { | ||
if (deleted[key] !== true) merged![key] = origin[key]; | ||
}); | ||
deleteKeys = keys(deleted).filter(d => deleted[d] !== true) | ||
deleteKeys = keys(deleted).filter((d: string) => deleted[d] !== true) | ||
} else { | ||
merged = { ...origin }; | ||
} | ||
union(keys(updated), deleteKeys).forEach(key => { | ||
merged[key] = objectMerge(origin[key], updated && updated[key], deleted && deleted[key]) | ||
union(keys(updated), deleteKeys).forEach((key: string) => { | ||
merged![key] = objectMerge(origin[key], updated && updated[key], deleted && deleted[key]) | ||
}); | ||
@@ -52,0 +53,0 @@ return merged; |
@@ -1,18 +0,19 @@ | ||
import { Emitter } from 'event-kit'; | ||
const omit = require('lodash/omit'); | ||
import { Emitter, Disposable } from 'event-kit'; | ||
import { StoreBaseConstructor } from '../StoreBase'; | ||
import IStoresDeclarer from '../IStoresDeclarer'; | ||
import IExtendStoreBase, { IExtendStoreBaseConstructor } from '../IExtendStoreBase'; | ||
import { StoreMapConstructor } from './StoreMap'; | ||
import { omit } from './objUtils'; | ||
export const addStateFilter = (StoreClass) => { | ||
return class MainStoreBase extends StoreClass { | ||
_stateListeners = {}; | ||
_stateFilters = {}; | ||
export default function(StoreClass: StoreBaseConstructor) { | ||
const ExtendStoreClass = StoreClass as any as IExtendStoreBaseConstructor; | ||
return class MainStoreBase extends ExtendStoreClass { | ||
_stateListeners: { [key: string]: any } = {}; | ||
_stateFilters: { [key: string]: any } = {}; | ||
_stateFiltersInit = false; // Check if or not the stateFilters has init | ||
appStores: any; | ||
static innerStores; | ||
constructor(...args) { | ||
super(...args); | ||
} | ||
getDefaultFilter() { | ||
static innerStores: IStoresDeclarer; | ||
getDefaultFilter(): { [key: string]: any } { | ||
if (this.options && this.options.defaultFilter) return { '*': true }; | ||
@@ -22,3 +23,3 @@ return { '*': false }; | ||
_initForClientId = (clientId) => { | ||
_initForClientId = (clientId: string) => { | ||
let clientFilters = this.getDefaultFilter(); | ||
@@ -28,3 +29,3 @@ this.getSubStoreInfos && this.getSubStoreInfos().forEach((storeInfo) => { | ||
let stateKey = storeInfo[3]; | ||
let subStore = this.getStore ? this.getStore(storeName) : this[storeName]; | ||
let subStore = this.getStore ? this.getStore(storeName) : (this as any)[storeName]; | ||
let storeFilter = subStore._stateFilters && subStore._stateFilters[clientId] || { '*': true }; | ||
@@ -42,3 +43,3 @@ if (stateKey) { | ||
// Init the state filters for the window with clientId | ||
let winManagerStore = (this.appStores || this.stores).winManagerStore; | ||
let winManagerStore = (this.appStores || this.getSubStores).winManagerStore; | ||
winManagerStore.getClienIds().forEach(this._initForClientId); | ||
@@ -54,4 +55,4 @@ this._stateFiltersInit = true; | ||
let stateKey = storeInfo[3]; | ||
let subStore = this.getStore ? this.getStore(storeName) : this[storeName]; | ||
subStore.emitter.on('did-filter-update', ({ clientId, filters }) => { | ||
let subStore = this.getStore ? this.getStore(storeName) : (this as any)[storeName]; | ||
subStore.emitter.on('did-filter-update', ({ clientId, filters }: { clientId: string, filters: any }) => { | ||
if (stateKey) { | ||
@@ -71,7 +72,7 @@ this._setFilter(clientId, { [stateKey]: filters }); | ||
_handleAddWin(clientId) { | ||
_handleAddWin(clientId: string) { | ||
if (this._stateFiltersInit) { | ||
this.getSubStoreInfos && this.getSubStoreInfos().forEach((storeInfo) => { | ||
let storeName = storeInfo[2]; | ||
let subStore = this.getStore ? this.getStore(storeName) : this[storeName]; | ||
let subStore = this.getStore ? this.getStore(storeName) : (this as any)[storeName]; | ||
subStore._handleAddWin && subStore._handleAddWin(clientId); | ||
@@ -83,3 +84,3 @@ }); | ||
_handleRemoveWin(clientId) { | ||
_handleRemoveWin(clientId: string) { | ||
if (this._stateFiltersInit) { | ||
@@ -90,3 +91,3 @@ this._stateListeners[clientId] = 0; | ||
let storeName = storeInfo[2]; | ||
let subStore = this.getStore ? this.getStore(storeName) : this[storeName]; | ||
let subStore = this.getStore ? this.getStore(storeName) : (this as any)[storeName]; | ||
subStore._handleRemoveWin && subStore._handleRemoveWin(clientId); | ||
@@ -97,3 +98,3 @@ }); | ||
_setFilter(clientId, newFilter) { | ||
_setFilter(clientId: string, newFilter: any) { | ||
const filterRunner = () => { | ||
@@ -112,3 +113,3 @@ let oldFilters = this._stateFilters[clientId] || this.getDefaultFilter(); | ||
listen = function(clientId: string) { | ||
listen = function(this: MainStoreBase, clientId: string) { | ||
if (!clientId) return console.error('The clientId is not specify'); | ||
@@ -123,3 +124,3 @@ let _stateListeners = this._stateListeners; | ||
unlisten = function(clientId: string) { | ||
unlisten = function(this: MainStoreBase, clientId: string) { | ||
if (!clientId) return console.error('The clientId is not specify'); | ||
@@ -134,183 +135,1 @@ let _stateListeners = this._stateListeners; | ||
} | ||
type KeyType = string | number | string[] | number[]; | ||
export interface IFilterStoreMap { | ||
listenForKeys(clientId: string, key: KeyType): void; | ||
unlistenForKeys(clientId: string, key: KeyType): void; | ||
add(key: string, prevInit?: Function): any; | ||
delete(key: string): void; | ||
clear(): void; | ||
dispose(): void; | ||
} | ||
export function addStateFilterForMap(StoreClass) { | ||
return class MainStoreBase extends StoreClass implements IFilterStoreMap { | ||
_stateListeners = {}; | ||
_stateFilters = {}; | ||
_filterDisposables = {}; | ||
_stateFiltersInit = false; // Check if or not the stateFilters has init | ||
constructor(...args) { | ||
super(...args); | ||
} | ||
getDefaultFilter() { | ||
// defaultFilter=true 表示默认的*为true,并且将observe所有key | ||
if (this.options && this.options.defaultFilter) return { '*': true }; | ||
return { '*': false }; | ||
} | ||
_initForClientId = (clientId) => { | ||
let defaultFilter = this.options && this.options.defaultFilter; | ||
let clientFilters = this.getDefaultFilter(); | ||
if (defaultFilter) { | ||
let entries = this.storeMap.entries(); | ||
for (let [key, store] of entries) { | ||
clientFilters[key] = store._stateFilters && store._stateFilters[clientId] || { '*': true }; | ||
} | ||
} | ||
this._stateFilters[clientId] = clientFilters; | ||
}; | ||
_initStateFilters() { | ||
let defaultFilter = this.options && this.options.defaultFilter; | ||
let winManagerStore = this.appStores.winManagerStore; | ||
winManagerStore.getClienIds().forEach(this._initForClientId); | ||
this._stateFiltersInit = true; | ||
// winManagerStore.onDidAddWin(initForClientId); | ||
// winManagerStore.onDidRemoveWin((clientId) => this._stateFilters[clientId] = null); | ||
if (defaultFilter) { | ||
for (let [key, store] of this.storeMap.entries()) { | ||
// clientFilters[key] = defaultFilter ? store._stateFilters && store._stateFilters[clientId] || { '*': true } : { '*': false }; | ||
this._filterDisposables[key] = store.emitter.on('did-filter-update', ({ clientId, filters }) => { | ||
this._setFilter(clientId, { [key]: filters }) | ||
}); | ||
} | ||
} | ||
} | ||
_initWrap() { | ||
this._initStateFilters(); | ||
super._initWrap(); | ||
} | ||
_setFilter(clientId, newFilter) { | ||
let oldFilters = this._stateFilters[clientId] || this.getDefaultFilter(); | ||
let nextFilters = { ...oldFilters, ...newFilter }; | ||
this._stateFilters[clientId] = nextFilters; | ||
this.emitter.emit('did-filter-update', { clientId, filters: nextFilters}); | ||
} | ||
_handleAddWin(clientId) { | ||
if (this._stateFiltersInit) { | ||
let entries = this.storeMap.entries(); | ||
for (let [key, store] of entries) { | ||
store._handleAddWin && store._handleAddWin(clientId); | ||
} | ||
this._initForClientId(clientId); | ||
} | ||
} | ||
_handleRemoveWin(clientId) { | ||
if (this._stateFiltersInit) { | ||
this._stateFilters[clientId] = null; | ||
let entries = this.storeMap.entries(); | ||
for (let [key, store] of entries) { | ||
this._stateListeners[clientId + key] = 0; | ||
store._handleRemoveWin && store._handleRemoveWin(clientId); | ||
} | ||
} | ||
} | ||
listenForKeys = function(clientId: string, key: KeyType) { | ||
if (!clientId) return console.error('The clientId is not specify'); | ||
let keys = Array.isArray(key) ? key : [key]; | ||
let _stateListeners = this._stateListeners; | ||
keys.forEach(key => { | ||
let saveKey = clientId + key; | ||
if (_stateListeners[saveKey] == null) _stateListeners[saveKey] = 0; | ||
_stateListeners[saveKey] += 1; | ||
if (_stateListeners[saveKey] === 1) { | ||
let store = this.storeMap.get(key); | ||
let storeFilter = store._stateFilters && store._stateFilters[clientId] || { '*': true }; | ||
this._setFilter(clientId, { [key]: storeFilter }); | ||
if (this._filterDisposables[saveKey]) { | ||
console.error(`The ${key} for ${clientId} has listened, This May be Bugs`); | ||
} | ||
this._filterDisposables[saveKey] = store.emitter.on('did-filter-update', ({ clientId: nowId, filters }) => { | ||
if (nowId === clientId) this._setFilter(clientId, { [key]: filters }); | ||
}); | ||
} | ||
}); | ||
} | ||
unlistenForKeys = function(clientId: string, key: KeyType) { | ||
if (!clientId) return console.error('The clientId is not specify'); | ||
let keys = Array.isArray(key) ? key : [key]; | ||
let _stateListeners = this._stateListeners; | ||
keys.forEach(key => { | ||
let saveKey = clientId + key; | ||
_stateListeners[saveKey] -= 1; | ||
if (_stateListeners[saveKey] === 0) { | ||
this._setFilter(clientId, { [key]: false }); | ||
this._filterDisposables[saveKey].dispose(); | ||
this._filterDisposables[saveKey] = null; | ||
} | ||
}); | ||
}; | ||
add(key, prevInit) { | ||
let newStore = super.add(key, prevInit); | ||
let defaultFilter = this.options && this.options.defaultFilter; | ||
if (defaultFilter) { | ||
Object.keys(this._stateFilters).forEach(clientId => { | ||
let filters = newStore._stateFilters && newStore._stateFilters[clientId] || { '*': true }; | ||
this._setFilter(clientId, { [key]: filters }); | ||
}); | ||
if (this._filterDisposables[key]) console.error(`The key ${key} should NOT add twice`); | ||
this._filterDisposables[key] = newStore.emitter.on('did-filter-update', ({ clientId, filters }) => { | ||
this._setFilter(clientId, { [key]: filters }); | ||
}); | ||
} | ||
return newStore; | ||
} | ||
deleteFilter(key) { | ||
let store = this.storeMap.get(key); | ||
Object.keys(this._stateFilters).forEach(clientId => { | ||
this._setFilter(clientId, { [key]: null }); | ||
}); | ||
if (this._filterDisposables[key]) { | ||
this._filterDisposables[key].dispose(); | ||
this._filterDisposables[key] = null; | ||
} | ||
} | ||
delete(key) { | ||
this.deleteFilter(key); | ||
super.delete(key); | ||
} | ||
clear() { | ||
let keys = this.storeMap.keys(); | ||
for (let key of keys) { | ||
this.deleteFilter(key); | ||
} | ||
super.clear(); | ||
} | ||
dispose() { | ||
super.dispose(); | ||
for (let key in this._filterDisposables) { | ||
let disposable = this._filterDisposables[key]; | ||
disposable && disposable.dispose(); | ||
} | ||
} | ||
} | ||
} |
@@ -1,9 +0,11 @@ | ||
export function beforeInit(store, parentStore) { | ||
import IExtendStoreBase from "../IExtendStoreBase"; | ||
export function beforeInit(store: IExtendStoreBase, parentStore: IExtendStoreBase) { | ||
store.parentStore = parentStore; | ||
if (parentStore.clientId) { | ||
store.clientId = parentStore.clientId; | ||
if ((parentStore as any).clientId) { | ||
(store as any).clientId = (parentStore as any).clientId; | ||
} | ||
} | ||
export const initStore = function(store, parentStore) { | ||
export const initStore = function(store: IExtendStoreBase, parentStore: IExtendStoreBase) { | ||
store.buildStores && store.buildStores(); | ||
@@ -19,5 +21,5 @@ | ||
export const disposeStore = function(store) { | ||
export const disposeStore = function(store: IExtendStoreBase) { | ||
store.disposeStores && store.disposeStores(); | ||
store.dispose(); | ||
} |
import { initStore, disposeStore } from './storeBuilder'; | ||
import { Emitter } from 'event-kit'; | ||
import { Emitter, Disposable } from 'event-kit'; | ||
import IExtendStoreBase from '../IExtendStoreBase'; | ||
// import { addStateFilter } from './stateFilterDecorator'; | ||
type StoreBuilder = () => IExtendStoreBase; | ||
type StoreObserver = (store: IExtendStoreBase, i: number) => Disposable; | ||
export default class StoreList { | ||
length: number = 0; | ||
storeArray = []; | ||
disposables = []; | ||
storeArray: IExtendStoreBase[] = []; | ||
disposables: Disposable[] = []; | ||
options: any; | ||
builder: any; | ||
observer: any; | ||
builder: StoreBuilder; | ||
observer: StoreObserver; | ||
parentStore: any = null; | ||
@@ -16,3 +20,3 @@ appStores: any; | ||
constructor(size, builder, observer, options) { | ||
constructor(size: number, builder: StoreBuilder, observer: StoreObserver, options: any) { | ||
this.builder = builder; | ||
@@ -28,3 +32,3 @@ this.observer = observer; | ||
setSize(count) { | ||
setSize(count: number) { | ||
if (this.length === count) return; | ||
@@ -34,3 +38,3 @@ if (this.length < count) { | ||
let newStore = this.builder(); | ||
newStore.listStoreKey = i; | ||
(newStore as any).listStoreKey = i; | ||
@@ -57,8 +61,15 @@ // if (this._isInit) initStore(newStore); | ||
forEach(callback) { return this.storeArray.forEach(callback); } | ||
map(callback) { return this.storeArray.map(callback); } | ||
filter(callback) { return this.storeArray.filter(callback); } | ||
get(index) { return this.storeArray[index]; } | ||
slice(begin, end) { return this.storeArray.slice(begin, end); } | ||
indexOf(item) { return this.storeArray.indexOf(item); } | ||
forEach(callback: (value: IExtendStoreBase, index: number, array: IExtendStoreBase[]) => void) { | ||
return this.storeArray.forEach(callback); | ||
} | ||
map(callback: (value: IExtendStoreBase, index: number, array: IExtendStoreBase[]) => any) { | ||
return this.storeArray.map(callback); | ||
} | ||
filter(callback: (value: IExtendStoreBase, index: number, array: IExtendStoreBase[]) => boolean) { | ||
return this.storeArray.filter(callback); | ||
} | ||
get(index: number) { return this.storeArray[index]; } | ||
slice(begin: number, end: number) { return this.storeArray.slice(begin, end); } | ||
indexOf(item: IExtendStoreBase) { return this.storeArray.indexOf(item); } | ||
} |
import { initStore, disposeStore } from './storeBuilder'; | ||
import { Emitter } from 'event-kit'; | ||
import IExtendStoreBase from '../IExtendStoreBase'; | ||
type StoreBuilder = () => IExtendStoreBase | undefined; | ||
type StoreMapObserver = (store: IExtendStoreBase, index: string) => void; | ||
export interface StoreMapConstructor { | ||
new (keys: string[] | null, builder: StoreBuilder, observer: StoreMapObserver, options: any): StoreMap; | ||
} | ||
export default class StoreMap { | ||
storeMap = new Map(); | ||
storeMap: Map<string, any> = new Map(); | ||
disposables = new Map(); | ||
options: any; | ||
builder: any; | ||
builder: StoreBuilder; | ||
observer: any; | ||
@@ -14,7 +22,7 @@ parentStore: any = null; | ||
constructor(keys, builder, observer, options) { | ||
constructor(keys: string[] | null, builder: StoreBuilder, observer: StoreMapObserver, options: any) { | ||
this.builder = builder; | ||
this.observer = observer; | ||
this.options = options; | ||
if (Array.isArray(keys)) keys.forEach(key => this.add(key, null)); | ||
if (Array.isArray(keys)) keys.forEach(key => this.add(key)); | ||
} | ||
@@ -26,16 +34,18 @@ | ||
add(key, prevInit) { | ||
add(key: string, prevInit?: (store: IExtendStoreBase) => void): IExtendStoreBase | undefined { | ||
if (this.storeMap.has(key)) return; | ||
let newStore = this.builder(); | ||
newStore.mapStoreKey = key; | ||
prevInit && prevInit(newStore); | ||
// if (this._isInit) initStore(newStore); | ||
initStore(newStore, this.parentStore); | ||
this.storeMap.set(key, newStore); | ||
this.disposables.set(key, this.observer(newStore, key)); | ||
return newStore; | ||
if (newStore) { | ||
(newStore as any).mapStoreKey = key; | ||
prevInit && prevInit(newStore); | ||
// if (this._isInit) initStore(newStore); | ||
initStore(newStore, this.parentStore); | ||
this.storeMap.set(key, newStore); | ||
this.disposables.set(key, this.observer(newStore, key)); | ||
return newStore; | ||
} | ||
} | ||
delete(key) { | ||
delete(key: string) { | ||
let store = this.storeMap.get(key); | ||
@@ -66,8 +76,15 @@ store && disposeStore(store); | ||
forEach(callback) { return this.storeMap.forEach(callback); } | ||
get(key) { return this.storeMap.get(key); } | ||
has(key) { return this.storeMap.has(key); } | ||
forEach(callback: (value: any, key: string, map: Map<string, any>) => void) { | ||
return this.storeMap.forEach(callback); | ||
} | ||
get(key: string) { return this.storeMap.get(key); } | ||
has(key: string) { return this.storeMap.has(key); } | ||
keys() { return this.storeMap.keys(); } | ||
values() { return this.storeMap.values(); } | ||
entries() { return this.storeMap.entries(); } | ||
} |
const isEmpty = require('lodash/isEmpty'); | ||
interface ListStorePathUnit { | ||
index: string; | ||
index?: string; | ||
name: string; | ||
@@ -32,2 +32,4 @@ type: 'Map' | 'List'; | ||
type Stores = { [storeKey: string]: any }; | ||
export default class StoreProxyHandler { | ||
@@ -39,14 +41,14 @@ storeProxyCache = new Map(); | ||
*/ | ||
genProxy(storePath, forwardStore, forwarder) { | ||
genProxy(storePath: StorePathUnit[], forwardStore: Stores | null, forwarder: Function) { | ||
return new Proxy(forwarder, { | ||
get: function(target, propName) { | ||
if (!propName) return; | ||
if (forwardStore && forwardStore[propName]) return forwardStore[propName]; | ||
if (forwardStore && forwardStore[propName as string]) return forwardStore[propName as string]; | ||
if (ListenForwardKeys.indexOf(propName as string) !== -1) { | ||
return function(...args) { | ||
args.unshift(window['clientId']); | ||
return function(...args: any[]) { | ||
args.unshift((window as any)['clientId']); | ||
return forwarder({ store: storePath, method: propName, args }); | ||
}; | ||
} | ||
return function(...args) { | ||
return function(...args: any[]) { | ||
return forwarder({ store: storePath, method: propName, args }); | ||
@@ -58,3 +60,3 @@ }; | ||
genIndexProxy(storePath, childFilters, forwarder) { | ||
genIndexProxy(storePath: StorePathUnit[], childFilters: { [key: string]: StoreShape }, forwarder: Function) { | ||
let storePathKey = storePath.slice(0, storePath.length - 1).toString(); | ||
@@ -64,3 +66,3 @@ let newProxy = new Proxy(forwarder, { | ||
if (!propName) return; | ||
const retIndexFunc = (index) => { | ||
const retIndexFunc = (index: string) => { | ||
let cacheStore = this.storeProxyCache.get(storePathKey + index); | ||
@@ -71,3 +73,3 @@ if (cacheStore) return cacheStore; | ||
...storePath.slice(0, -1), | ||
{ ...storePath[storePath.length - 1], index } | ||
{ ...(storePath[storePath.length - 1] as ListStorePathUnit), index } | ||
]; | ||
@@ -81,6 +83,6 @@ let childStores = this.proxyStore(mapStorepath, childFilters, forwarder); | ||
let oneStorePath = [ | ||
...storePath.slice(0, -1), storePath[storePath.length - 1].name | ||
...storePath.slice(0, -1), (storePath[storePath.length - 1] as ListStorePathUnit).name | ||
]; | ||
return function(...args) { | ||
args.unshift(window['clientId']); | ||
return function(...args: any[]) { | ||
args.unshift((window as any)['clientId']); | ||
return forwarder({ store: oneStorePath, method: propName, args }); | ||
@@ -92,3 +94,3 @@ }; | ||
} | ||
return retIndexFunc(propName); | ||
return retIndexFunc(propName as string); | ||
} | ||
@@ -99,5 +101,5 @@ }); | ||
proxyStore(parentStore: StorePathUnit[], storeFilters: StoreFilters, forwarder: Function) { | ||
proxyStore(parentStore: StorePathUnit[], storeFilters: StoreFilters, forwarder: Function): Stores | null { | ||
if (isEmpty(storeFilters)) return null; | ||
let stores = {}; | ||
let stores: { [storeKey: string]: any } = {}; | ||
for (let name in storeFilters) { | ||
@@ -107,3 +109,3 @@ let storeInfo = storeFilters[name]; | ||
let { type, filters, path = parentStore } = storeInfo; | ||
let names; | ||
let names: StorePathUnit[] = []; | ||
if (type === 'Store') { | ||
@@ -117,6 +119,6 @@ names = [...path, name]; | ||
if (type === 'Store') { | ||
let childStores = this.proxyStore(names, filters, forwarder); | ||
let childStores = this.proxyStore(names, filters!, forwarder); | ||
stores[name] = this.genProxy(names, childStores, forwarder); | ||
} else { | ||
stores[name] = this.genIndexProxy(names, filters, forwarder); | ||
stores[name] = this.genIndexProxy(names, filters!, forwarder); | ||
} | ||
@@ -123,0 +125,0 @@ } |
import StoreBase from './MainStoreBase'; | ||
export default class WinSpecStoreBase extends StoreBase { | ||
clientId: string; | ||
clientId: string = 'mainClient'; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
388859
184
9413
24
+ Addedevent-kit@^2.5.3
+ Addedajv@6.12.6(transitive)
+ Addedconf@5.0.0(transitive)
+ Addeddot-prop@5.3.0(transitive)
+ Addedelectron-store@4.0.0(transitive)
+ Addedenv-paths@2.2.1(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedfind-up@3.0.0(transitive)
+ Addedis-obj@2.0.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-schema-typed@7.0.3(transitive)
+ Addedlocate-path@3.0.0(transitive)
+ Addedmake-dir@3.1.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@3.0.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpkg-up@3.1.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedtype-fest@0.5.2(transitive)
+ Addedtypedarray-to-buffer@3.1.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addedwrite-file-atomic@3.0.3(transitive)
- Removedevent-flux@^0.10.3
- Removedlodash@^4.13.1
- Removedconf@2.2.0(transitive)
- Removeddot-prop@4.2.1(transitive)
- Removedelectron-store@2.0.0(transitive)
- Removedenv-paths@1.0.0(transitive)
- Removedevent-flux@0.10.3(transitive)
- Removedfind-up@2.1.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedis-obj@1.0.1(transitive)
- Removedlocate-path@2.0.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedmake-dir@1.3.0(transitive)
- Removedp-limit@1.3.0(transitive)
- Removedp-locate@2.0.0(transitive)
- Removedp-try@1.0.0(transitive)
- Removedpify@3.0.0(transitive)
- Removedpkg-up@2.0.0(transitive)
- Removedwrite-file-atomic@2.4.3(transitive)
Updatedelectron-store@^4.0.0
Updatedjson-immutable-bn@^0.4.4