From 382da272b8b975adb85c8bd4c9a3f059b58d94ef Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 16 Dec 2021 17:13:15 -0500 Subject: [PATCH] Add notification on timeout and force quick getting of entities on frame init Signed-off-by: Sebastian Malton --- src/common/ipc/catalog.ts | 32 +++++++++++++++++++++ src/main/catalog-pusher.ts | 21 ++++++++------ src/renderer/api/catalog-entity-registry.ts | 7 ++++- src/renderer/cluster-frame.tsx | 7 +++++ 4 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 src/common/ipc/catalog.ts diff --git a/src/common/ipc/catalog.ts b/src/common/ipc/catalog.ts new file mode 100644 index 0000000000..7477648594 --- /dev/null +++ b/src/common/ipc/catalog.ts @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +export enum CatalogIpcEvents { + /** + * This is broadcast on whenever there is an update to any catalog item + */ + ITEMS = "catalog:items", + + /** + * This can be sent from renderer to main to initialize a broadcast of ITEMS + */ + INIT = "catalog:init", +} diff --git a/src/main/catalog-pusher.ts b/src/main/catalog-pusher.ts index b662c36f69..b86a03c7b1 100644 --- a/src/main/catalog-pusher.ts +++ b/src/main/catalog-pusher.ts @@ -20,22 +20,25 @@ */ import { reaction } from "mobx"; -import { broadcastMessage } from "../common/ipc"; +import { broadcastMessage, ipcMainOn } from "../common/ipc"; import type { CatalogEntityRegistry } from "./catalog"; import "../common/catalog-entities/kubernetes-cluster"; -import { toJS } from "../common/utils"; +import { disposer, toJS } from "../common/utils"; import { debounce } from "lodash"; import type { CatalogEntity } from "../common/catalog"; - +import { CatalogIpcEvents } from "../common/ipc/catalog"; const broadcaster = debounce((items: CatalogEntity[]) => { - broadcastMessage("catalog:items", items); + broadcastMessage(CatalogIpcEvents.ITEMS, items); }, 1_000, { leading: true, trailing: true }); export function pushCatalogToRenderer(catalog: CatalogEntityRegistry) { - return reaction(() => toJS(catalog.items), (items) => { - broadcaster(items); - }, { - fireImmediately: true, - }); + return disposer( + ipcMainOn(CatalogIpcEvents.INIT, () => broadcaster(toJS(catalog.items))), + reaction(() => toJS(catalog.items), (items) => { + broadcaster(items); + }, { + fireImmediately: true, + }), + ); } diff --git a/src/renderer/api/catalog-entity-registry.ts b/src/renderer/api/catalog-entity-registry.ts index abeaae6f68..4885c8f265 100644 --- a/src/renderer/api/catalog-entity-registry.ts +++ b/src/renderer/api/catalog-entity-registry.ts @@ -30,6 +30,8 @@ import { once } from "lodash"; import logger from "../../common/logger"; import { catalogEntityRunContext } from "./catalog-entity"; import { CatalogRunEvent } from "../../common/catalog/catalog-run-event"; +import { ipcRenderer } from "electron"; +import { CatalogIpcEvents } from "../../common/ipc/catalog"; export type EntityFilter = (entity: CatalogEntity) => any; export type CatalogEntityOnBeforeRun = (event: CatalogRunEvent) => void | Promise; @@ -70,9 +72,12 @@ export class CatalogEntityRegistry { } init() { - ipcRendererOn("catalog:items", (event, items: (CatalogEntityData & CatalogEntityKindData)[]) => { + ipcRendererOn(CatalogIpcEvents.ITEMS, (event, items: (CatalogEntityData & CatalogEntityKindData)[]) => { this.updateItems(items); }); + + // Make sure that we get items ASAP and not the next time one of them changes + ipcRenderer.send(CatalogIpcEvents.INIT); } @action updateItems(items: (CatalogEntityData & CatalogEntityKindData)[]) { diff --git a/src/renderer/cluster-frame.tsx b/src/renderer/cluster-frame.tsx index 465a78b591..5113377116 100755 --- a/src/renderer/cluster-frame.tsx +++ b/src/renderer/cluster-frame.tsx @@ -106,6 +106,13 @@ export class ClusterFrame extends React.Component { when( () => Boolean(catalogEntityRegistry.activeEntity), () => extensionLoader.loadOnClusterRenderer(catalogEntityRegistry.activeEntity as KubernetesCluster), + { + timeout: 15_000, + onError: (error) => { + console.warn("[CLUSTER-FRAME]: error from activeEntity when()", error); + Notifications.error("Failed to get KubernetesCluster for this view. Extensions will not be loaded."); + }, + }, ); setTimeout(() => {