diff --git a/src/common/di-kludge/di-kludge.ts b/src/common/di-kludge/di-kludge.ts new file mode 100644 index 0000000000..863eca9382 --- /dev/null +++ b/src/common/di-kludge/di-kludge.ts @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import type { DependencyInjectionContainer } from "@ogre-tools/injectable"; + +let kludgeDi: DependencyInjectionContainer; + +export const setDiKludge = (di: DependencyInjectionContainer) => { + kludgeDi = di; +}; + +export const getDiKludge = () => kludgeDi; diff --git a/src/common/di-kludge/get-legacy-singleton/get-legacy-singleton.ts b/src/common/di-kludge/get-legacy-singleton/get-legacy-singleton.ts new file mode 100644 index 0000000000..3ba3ac5d64 --- /dev/null +++ b/src/common/di-kludge/get-legacy-singleton/get-legacy-singleton.ts @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import type { Injectable } from "@ogre-tools/injectable"; +import { getDiKludge } from "../di-kludge"; + +type Awaited = TMaybePromise extends PromiseLike + ? TValue + : TMaybePromise; + +export const getLegacySingleton = < + TInjectable extends Injectable< + TInstance, + TDependencies, + TInstantiationParameter + >, + TInstance, + TDependencies extends object, + TInstantiationParameter, + TMaybePromiseInstance = ReturnType, +>( + injectableKey: TInjectable, + ) => ({ + createInstance: (): TMaybePromiseInstance extends PromiseLike + ? Awaited + : TMaybePromiseInstance => { + const di = getDiKludge(); + + return di.inject(injectableKey); + }, + + getInstance: (): TMaybePromiseInstance extends PromiseLike + ? Awaited + : TMaybePromiseInstance => { + const di = getDiKludge(); + + return di.inject(injectableKey); + }, + + resetInstance: () => { + const di = getDiKludge(); + + // @ts-ignore + return di.purge(injectableKey); + }, + }); diff --git a/src/main/getDi.ts b/src/main/getDi.ts index 2b59923a6c..4a81bb9086 100644 --- a/src/main/getDi.ts +++ b/src/main/getDi.ts @@ -20,13 +20,19 @@ */ import { createContainer } from "@ogre-tools/injectable"; +import { setDiKludge } from "../common/di-kludge/di-kludge"; -export const getDi = () => - createContainer( +export const getDi = () => { + const di = createContainer( getRequireContextForMainCode, getRequireContextForCommonExtensionCode, ); + setDiKludge(di); + + return di; +}; + const getRequireContextForMainCode = () => require.context("./", true, /\.injectable\.(ts|tsx)$/); diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index 06b0588ae2..a977e70a26 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -26,10 +26,13 @@ import { createContainer, ConfigurableDependencyInjectionContainer, } from "@ogre-tools/injectable"; +import { setDiKludge } from "../common/di-kludge/di-kludge"; export const getDiForUnitTesting = () => { const di: ConfigurableDependencyInjectionContainer = createContainer(); + setDiKludge(di); + getInjectableFilePaths() .map(key => { // eslint-disable-next-line @typescript-eslint/no-var-requires diff --git a/src/renderer/components/getDi.tsx b/src/renderer/components/getDi.tsx index 65367a8c77..859563534e 100644 --- a/src/renderer/components/getDi.tsx +++ b/src/renderer/components/getDi.tsx @@ -21,11 +21,14 @@ import { createContainer } from "@ogre-tools/injectable"; import type { ConfigurableDependencyInjectionContainer } from "@ogre-tools/injectable"; +import { setDiKludge } from "../../common/di-kludge/di-kludge"; export const getDi = () => { const di: ConfigurableDependencyInjectionContainer = createContainer( () => require.context("./", true, /\.injectable\.(ts|tsx)$/), ); + setDiKludge(di); + return di; }; diff --git a/src/renderer/components/getDiForUnitTesting.tsx b/src/renderer/components/getDiForUnitTesting.tsx index 4e94d20dae..f182d8131e 100644 --- a/src/renderer/components/getDiForUnitTesting.tsx +++ b/src/renderer/components/getDiForUnitTesting.tsx @@ -26,10 +26,13 @@ import { createContainer, ConfigurableDependencyInjectionContainer, } from "@ogre-tools/injectable"; +import { setDiKludge } from "../../common/di-kludge/di-kludge"; export const getDiForUnitTesting = () => { const di: ConfigurableDependencyInjectionContainer = createContainer(); + setDiKludge(di); + getInjectableFilePaths() .map(key => { const injectable = require(key).default;