Replace global state usages of ExtensionLoader with DI

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen committed 2021-12-15 15:37:36 +02:00
1 parent 0b172de741
commit c92c8f8a11
56 files changed
+2110 -761

No files matched your search

@@ -1,116 +0,0 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import React from "react";
import { attemptInstallByInfo } from "../components/+extensions";
import { LensProtocolRouterRenderer } from "./router";
import { navigate } from "../navigation/helpers";
import { catalogEntityRegistry } from "../api/catalog-entity-registry";
import { ClusterStore } from "../../common/cluster-store";
import { EXTENSION_NAME_MATCH, EXTENSION_PUBLISHER_MATCH, LensProtocolRouter } from "../../common/protocol-handler";
import { Notifications } from "../components/notifications";
import * as routes from "../../common/routes";
export function bindProtocolAddRouteHandlers() {
LensProtocolRouterRenderer
.getInstance()
.addInternalHandler("/preferences", ({ search: { highlight }}) => {
navigate(routes.preferencesURL({ fragment: highlight }));
})
.addInternalHandler("/", ({ tail }) => {
if (tail) {
Notifications.shortInfo(
<p>
Unknown Action for <code>lens://app/{tail}</code>.{" "}
Are you on the latest version?
</p>,
);
}
navigate(routes.catalogURL());
})
.addInternalHandler("/landing", () => {
navigate(routes.catalogURL());
})
.addInternalHandler("/landing/view/:group/:kind", ({ pathname: { group, kind }}) => {
navigate(routes.catalogURL({
params: {
group, kind,
},
}));
})
.addInternalHandler("/cluster", () => {
navigate(routes.addClusterURL());
})
.addInternalHandler("/entity/:entityId/settings", ({ pathname: { entityId }}) => {
const entity = catalogEntityRegistry.getById(entityId);
if (entity) {
navigate(routes.entitySettingsURL({ params: { entityId }}));
} else {
Notifications.shortInfo(
<p>
Unknown catalog entity <code>{entityId}</code>.
</p>,
);
}
})
// Handlers below are deprecated and only kept for backward compact purposes
.addInternalHandler("/cluster/:clusterId", ({ pathname: { clusterId }}) => {
const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) {
navigate(routes.clusterViewURL({ params: { clusterId }}));
} else {
Notifications.shortInfo(
<p>
Unknown catalog entity <code>{clusterId}</code>.
</p>,
);
}
})
.addInternalHandler("/cluster/:clusterId/settings", ({ pathname: { clusterId }}) => {
const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) {
navigate(routes.entitySettingsURL({ params: { entityId: clusterId }}));
} else {
Notifications.shortInfo(
<p>
Unknown catalog entity <code>{clusterId}</code>.
</p>,
);
}
})
.addInternalHandler("/extensions", () => {
navigate(routes.extensionsURL());
})
.addInternalHandler(`/extensions/install${LensProtocolRouter.ExtensionUrlSchema}`, ({ pathname, search: { version }}) => {
const name = [
pathname[EXTENSION_PUBLISHER_MATCH],
pathname[EXTENSION_NAME_MATCH],
].filter(Boolean)
.join("/");
navigate(routes.extensionsURL());
attemptInstallByInfo({ name, version, requireConfirmation: true });
});
}
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import type { Injectable } from "@ogre-tools/injectable";
import { lifecycleEnum } from "@ogre-tools/injectable";
import attemptInstallByInfoInjectable from "../../components/+extensions/attempt-install-by-info/attempt-install-by-info.injectable";
import {
bindProtocolAddRouteHandlers,
Dependencies,
} from "./bind-protocol-add-route-handlers";
import lensProtocolRouterRendererInjectable
from "../lens-protocol-router-renderer/lens-protocol-router-renderer.injectable";
const bindProtocolAddRouteHandlersInjectable: Injectable<() => void, Dependencies> = {
getDependencies: di => ({
attemptInstallByInfo: di.inject(attemptInstallByInfoInjectable),
lensProtocolRouterRenderer: di.inject(lensProtocolRouterRendererInjectable),
}),
instantiate: bindProtocolAddRouteHandlers,
lifecycle: lifecycleEnum.singleton,
};
export default bindProtocolAddRouteHandlersInjectable;
@@ -0,0 +1,147 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import React from "react";
import type { LensProtocolRouterRenderer } from "../lens-protocol-router-renderer/lens-protocol-router-renderer";
import { navigate } from "../../navigation/helpers";
import { catalogEntityRegistry } from "../../api/catalog-entity-registry";
import { ClusterStore } from "../../../common/cluster-store";
import {
EXTENSION_NAME_MATCH,
EXTENSION_PUBLISHER_MATCH,
LensProtocolRouter,
} from "../../../common/protocol-handler";
import { Notifications } from "../../components/notifications";
import * as routes from "../../../common/routes";
import type { ExtensionInfo } from "../../components/+extensions/attempt-install-by-info/attempt-install-by-info";
export interface Dependencies {
attemptInstallByInfo: (extensionInfo: ExtensionInfo) => Promise<void>;
lensProtocolRouterRenderer: LensProtocolRouterRenderer;
}
export const bindProtocolAddRouteHandlers =
({ attemptInstallByInfo, lensProtocolRouterRenderer }: Dependencies) =>
() => {
lensProtocolRouterRenderer
.addInternalHandler("/preferences", ({ search: { highlight }}) => {
navigate(routes.preferencesURL({ fragment: highlight }));
})
.addInternalHandler("/", ({ tail }) => {
if (tail) {
Notifications.shortInfo(
<p>
Unknown Action for <code>lens://app/{tail}</code>. Are you on the
latest version?
</p>,
);
}
navigate(routes.catalogURL());
})
.addInternalHandler("/landing", () => {
navigate(routes.catalogURL());
})
.addInternalHandler(
"/landing/view/:group/:kind",
({ pathname: { group, kind }}) => {
navigate(
routes.catalogURL({
params: {
group,
kind,
},
}),
);
},
)
.addInternalHandler("/cluster", () => {
navigate(routes.addClusterURL());
})
.addInternalHandler(
"/entity/:entityId/settings",
({ pathname: { entityId }}) => {
const entity = catalogEntityRegistry.getById(entityId);
if (entity) {
navigate(routes.entitySettingsURL({ params: { entityId }}));
} else {
Notifications.shortInfo(
<p>
Unknown catalog entity <code>{entityId}</code>.
</p>,
);
}
},
)
// Handlers below are deprecated and only kept for backward compact purposes
.addInternalHandler(
"/cluster/:clusterId",
({ pathname: { clusterId }}) => {
const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) {
navigate(routes.clusterViewURL({ params: { clusterId }}));
} else {
Notifications.shortInfo(
<p>
Unknown catalog entity <code>{clusterId}</code>.
</p>,
);
}
},
)
.addInternalHandler(
"/cluster/:clusterId/settings",
({ pathname: { clusterId }}) => {
const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) {
navigate(
routes.entitySettingsURL({ params: { entityId: clusterId }}),
);
} else {
Notifications.shortInfo(
<p>
Unknown catalog entity <code>{clusterId}</code>.
</p>,
);
}
},
)
.addInternalHandler("/extensions", () => {
navigate(routes.extensionsURL());
})
.addInternalHandler(
`/extensions/install${LensProtocolRouter.ExtensionUrlSchema}`,
({ pathname, search: { version }}) => {
const name = [
pathname[EXTENSION_PUBLISHER_MATCH],
pathname[EXTENSION_NAME_MATCH],
]
.filter(Boolean)
.join("/");
navigate(routes.extensionsURL());
attemptInstallByInfo({ name, version, requireConfirmation: true });
},
);
};
+2 -2
View File
@@ -19,5 +19,5 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
export * from "./router";
export * from "./app-handlers";
export { LensProtocolRouterRenderer } from "./lens-protocol-router-renderer/lens-protocol-router-renderer";
export { bindProtocolAddRouteHandlers } from "./bind-protocol-add-route-handlers/bind-protocol-add-route-handlers";
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import type { Injectable } from "@ogre-tools/injectable";
import { lifecycleEnum } from "@ogre-tools/injectable";
import extensionLoaderInjectable from "../../../extensions/extension-loader/extension-loader.injectable";
import type { Dependencies } from "./lens-protocol-router-renderer";
import { LensProtocolRouterRenderer } from "./lens-protocol-router-renderer";
const lensProtocolRouterRendererInjectable: Injectable<
LensProtocolRouterRenderer,
Dependencies
> = {
getDependencies: di => ({
extensionLoader: di.inject(extensionLoaderInjectable),
}),
instantiate: dependencies => new LensProtocolRouterRenderer(dependencies),
lifecycle: lifecycleEnum.singleton,
};
export default lensProtocolRouterRendererInjectable;
@@ -21,11 +21,12 @@
import React from "react";
import { ipcRenderer } from "electron";
import * as proto from "../../common/protocol-handler";
import * as proto from "../../../common/protocol-handler";
import Url from "url-parse";
import { onCorrect } from "../../common/ipc";
import { foldAttemptResults, ProtocolHandlerInvalid, RouteAttempt } from "../../common/protocol-handler";
import { Notifications } from "../components/notifications";
import { onCorrect } from "../../../common/ipc";
import { foldAttemptResults, ProtocolHandlerInvalid, RouteAttempt } from "../../../common/protocol-handler";
import { Notifications } from "../../components/notifications";
import type { ExtensionLoader } from "../../../extensions/extension-loader";
function verifyIpcArgs(args: unknown[]): args is [string, RouteAttempt] {
if (args.length !== 2) {
@@ -46,7 +47,16 @@ function verifyIpcArgs(args: unknown[]): args is [string, RouteAttempt] {
}
}
export interface Dependencies {
extensionLoader: ExtensionLoader
}
export class LensProtocolRouterRenderer extends proto.LensProtocolRouter {
constructor(protected dependencies: Dependencies) {
super(dependencies);
}
/**
* This function is needed to be called early on in the renderers lifetime.
*/