mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix main->renderer catalog entity sync issue
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
parent
643b0d861f
commit
dae0be08f9
@ -24,10 +24,17 @@ import { broadcastMessage } from "../common/ipc";
|
||||
import type { CatalogEntityRegistry } from "./catalog";
|
||||
import "../common/catalog-entities/kubernetes-cluster";
|
||||
import { toJS } from "../common/utils";
|
||||
import { debounce } from "lodash";
|
||||
import type { CatalogEntity } from "../common/catalog";
|
||||
|
||||
|
||||
const broadcaster = debounce((items: CatalogEntity[]) => {
|
||||
broadcastMessage("catalog:items", items);
|
||||
}, 1_000, { trailing: true });
|
||||
|
||||
export function pushCatalogToRenderer(catalog: CatalogEntityRegistry) {
|
||||
return reaction(() => toJS(catalog.items), (items) => {
|
||||
broadcastMessage("catalog:items", items);
|
||||
broadcaster(items);
|
||||
}, {
|
||||
fireImmediately: true,
|
||||
});
|
||||
|
||||
@ -26,7 +26,7 @@ import type { CatalogEntityData, CatalogEntityKindData } from "../catalog-entity
|
||||
|
||||
class TestCatalogEntityRegistry extends CatalogEntityRegistry {
|
||||
replaceItems(items: Array<CatalogEntityData & CatalogEntityKindData>) {
|
||||
this.rawItems.replace(items);
|
||||
this.updateItems(items);
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,6 +99,32 @@ describe("CatalogEntityRegistry", () => {
|
||||
expect(catalog.items[0].status.phase).toEqual("connected");
|
||||
});
|
||||
|
||||
it("updates activeEntity", () => {
|
||||
const catalog = new TestCatalogEntityRegistry(catalogCategoryRegistry);
|
||||
const items = [{
|
||||
apiVersion: "entity.k8slens.dev/v1alpha1",
|
||||
kind: "KubernetesCluster",
|
||||
metadata: {
|
||||
uid: "123",
|
||||
name: "foobar",
|
||||
source: "test",
|
||||
labels: {}
|
||||
},
|
||||
status: {
|
||||
phase: "disconnected"
|
||||
},
|
||||
spec: {}
|
||||
}];
|
||||
|
||||
catalog.replaceItems(items);
|
||||
catalog.activeEntity = catalog.items[0];
|
||||
expect(catalog.activeEntity.status.phase).toEqual("disconnected");
|
||||
|
||||
items[0].status.phase = "connected";
|
||||
catalog.replaceItems(items);
|
||||
expect(catalog.activeEntity.status.phase).toEqual("connected");
|
||||
});
|
||||
|
||||
it("removes deleted items", () => {
|
||||
const catalog = new TestCatalogEntityRegistry(catalogCategoryRegistry);
|
||||
const items = [
|
||||
|
||||
@ -19,17 +19,16 @@
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { computed, makeObservable, observable } from "mobx";
|
||||
import { computed, observable, makeObservable, action } from "mobx";
|
||||
import { subscribeToBroadcast } from "../../common/ipc";
|
||||
import { CatalogCategory, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntity, CatalogEntityData, CatalogEntityKindData } from "../../common/catalog";
|
||||
import "../../common/catalog-entities";
|
||||
import { iter } from "../utils";
|
||||
import type { Cluster } from "../../main/cluster";
|
||||
import { ClusterStore } from "../../common/cluster-store";
|
||||
|
||||
export class CatalogEntityRegistry {
|
||||
protected rawItems = observable.array<CatalogEntityData & CatalogEntityKindData>();
|
||||
@observable.ref activeEntity?: CatalogEntity;
|
||||
protected _entities = observable.map<string, CatalogEntity>([], { deep: true });
|
||||
@observable.ref activeEntity: CatalogEntity;
|
||||
|
||||
constructor(private categoryRegistry: CatalogCategoryRegistry) {
|
||||
makeObservable(this);
|
||||
@ -37,20 +36,42 @@ export class CatalogEntityRegistry {
|
||||
|
||||
init() {
|
||||
subscribeToBroadcast("catalog:items", (ev, items: (CatalogEntityData & CatalogEntityKindData)[]) => {
|
||||
this.rawItems.replace(items);
|
||||
this.updateItems(items);
|
||||
});
|
||||
}
|
||||
|
||||
@action updateItems(items: (CatalogEntityData & CatalogEntityKindData)[]) {
|
||||
const newIds = items.map((item) => item.metadata.uid);
|
||||
|
||||
Array.from(this._entities.keys()).forEach((uid) => {
|
||||
if (!newIds.includes(uid)) this._entities.delete(uid);
|
||||
});
|
||||
|
||||
items.forEach((item) => {
|
||||
const existing = this._entities.get(item.metadata.uid);
|
||||
|
||||
if (!existing) {
|
||||
const entity = this.categoryRegistry.getEntityForData(item);
|
||||
|
||||
if (entity) this._entities.set(entity.metadata.uid, entity);
|
||||
} else {
|
||||
existing.metadata = item.metadata;
|
||||
existing.spec = item.spec;
|
||||
existing.status = item.status;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@computed get items() {
|
||||
return Array.from(iter.filterMap(this.rawItems, rawItem => this.categoryRegistry.getEntityForData(rawItem)));
|
||||
return Array.from(this._entities.values());
|
||||
}
|
||||
|
||||
@computed get entities(): Map<string, CatalogEntity> {
|
||||
return new Map(this.items.map(item => [item.metadata.uid, item]));
|
||||
return this._entities;
|
||||
}
|
||||
|
||||
getById(id: string) {
|
||||
return this.entities.get(id);
|
||||
getById<T extends CatalogEntity>(id: string) {
|
||||
return this.entities.get(id) as T;
|
||||
}
|
||||
|
||||
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user