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
Jari Kolehmainen df0f080380
Simplify pages/menus/registry extension api internal implementation (#1364)
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
Co-authored-by: Roman <ixrock@gmail.com>
2020-11-13 17:04:39 +02:00

25 lines
577 B
TypeScript

// Base class for extensions-api registries
import { action, observable } from "mobx";
export class BaseRegistry<T = any> {
private items = observable<T>([], { deep: false });
getItems(): T[] {
return this.items.toJS();
}
@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};
})
}
}