From 5005d34c2e13d07d7cf1981d11b2e43e8117a125 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 23 Nov 2020 12:54:39 -0500 Subject: [PATCH] change BaseRegistry to only have one type parameter (#1474) * change BaseRegistry to only have one type parameter Signed-off-by: Sebastian Malton --- src/common/utils/index.ts | 3 +++ src/common/utils/rectify-array.ts | 8 ++++++++ src/extensions/registries/base-registry.ts | 13 +++++++------ src/extensions/registries/page-registry.ts | 8 +++++--- src/renderer/utils/index.ts | 1 - 5 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 src/common/utils/rectify-array.ts diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index e43863284d..330b98fcf7 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -1,5 +1,7 @@ // Common utils (main OR renderer) +export const noop: any = () => { /* empty */ }; + export * from "./app-version"; export * from "./autobind"; export * from "./base64"; @@ -12,3 +14,4 @@ export * from "./splitArray"; export * from "./saveToAppFiles"; export * from "./singleton"; export * from "./openExternal"; +export * from "./rectify-array"; diff --git a/src/common/utils/rectify-array.ts b/src/common/utils/rectify-array.ts new file mode 100644 index 0000000000..48feb3a165 --- /dev/null +++ b/src/common/utils/rectify-array.ts @@ -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(items: T | T[]): T[] { + return Array.isArray(items) ? items : [items]; +} diff --git a/src/extensions/registries/base-registry.ts b/src/extensions/registries/base-registry.ts index 73dbd373f0..ff8760151c 100644 --- a/src/extensions/registries/base-registry.ts +++ b/src/extensions/registries/base-registry.ts @@ -1,20 +1,21 @@ // Base class for extensions-api registries import { action, observable } from "mobx"; import { LensExtension } from "../lens-extension"; +import { recitfy } from "../../common/utils"; -export class BaseRegistry { +export class BaseRegistry { private items = observable([], { deep: false }); - getItems(): I[] { - return this.items.toJS() as I[]; + 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 normalizedItems = (Array.isArray(items) ? items : [items]); - this.items.push(...normalizedItems); - return () => this.remove(...normalizedItems); + const itemArray = recitfy(items); + this.items.push(...itemArray); + return () => this.remove(...itemArray); } @action diff --git a/src/extensions/registries/page-registry.ts b/src/extensions/registries/page-registry.ts index 4b9c872715..0b385c02c4 100644 --- a/src/extensions/registries/page-registry.ts +++ b/src/extensions/registries/page-registry.ts @@ -7,6 +7,7 @@ import { compile } from "path-to-regexp"; import { BaseRegistry } from "./base-registry"; import { LensExtension } from "../lens-extension"; import logger from "../../main/logger"; +import { recitfy } from "../../common/utils"; export interface PageRegistration { /** @@ -59,12 +60,13 @@ export function getExtensionPageUrl

({ extensionId, pageId = "" return extPageRoutePath; } -export class PageRegistry extends BaseRegistry { +export class PageRegistry extends BaseRegistry { @action - add(items: PageRegistration[], ext: LensExtension) { + add(items: PageRegistration | PageRegistration[], ext: LensExtension) { + const itemArray = recitfy(items); let registeredPages: RegisteredPage[] = []; try { - registeredPages = items.map(page => ({ + registeredPages = itemArray.map(page => ({ ...page, extensionId: ext.name, routePath: getExtensionPageUrl({ extensionId: ext.name, pageId: page.id ?? page.routePath }), diff --git a/src/renderer/utils/index.ts b/src/renderer/utils/index.ts index 149d9aa1e8..f76547371d 100755 --- a/src/renderer/utils/index.ts +++ b/src/renderer/utils/index.ts @@ -1,6 +1,5 @@ // Common usage utils & helpers -export const noop: any = Function(); export const isElectron = !!navigator.userAgent.match(/Electron/); export * from "../../common/utils";