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 ca67caea60 Enforce semicolons in eslint
Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
2020-11-20 07:46:49 +03:00

27 lines
761 B
TypeScript

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