1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Allow asynchronous instances in injectable typing

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2021-11-19 14:13:53 +02:00
parent 0c0dfe5966
commit c9503c1d14
5 changed files with 31 additions and 24 deletions

View File

@ -52,7 +52,7 @@ import { AppPaths } from "../common/app-paths";
import { registerCustomThemes } from "./components/monaco-editor";
import { getDi } from "./components/getDi";
import { DiContextProvider } from "@ogre-tools/injectable-react";
import type { IDependencyInjectionContainer } from "@ogre-tools/injectable";
import type { DependencyInjectionContainer } from "@ogre-tools/injectable";
if (process.isMainFrame) {
SentryInit();
@ -76,7 +76,7 @@ type AppComponent = React.ComponentType & {
init(rootElem: HTMLElement): Promise<void>;
};
export async function bootstrap(comp: () => Promise<AppComponent>, di: IDependencyInjectionContainer) {
export async function bootstrap(comp: () => Promise<AppComponent>, di: DependencyInjectionContainer) {
const rootElem = document.getElementById("app");
const logPrefix = `[BOOTSTRAP-${process.isMainFrame ? "ROOT" : "CLUSTER"}-FRAME]:`;

View File

@ -20,10 +20,10 @@
*/
import { createContainer } from "@ogre-tools/injectable";
import type { IConfigurableDependencyInjectionContainer } from "@ogre-tools/injectable";
import type { ConfigurableDependencyInjectionContainer } from "@ogre-tools/injectable";
export const getDi = () => {
const di: IConfigurableDependencyInjectionContainer = createContainer(
const di: ConfigurableDependencyInjectionContainer = createContainer(
() => require.context("./", true, /\.injectable\.(ts|tsx)$/),
);

View File

@ -24,11 +24,11 @@ import { memoize } from "lodash/fp";
import {
createContainer,
IConfigurableDependencyInjectionContainer,
ConfigurableDependencyInjectionContainer,
} from "@ogre-tools/injectable";
export const getDiForUnitTesting = () => {
const di: IConfigurableDependencyInjectionContainer = createContainer();
const di: ConfigurableDependencyInjectionContainer = createContainer();
getInjectableFilePaths()
.map(key => {

View File

@ -20,16 +20,16 @@
*/
/// <reference types="react" />
declare module "@ogre-tools/injectable-react" {
import type { IDependencyInjectionContainer, IInjectable } from "@ogre-tools/injectable";
import type { DependencyInjectionContainer, Injectable } from "@ogre-tools/injectable";
interface IDependencyInjectionContainerProviderProps {
di: IDependencyInjectionContainer;
interface DependencyInjectionContainerProviderProps {
di: DependencyInjectionContainer;
}
export const DiContextProvider: React.Provider<IDependencyInjectionContainerProviderProps>;
export const DiContextProvider: React.Provider<DependencyInjectionContainerProviderProps>;
export const Inject: <
TComponentInjectable extends IInjectable<any>,
TComponentInjectable extends Injectable<any>,
>({
Component,
injectableKey,

View File

@ -19,24 +19,29 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
declare module "@ogre-tools/injectable" {
export interface IDependencyInjectionContainer {
type Awaited<TMaybePromise> = TMaybePromise extends PromiseLike<infer TValue>
? TValue
: TMaybePromise;
export interface DependencyInjectionContainer {
inject: <
TInjectable extends IInjectable<TInstance, TDependencies>,
TInjectable extends Injectable<TInstance, TDependencies>,
TInstance,
TDependencies,
TMaybePromiseInstance = ReturnType<TInjectable["instantiate"]>,
>(
injectableKey: TInjectable,
) => ReturnType<TInjectable["instantiate"]>;
) => TMaybePromiseInstance extends PromiseLike<any>
? Awaited<TMaybePromiseInstance>
: TMaybePromiseInstance;
}
export interface IConfigurableDependencyInjectionContainer
extends IDependencyInjectionContainer {
register: (
injectable: IInjectable<any> | IComponentInjectable<any>,
) => void;
export interface ConfigurableDependencyInjectionContainer
extends DependencyInjectionContainer {
register: (injectable: Injectable<any>) => void;
preventSideEffects: () => void;
override: <TInjectable extends IInjectable<TInstance, any>, TInstance>(
override: <TInjectable extends Injectable<TInstance, any>, TInstance>(
injectable: TInjectable,
overrider:
| ReturnType<TInjectable["instantiate"]>
@ -47,7 +52,7 @@ declare module "@ogre-tools/injectable" {
) => void;
}
export interface IInjectable<
export interface Injectable<
TInstance,
TDependencies extends object = {},
TInstantiationParameter extends object = {},
@ -55,7 +60,7 @@ declare module "@ogre-tools/injectable" {
id?: string;
getDependencies: (
di?: IDependencyInjectionContainer,
di?: DependencyInjectionContainer,
) => TDependencies | Promise<TDependencies>;
lifecycle?: lifecycleEnum;
@ -63,7 +68,9 @@ declare module "@ogre-tools/injectable" {
instantiate: (
dependencies: TDependencies,
instantiationParameter: TInstantiationParameter,
) => TInstance;
) => Promise<TInstance> | TInstance;
causesSideEffects?: boolean;
}
export enum lifecycleEnum {
@ -74,5 +81,5 @@ declare module "@ogre-tools/injectable" {
// eslint-disable-next-line unused-imports/no-unused-vars-ts
export const createContainer = (...getRequireContexts: any[]) =>
IConfigurableDependencyInjectionContainer;
ConfigurableDependencyInjectionContainer;
}