mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
27 lines
761 B
TypeScript
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};
|
|
});
|
|
}
|
|
}
|