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

catalog-pusher clean up, replaced .observe_() to external observe() helper from "mobx"

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-05-16 15:36:02 +03:00
parent 3405c49613
commit 73ebf0a457
3 changed files with 31 additions and 22 deletions

View File

@ -19,35 +19,40 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { reaction } from "mobx"; import { autorun } from "mobx";
import { broadcastMessage, subscribeToBroadcast, unsubscribeFromBroadcast } from "../common/ipc"; import { broadcastMessage, subscribeToBroadcast, unsubscribeFromBroadcast } from "../common/ipc";
import { CatalogEntityRegistry } from "../common/catalog"; import { CatalogEntityRegistry } from "../common/catalog";
import "../common/catalog-entities/kubernetes-cluster"; import "../common/catalog-entities/kubernetes-cluster";
import { Disposer } from "../common/utils"; import { Disposer, disposer } from "../common/utils";
import logger from "./logger";
export class CatalogPusher { export class CatalogPusher {
static logPrefix = `[CatalogPusher]`;
static init(catalog: CatalogEntityRegistry) { static init(catalog: CatalogEntityRegistry) {
new CatalogPusher(catalog).init(); return new CatalogPusher(catalog).init();
} }
private constructor(private catalog: CatalogEntityRegistry) { private constructor(private catalog: CatalogEntityRegistry) {
} }
init() { private init(): Disposer {
const disposers: Disposer[] = [ logger.info(`${CatalogPusher.logPrefix}: init`);
reaction(
() => this.catalog.items,
(items) => broadcastMessage("catalog:items", items),
{ fireImmediately: true }
),
];
const listener = subscribeToBroadcast("catalog:broadcast", () => { const dispose = disposer();
const broadcastItems = () => {
logger.info(`${CatalogPusher.logPrefix}: broadcasting entities`);
broadcastMessage("catalog:items", this.catalog.items); broadcastMessage("catalog:items", this.catalog.items);
}); };
disposers.push(() => unsubscribeFromBroadcast("catalog:broadcast", listener)); // broadcast entities when catalog items gets updated
dispose.push(autorun(broadcastItems));
return disposers; // broadcast entities from IPC-event request
const listener = subscribeToBroadcast("catalog:broadcast", broadcastItems);
dispose.push(() => unsubscribeFromBroadcast("catalog:broadcast", listener));
return dispose;
} }
} }

View File

@ -19,7 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { action, computed, IComputedValue, makeObservable, observable, ObservableMap, runInAction, } from "mobx"; import { action, computed, IComputedValue, makeObservable, observable, ObservableMap, runInAction, observe } from "mobx";
import { CatalogEntity, catalogEntityRegistry } from "../../common/catalog"; import { CatalogEntity, catalogEntityRegistry } from "../../common/catalog";
import { watch } from "chokidar"; import { watch } from "chokidar";
import fs from "fs"; import fs from "fs";
@ -74,7 +74,7 @@ export class KubeconfigSyncManager extends Singleton {
this.startNewSync(filePath); this.startNewSync(filePath);
} }
this.syncListDisposer = UserStore.getInstance().syncKubeconfigEntries.observe_(change => { this.syncListDisposer = observe(UserStore.getInstance().syncKubeconfigEntries, change => {
switch (change.type) { switch (change.type) {
case "add": case "add":
this.startNewSync(change.name); this.startNewSync(change.name);

View File

@ -19,13 +19,14 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { action, observable, makeObservable } from "mobx"; import { action, makeObservable, observable } from "mobx";
import { broadcastMessage, subscribeToBroadcast } from "../../common/ipc"; import { broadcastMessage, subscribeToBroadcast } from "../../common/ipc";
import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntityKindData } from "../../common/catalog"; import { CatalogCategory, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntity, CatalogEntityData, CatalogEntityKindData } from "../../common/catalog";
import "../../common/catalog-entities"; import "../../common/catalog-entities";
import logger from "../../main/logger";
export class CatalogEntityRegistry { export class CatalogEntityRegistry {
@observable protected _items: CatalogEntity[] = observable.array(); protected _items = observable.array<CatalogEntity>();
@observable protected _activeEntity: CatalogEntity; @observable protected _activeEntity: CatalogEntity;
constructor(private categoryRegistry: CatalogCategoryRegistry) { constructor(private categoryRegistry: CatalogCategoryRegistry) {
@ -33,14 +34,17 @@ export class CatalogEntityRegistry {
} }
init() { init() {
subscribeToBroadcast("catalog:items", (ev, items: (CatalogEntityData & CatalogEntityKindData)[]) => { subscribeToBroadcast("catalog:items", (event, items: (CatalogEntityData & CatalogEntityKindData)[]) => {
logger.info(`[CatalogEntityRegistry]: received new catalog items`, items);
this.updateItems(items); this.updateItems(items);
}); });
broadcastMessage("catalog:broadcast"); broadcastMessage("catalog:broadcast");
} }
@action updateItems(items: (CatalogEntityData & CatalogEntityKindData)[]) { @action updateItems(items: (CatalogEntityData & CatalogEntityKindData)[]) {
this._items = items.map(data => this.categoryRegistry.getEntityForData(data)); this._items.replace(
items.map(data => this.categoryRegistry.getEntityForData(data))
);
} }
set activeEntity(entity: CatalogEntity) { set activeEntity(entity: CatalogEntity) {