1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/legacy-global-di/src/as-global-object.ts
Sebastian Malton 576373b16b chore: Extract legacy global di into separate package
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-05-02 10:11:52 -07:00

43 lines
1.3 KiB
TypeScript

/**
* 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;