mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
chore: Extract legacy global di into separate package
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
1c23162b58
commit
576373b16b
49 files changed
+175
-265
No files matched your search
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getLegacyGlobalDiForExtensionApi } from "./global-di";
|
||||
import type { Inject } from "@ogre-tools/injectable";
|
||||
|
||||
export const asLegacyGlobalFunctionForExtensionApi = ((injectableKey, instantiationParameter) =>
|
||||
(...args: unknown[]) => {
|
||||
const injected = getLegacyGlobalDiForExtensionApi().inject(injectableKey, instantiationParameter) as unknown as (
|
||||
...args: unknown[]
|
||||
) => unknown;
|
||||
|
||||
return injected(...args);
|
||||
}) as Inject;
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { Inject } from "@ogre-tools/injectable";
|
||||
import { getLegacyGlobalDiForExtensionApi } from "./global-di";
|
||||
|
||||
export const asLegacyGlobalForExtensionApi = ((injectable, instantiationParameter) =>
|
||||
new Proxy(
|
||||
{},
|
||||
|
||||
{
|
||||
apply(target: unknown, thisArg, argArray: unknown[]) {
|
||||
const fn = getLegacyGlobalDiForExtensionApi().inject(injectable, instantiationParameter) as unknown as (
|
||||
...args: unknown[]
|
||||
) => unknown;
|
||||
|
||||
return fn(...argArray);
|
||||
},
|
||||
|
||||
get(target: Record<string | symbol, unknown>, propertyName) {
|
||||
if (propertyName === "$$typeof") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const instance = getLegacyGlobalDiForExtensionApi().inject(injectable, instantiationParameter) as Record<
|
||||
string | symbol,
|
||||
unknown
|
||||
>;
|
||||
|
||||
const propertyValue = instance[propertyName] ?? target[propertyName];
|
||||
|
||||
if (typeof propertyValue === "function") {
|
||||
return function (...args: unknown[]) {
|
||||
return propertyValue.apply(instance, args) as unknown;
|
||||
};
|
||||
}
|
||||
|
||||
return propertyValue;
|
||||
},
|
||||
},
|
||||
)) as Inject;
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { DiContainer } from "@ogre-tools/injectable";
|
||||
|
||||
export type Environments = "main" | "renderer";
|
||||
|
||||
const legacyGlobalDis = new Map<Environments, DiContainer>();
|
||||
|
||||
export const setLegacyGlobalDiForExtensionApi = (di: DiContainer, environment: Environments) => {
|
||||
legacyGlobalDis.set(environment, di);
|
||||
};
|
||||
|
||||
export const getLegacyGlobalDiForExtensionApi = () => {
|
||||
if (legacyGlobalDis.size > 1) {
|
||||
throw new Error("Tried to get DI container using legacy globals where there is multiple containers available.");
|
||||
}
|
||||
|
||||
const [di] = [...legacyGlobalDis.values()];
|
||||
|
||||
if (!di) {
|
||||
throw new Error("Tried to get DI container using legacy globals where there is no containers available.");
|
||||
}
|
||||
|
||||
return di;
|
||||
};
|
||||
|
||||
export function getEnvironmentSpecificLegacyGlobalDiForExtensionApi(environment: Environments) {
|
||||
const di = legacyGlobalDis.get(environment);
|
||||
|
||||
if (!di) {
|
||||
throw new Error("Tried to get DI container using legacy globals in environment which doesn't exist");
|
||||
}
|
||||
|
||||
return di;
|
||||
}
|
||||
Reference in new issue
Block a user