1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix: sync in sub-frames for common-stores are broken (#724)

* fix: sync in sub-frames for common-stores are broken

Signed-off-by: Roman <ixrock@gmail.com>

* clean up

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-08-20 20:33:06 +03:00 committed by GitHub
parent 86e1e23ccd
commit b6a6dadfce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import { action, observable, reaction, runInAction, toJS, when } from "mobx";
import Singleton from "./utils/singleton"; import Singleton from "./utils/singleton";
import { getAppVersion } from "./utils/app-version"; import { getAppVersion } from "./utils/app-version";
import logger from "../main/logger"; import logger from "../main/logger";
import { broadcastIpc } from "./ipc"; import { broadcastIpc, IpcBroadcastParams } from "./ipc";
import isEqual from "lodash/isEqual"; import isEqual from "lodash/isEqual";
export interface BaseStoreParams<T = any> extends ConfOptions<T> { export interface BaseStoreParams<T = any> extends ConfOptions<T> {
@ -63,7 +63,7 @@ export class BaseStore<T = any> extends Singleton {
this.isLoaded = true; this.isLoaded = true;
} }
protected async save(model: T) { protected async saveToFile(model: T) {
logger.info(`[STORE]: SAVING ${this.name}`); logger.info(`[STORE]: SAVING ${this.name}`);
// todo: update when fixed https://github.com/sindresorhus/conf/issues/114 // todo: update when fixed https://github.com/sindresorhus/conf/issues/114
Object.entries(model).forEach(([key, value]) => { Object.entries(model).forEach(([key, value]) => {
@ -115,8 +115,8 @@ export class BaseStore<T = any> extends Singleton {
protected async onModelChange(model: T) { protected async onModelChange(model: T) {
if (ipcMain) { if (ipcMain) {
this.save(model); // save config file this.saveToFile(model); // save config file
broadcastIpc({ channel: this.syncChannel, args: [model] }); // broadcast to renderer views this.syncToWebViews(model); // send update to renderer views
} }
// send "update-request" to main-process // send "update-request" to main-process
if (ipcRenderer) { if (ipcRenderer) {
@ -124,6 +124,30 @@ export class BaseStore<T = any> extends Singleton {
} }
} }
protected async syncToWebViews(model: T) {
const msg: IpcBroadcastParams = {
channel: this.syncChannel,
args: [model],
}
broadcastIpc(msg); // send to all windows (BrowserWindow, webContents)
const frames = await this.getSubFrames();
frames.forEach(frameId => {
broadcastIpc({ frameId, ...msg }); // send to all sub-frames (e.g. cluster-view managed in iframe)
});
}
// todo: refactor?
protected async getSubFrames(): Promise<number[]> {
const subFrames: number[] = [];
const { clusterStore } = await import("./cluster-store");
clusterStore.clustersList.forEach(cluster => {
if (cluster.frameId) {
subFrames.push(cluster.frameId)
}
});
return subFrames;
}
@action @action
protected fromStore(data: T) { protected fromStore(data: T) {
this.data = data; this.data = data;

View File

@ -3,7 +3,7 @@ import { ClusterId, clusterStore } from "./cluster-store";
import { tracker } from "./tracker"; import { tracker } from "./tracker";
export const clusterIpc = { export const clusterIpc = {
init: createIpcChannel({ initView: createIpcChannel({
channel: "cluster:init", channel: "cluster:init",
handle: async (clusterId: ClusterId, frameId: number) => { handle: async (clusterId: ClusterId, frameId: number) => {
const cluster = clusterStore.getById(clusterId); const cluster = clusterStore.getById(clusterId);

View File

@ -38,10 +38,11 @@ import { webFrame } from "electron";
@observer @observer
export class App extends React.Component { export class App extends React.Component {
static async init() { static async init() {
const frameId = webFrame.routingId;
const clusterId = getHostedClusterId(); const clusterId = getHostedClusterId();
logger.info(`[APP]: Init dashboard, clusterId=${clusterId}`) logger.info(`[APP]: Init dashboard, clusterId=${clusterId}, frameId=${frameId}`)
await Terminal.preloadFonts() await Terminal.preloadFonts()
await clusterIpc.init.invokeFromRenderer(clusterId, webFrame.routingId); await clusterIpc.initView.invokeFromRenderer(clusterId, frameId);
await getHostedCluster().whenInitialized; await getHostedCluster().whenInitialized;
} }