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,6 @@
|
||||
module.exports = {
|
||||
extends: "@k8slens/eslint-config/eslint",
|
||||
parserOptions: {
|
||||
project: "./tsconfig.json",
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
"@k8slens/eslint-config/prettier"
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
export * from "./src/as-global-function";
|
||||
export * from "./src/as-global-object";
|
||||
export * from "./src/global-di";
|
||||
@@ -0,0 +1 @@
|
||||
module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "@k8slens/legacy-global-di",
|
||||
"private": false,
|
||||
"version": "1.0.0-alpha.0",
|
||||
"description": "Injection tokens for implementing metrics for Lens",
|
||||
"type": "commonjs",
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/lensapp/lens.git"
|
||||
},
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"author": {
|
||||
"name": "OpenLens Authors",
|
||||
"email": "info@k8slens.dev"
|
||||
},
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/lensapp/lens",
|
||||
"scripts": {
|
||||
"build": "webpack",
|
||||
"clean": "rimraf dist/",
|
||||
"test": "jest --coverage --runInBand",
|
||||
"lint": "lens-lint",
|
||||
"lint:fix": "lens-lint --fix"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@ogre-tools/injectable": "^15.1.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@k8slens/eslint-config": "^6.5.0-alpha.2",
|
||||
"@k8slens/jest": "^6.5.0-alpha.4",
|
||||
"@k8slens/typescript": "^6.5.0-alpha.2"
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "@k8slens/typescript/config/base.json",
|
||||
"include": ["**/*.ts"]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
module.exports = require("@k8slens/webpack").configForReact;
|
||||
Reference in new issue
Block a user