From ab61fd6308a4a80dd4751e58e51ea04ae1680e62 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 16 Jul 2020 15:31:06 +0300 Subject: [PATCH] auto-update clusters state in renderer stores Signed-off-by: Roman --- src/common/base-store.ts | 10 +++++++++- src/common/cluster-store.ts | 13 ++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/common/base-store.ts b/src/common/base-store.ts index 8e8c2935ee..77969fbac8 100644 --- a/src/common/base-store.ts +++ b/src/common/base-store.ts @@ -2,7 +2,7 @@ import path from "path" import Config from "conf" import { Options as ConfOptions } from "conf/dist/source/types" import { app, ipcMain, ipcRenderer, remote } from "electron" -import { action, observable, reaction, toJS, when } from "mobx"; +import { action, observable, reaction, runInAction, toJS, when } from "mobx"; import Singleton from "./utils/singleton"; import { getAppVersion } from "./utils/app-version"; import logger from "../main/logger"; @@ -101,6 +101,14 @@ export class BaseStore extends Singleton { this.syncDisposers.length = 0; } + protected applyWithoutSync(callback: () => void) { + this.disableSync(); + runInAction(callback); + if (this.params.syncEnabled) { + this.enableSync(); + } + } + protected onSync(model: T) { if (!isEqual(this.toJSON(), model)) { this.fromStore(model); diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index a1ea9fc728..a866d44d87 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -1,9 +1,11 @@ +import { ipcRenderer } from "electron"; import type { WorkspaceId } from "./workspace-store"; import { action, computed, observable, toJS } from "mobx"; import { v4 as uuid } from "uuid" import { BaseStore } from "./base-store"; -import { Cluster } from "../main/cluster"; +import { Cluster, ClusterState } from "../main/cluster"; import migrations from "../migrations/cluster-store" +import logger from "../main/logger"; export interface ClusterStoreModel { activeCluster?: ClusterId; // last opened cluster @@ -46,6 +48,15 @@ export class ClusterStore extends BaseStore { accessPropertiesByDotNotation: false, // To make dots safe in cluster context names migrations: migrations, }); + if (ipcRenderer) { + ipcRenderer.on("cluster:state", (event, clusterState: ClusterState) => { + this.applyWithoutSync(() => { + logger.info(`[CLUSTER-STORE]: received cluster(${clusterState.id}) update`, clusterState); + const cluster = this.getById(clusterState.id); + if (cluster) cluster.updateModel(clusterState) + }) + }) + } } @observable activeClusterId: ClusterId;