Bump injectable to 5.1.2 (#5010)

This commit is contained in:
Iku-turso authored and GitHub committed 2022-03-17 08:15:21 -04:00
1 parent 8c8bab540c
commit 6b28f9e9c3
15 files changed
+195 -129

No files matched your search

@@ -2,29 +2,19 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Injectable, TentativeTuple } from "@ogre-tools/injectable";
import { getLegacyGlobalDiForExtensionApi } from "./legacy-global-di-for-extension-api";
import type { Inject } from "@ogre-tools/injectable";
type FactoryType = <
TInjectable extends Injectable<unknown, TInstance, TInstantiationParameter>,
TInstantiationParameter,
TInstance extends (...args: unknown[]) => any,
TFunction extends (...args: unknown[]) => any = Awaited<
ReturnType<TInjectable["instantiate"]>
>,
>(
injectableKey: TInjectable,
...instantiationParameter: TentativeTuple<TInstantiationParameter>
) => (...args: Parameters<TFunction>) => ReturnType<TFunction>;
export const asLegacyGlobalFunctionForExtensionApi = ((
injectableKey,
instantiationParameter,
) =>
(...args: any[]) => {
const injected = getLegacyGlobalDiForExtensionApi().inject(
injectableKey,
instantiationParameter,
) as unknown as (...args: any[]) => any;
export const asLegacyGlobalFunctionForExtensionApi: FactoryType =
(injectableKey, ...instantiationParameter) =>
(...args) => {
const injected = getLegacyGlobalDiForExtensionApi().inject(
injectableKey,
...instantiationParameter,
);
return injected(...args);
};
return injected(...args);
}) as Inject<false>;
@@ -0,0 +1,80 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import {
createContainer,
DiContainer,
getInjectable,
Injectable,
} from "@ogre-tools/injectable";
import { setLegacyGlobalDiForExtensionApi } from "./legacy-global-di-for-extension-api";
import { asLegacyGlobalObjectForExtensionApiWithModifications } from "./as-legacy-global-object-for-extension-api-with-modifications";
describe("asLegacyGlobalObjectForExtensionApiWithModifications", () => {
describe("given legacy global object", () => {
let di: DiContainer;
let someInjectable: Injectable<{ someProperty: string }, unknown, void>;
let actual: { someProperty: string } & {
someModificationProperty: string;
};
beforeEach(() => {
di = createContainer();
jest.spyOn(di, "inject");
setLegacyGlobalDiForExtensionApi(di);
someInjectable = getInjectable({
id: "some-injectable",
instantiate: () => ({
someProperty: "some-property-value",
}),
});
di.register(someInjectable);
actual = asLegacyGlobalObjectForExtensionApiWithModifications(
someInjectable,
{ someModificationProperty: "some-modification-value" },
);
});
it("when not accessed, does not inject yet", () => {
expect(di.inject).not.toHaveBeenCalled();
});
describe("when a property of global is accessed, ", () => {
let actualPropertyValue: string;
beforeEach(() => {
actualPropertyValue = actual.someProperty;
});
it("injects the injectable for global", () => {
expect(di.inject).toHaveBeenCalledWith(someInjectable, undefined);
});
it("global has property of injectable", () => {
expect(actualPropertyValue).toBe("some-property-value");
});
});
describe("when a property of modification is accessed, ", () => {
let actualModificationValue: string;
beforeEach(() => {
actualModificationValue = actual.someModificationProperty;
});
it("injects the injectable for global", () => {
expect(di.inject).toHaveBeenCalledWith(someInjectable, undefined);
});
it("global has property of modification", () => {
expect(actualModificationValue).toBe("some-modification-value");
});
});
});
});
@@ -2,44 +2,18 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Injectable, TentativeTuple } from "@ogre-tools/injectable";
import { getLegacyGlobalDiForExtensionApi } from "./legacy-global-di-for-extension-api";
type MapInjectables<T> = {
[Key in keyof T]: T[Key] extends () => infer Res ? Res : never;
};
import { asLegacyGlobalForExtensionApi } from "./as-legacy-global-object-for-extension-api";
import type { Injectable } from "@ogre-tools/injectable";
export const asLegacyGlobalObjectForExtensionApiWithModifications = <
TInjectable extends Injectable<unknown, unknown, TInstantiationParameter>,
TInstantiationParameter,
OtherFields extends Record<string, () => any>,
InjectableInstance extends InjectionTokenInstance,
InjectionTokenInstance,
ModificationObject extends object,
>(
injectableKey: TInjectable,
otherFields: OtherFields,
...instantiationParameter: TentativeTuple<TInstantiationParameter>
injectable: Injectable<InjectableInstance, InjectionTokenInstance, void>,
modificationObject: ModificationObject,
) =>
new Proxy(
{},
{
get(target, propertyName) {
if (propertyName === "$$typeof") {
return undefined;
}
const instance: any = getLegacyGlobalDiForExtensionApi().inject(
injectableKey,
...instantiationParameter,
);
const propertyValue = instance[propertyName] ?? otherFields[propertyName as any]();
if (typeof propertyValue === "function") {
return function (...args: any[]) {
return propertyValue.apply(instance, args);
};
}
return propertyValue;
},
},
) as ReturnType<TInjectable["instantiate"]> & MapInjectables<OtherFields>;
Object.assign(
asLegacyGlobalForExtensionApi(injectable),
modificationObject,
);
@@ -2,30 +2,37 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Injectable, TentativeTuple } from "@ogre-tools/injectable";
import type { Inject } from "@ogre-tools/injectable";
import { getLegacyGlobalDiForExtensionApi } from "./legacy-global-di-for-extension-api";
export const asLegacyGlobalObjectForExtensionApi = <
TInjectable extends Injectable<unknown, unknown, TInstantiationParameter>,
TInstantiationParameter,
>(
injectableKey: TInjectable,
...instantiationParameter: TentativeTuple<TInstantiationParameter>
) =>
export const asLegacyGlobalForExtensionApi = ((
injectable,
instantiationParameter,
) =>
new Proxy(
{},
{
apply(target: any, thisArg, argArray) {
const fn = getLegacyGlobalDiForExtensionApi().inject(
injectable,
instantiationParameter,
) as unknown as (...args: any[]) => any;
return fn(...argArray);
},
get(target, propertyName) {
if (propertyName === "$$typeof") {
return undefined;
}
const instance: any = getLegacyGlobalDiForExtensionApi().inject(
injectableKey,
...instantiationParameter,
injectable,
instantiationParameter,
);
const propertyValue = instance[propertyName];
const propertyValue = instance[propertyName] ?? target[propertyName];
if (typeof propertyValue === "function") {
return function (...args: any[]) {
@@ -36,4 +43,4 @@ export const asLegacyGlobalObjectForExtensionApi = <
return propertyValue;
},
},
) as ReturnType<TInjectable["instantiate"]>;
)) as Inject<false>;