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

typesafe catalog category events

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-05-19 13:03:19 +03:00
parent f168f137e5
commit 8c8af5c3f7
6 changed files with 40 additions and 7 deletions

View File

@ -354,6 +354,7 @@
"ts-loader": "^7.0.5", "ts-loader": "^7.0.5",
"ts-node": "^8.10.2", "ts-node": "^8.10.2",
"type-fest": "^1.0.2", "type-fest": "^1.0.2",
"typed-emitter": "^1.3.1",
"typedoc": "0.17.0-3", "typedoc": "0.17.0-3",
"typedoc-plugin-markdown": "^2.4.0", "typedoc-plugin-markdown": "^2.4.0",
"typeface-roboto": "^0.0.75", "typeface-roboto": "^0.0.75",

View File

@ -139,7 +139,7 @@ export class KubernetesCluster extends CatalogEntity<CatalogEntityMetadata, Kube
const category = catalogCategoryRegistry.getCategoryForEntity<KubernetesClusterCategory>(this); const category = catalogCategoryRegistry.getCategoryForEntity<KubernetesClusterCategory>(this);
if (category) category.emit("contextMenuOpen", this, context); if (category) category.emit("onContextMenuOpen", this, context);
} }
} }

View File

@ -19,7 +19,8 @@
* 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 { EventEmitter } from "events"; import EventEmitter from "events";
import TypedEmitter from "typed-emitter";
import { observable } from "mobx"; import { observable } from "mobx";
type ExtractEntityMetadataType<Entity> = Entity extends CatalogEntity<infer Metadata> ? Metadata : never; type ExtractEntityMetadataType<Entity> = Entity extends CatalogEntity<infer Metadata> ? Metadata : never;
@ -47,7 +48,13 @@ export interface CatalogCategorySpec {
}; };
} }
export abstract class CatalogCategory extends EventEmitter { export interface CatalogCategoryEvents {
onLoad: () => void;
onCatalogAddMenu: (context: CatalogEntityAddMenuContext) => void;
onContextMenuOpen: (entity: CatalogEntity, context: CatalogEntityContextMenuContext) => void;
}
export abstract class CatalogCategory extends (EventEmitter as new () => TypedEmitter<CatalogCategoryEvents>) {
abstract readonly apiVersion: string; abstract readonly apiVersion: string;
abstract readonly kind: string; abstract readonly kind: string;
abstract metadata: { abstract metadata: {
@ -59,6 +66,18 @@ export abstract class CatalogCategory extends EventEmitter {
public getId(): string { public getId(): string {
return `${this.spec.group}/${this.spec.names.kind}`; return `${this.spec.group}/${this.spec.names.kind}`;
} }
public async onLoad(): Promise<void> {
for( const listener of this.listeners("onLoad")) {
await listener();
}
}
public async onCatalogAddMenu(context: CatalogEntityAddMenuContext): Promise<void> {
for( const listener of this.listeners("onCatalogAddMenu")) {
await listener(context);
}
}
} }
export interface CatalogEntityMetadata { export interface CatalogEntityMetadata {

View File

@ -50,7 +50,7 @@ export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
menuItems: this.menuItems menuItems: this.menuItems
}; };
category.emit("onCatalogAddMenu", context); category.onCatalogAddMenu(context);
} }
}, { fireImmediately: true }) }, { fireImmediately: true })
]); ]);

View File

@ -24,7 +24,7 @@ import { catalogEntityRegistry } from "../../api/catalog-entity-registry";
import { CatalogEntity, CatalogEntityActionContext } from "../../api/catalog-entity"; import { CatalogEntity, CatalogEntityActionContext } from "../../api/catalog-entity";
import { ItemObject, ItemStore } from "../../item.store"; import { ItemObject, ItemStore } from "../../item.store";
import { autobind } from "../../utils"; import { autobind } from "../../utils";
import { CatalogCategory } from "../../../common/catalog"; import { CatalogCategory, catalogCategoryRegistry } from "../../../common/catalog";
export class CatalogEntityItem implements ItemObject { export class CatalogEntityItem implements ItemObject {
constructor(public entity: CatalogEntity) {} constructor(public entity: CatalogEntity) {}
@ -98,14 +98,22 @@ export class CatalogEntityStore extends ItemStore<CatalogEntityItem> {
watch() { watch() {
const disposers: IReactionDisposer[] = [ const disposers: IReactionDisposer[] = [
reaction(() => this.entities, () => this.loadAll()), reaction(() => this.entities, async () => await this.loadAll()),
reaction(() => this.activeCategory, () => this.loadAll(), { delay: 100}) reaction(() => this.activeCategory, () => this.loadAll(), { delay: 100})
]; ];
return () => disposers.forEach((dispose) => dispose()); return () => disposers.forEach((dispose) => dispose());
} }
loadAll() { async loadAll() {
if (this.activeCategory) {
await this.activeCategory.onLoad();
} else {
for (const category of catalogCategoryRegistry.items) {
await category.onLoad();
}
}
return this.loadItems(() => this.entities); return this.loadItems(() => this.entities);
} }
} }

View File

@ -13807,6 +13807,11 @@ type-is@~1.6.17, type-is@~1.6.18:
media-typer "0.3.0" media-typer "0.3.0"
mime-types "~2.1.24" mime-types "~2.1.24"
typed-emitter@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/typed-emitter/-/typed-emitter-1.3.1.tgz#c98d71551a99d5f08ba9085ee44b8fc9b2357502"
integrity sha512-2h7utWyXgd2R2u2IuL8B4yu1gqMxbgUj2VS/MGVbFhEVQNJKXoQQoS5CBMh+eW31zFeSmDfEQ3qQf4xy5SlPVQ==
typedarray-to-buffer@^3.1.5: typedarray-to-buffer@^3.1.5:
version "3.1.5" version "3.1.5"
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080" resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"