1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/extensions/registries/base-registry.ts
Panu Horsmalahti 4e02e086a9
Remove extension when folder is removed during runtime (#1518)
Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
2020-11-26 09:40:37 +02:00

28 lines
730 B
TypeScript

// Base class for extensions-api registries
import { action, observable } from "mobx";
import { LensExtension } from "../lens-extension";
import { rectify } from "../../common/utils";
export class BaseRegistry<T> {
private items = observable<T>([], { deep: false });
getItems(): T[] {
return this.items.toJS();
}
add(items: T | T[], ext?: LensExtension): () => void; // allow method overloading with required "ext"
@action
add(items: T | T[]) {
const itemArray = rectify(items);
this.items.push(...itemArray);
return () => this.remove(...itemArray);
}
@action
remove(...items: T[]) {
items.forEach(item => {
this.items.remove(item); // works because of {deep: false};
});
}
}