mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
change BaseRegistry to only have one type parameter (#1474)
* change BaseRegistry to only have one type parameter Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
0a5e7a048d
commit
5005d34c2e
@ -1,5 +1,7 @@
|
|||||||
// Common utils (main OR renderer)
|
// Common utils (main OR renderer)
|
||||||
|
|
||||||
|
export const noop: any = () => { /* empty */ };
|
||||||
|
|
||||||
export * from "./app-version";
|
export * from "./app-version";
|
||||||
export * from "./autobind";
|
export * from "./autobind";
|
||||||
export * from "./base64";
|
export * from "./base64";
|
||||||
@ -12,3 +14,4 @@ export * from "./splitArray";
|
|||||||
export * from "./saveToAppFiles";
|
export * from "./saveToAppFiles";
|
||||||
export * from "./singleton";
|
export * from "./singleton";
|
||||||
export * from "./openExternal";
|
export * from "./openExternal";
|
||||||
|
export * from "./rectify-array";
|
||||||
|
|||||||
8
src/common/utils/rectify-array.ts
Normal file
8
src/common/utils/rectify-array.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* rectify condences the single item or array of T type, to an array.
|
||||||
|
* @param items either one item or an array of items
|
||||||
|
* @returns a list of items
|
||||||
|
*/
|
||||||
|
export function recitfy<T>(items: T | T[]): T[] {
|
||||||
|
return Array.isArray(items) ? items : [items];
|
||||||
|
}
|
||||||
@ -1,20 +1,21 @@
|
|||||||
// Base class for extensions-api registries
|
// Base class for extensions-api registries
|
||||||
import { action, observable } from "mobx";
|
import { action, observable } from "mobx";
|
||||||
import { LensExtension } from "../lens-extension";
|
import { LensExtension } from "../lens-extension";
|
||||||
|
import { recitfy } from "../../common/utils";
|
||||||
|
|
||||||
export class BaseRegistry<T = object, I extends T = T> {
|
export class BaseRegistry<T> {
|
||||||
private items = observable<T>([], { deep: false });
|
private items = observable<T>([], { deep: false });
|
||||||
|
|
||||||
getItems(): I[] {
|
getItems(): T[] {
|
||||||
return this.items.toJS() as I[];
|
return this.items.toJS();
|
||||||
}
|
}
|
||||||
|
|
||||||
add(items: T | T[], ext?: LensExtension): () => void; // allow method overloading with required "ext"
|
add(items: T | T[], ext?: LensExtension): () => void; // allow method overloading with required "ext"
|
||||||
@action
|
@action
|
||||||
add(items: T | T[]) {
|
add(items: T | T[]) {
|
||||||
const normalizedItems = (Array.isArray(items) ? items : [items]);
|
const itemArray = recitfy(items);
|
||||||
this.items.push(...normalizedItems);
|
this.items.push(...itemArray);
|
||||||
return () => this.remove(...normalizedItems);
|
return () => this.remove(...itemArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import { compile } from "path-to-regexp";
|
|||||||
import { BaseRegistry } from "./base-registry";
|
import { BaseRegistry } from "./base-registry";
|
||||||
import { LensExtension } from "../lens-extension";
|
import { LensExtension } from "../lens-extension";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
|
import { recitfy } from "../../common/utils";
|
||||||
|
|
||||||
export interface PageRegistration {
|
export interface PageRegistration {
|
||||||
/**
|
/**
|
||||||
@ -59,12 +60,13 @@ export function getExtensionPageUrl<P extends object>({ extensionId, pageId = ""
|
|||||||
return extPageRoutePath;
|
return extPageRoutePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class PageRegistry extends BaseRegistry<PageRegistration, RegisteredPage> {
|
export class PageRegistry extends BaseRegistry<RegisteredPage> {
|
||||||
@action
|
@action
|
||||||
add(items: PageRegistration[], ext: LensExtension) {
|
add(items: PageRegistration | PageRegistration[], ext: LensExtension) {
|
||||||
|
const itemArray = recitfy(items);
|
||||||
let registeredPages: RegisteredPage[] = [];
|
let registeredPages: RegisteredPage[] = [];
|
||||||
try {
|
try {
|
||||||
registeredPages = items.map(page => ({
|
registeredPages = itemArray.map(page => ({
|
||||||
...page,
|
...page,
|
||||||
extensionId: ext.name,
|
extensionId: ext.name,
|
||||||
routePath: getExtensionPageUrl({ extensionId: ext.name, pageId: page.id ?? page.routePath }),
|
routePath: getExtensionPageUrl({ extensionId: ext.name, pageId: page.id ?? page.routePath }),
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
// Common usage utils & helpers
|
// Common usage utils & helpers
|
||||||
|
|
||||||
export const noop: any = Function();
|
|
||||||
export const isElectron = !!navigator.userAgent.match(/Electron/);
|
export const isElectron = !!navigator.userAgent.match(/Electron/);
|
||||||
|
|
||||||
export * from "../../common/utils";
|
export * from "../../common/utils";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user