mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Introduce a way to start replacing usages of Singleton base-class with no changes required on use places
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
f9cf08b4a8
commit
34a47f5f44
29
src/common/di-kludge/di-kludge.ts
Normal file
29
src/common/di-kludge/di-kludge.ts
Normal file
@ -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;
|
||||||
@ -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> = TMaybePromise extends PromiseLike<infer TValue>
|
||||||
|
? TValue
|
||||||
|
: TMaybePromise;
|
||||||
|
|
||||||
|
export const getLegacySingleton = <
|
||||||
|
TInjectable extends Injectable<
|
||||||
|
TInstance,
|
||||||
|
TDependencies,
|
||||||
|
TInstantiationParameter
|
||||||
|
>,
|
||||||
|
TInstance,
|
||||||
|
TDependencies extends object,
|
||||||
|
TInstantiationParameter,
|
||||||
|
TMaybePromiseInstance = ReturnType<TInjectable["instantiate"]>,
|
||||||
|
>(
|
||||||
|
injectableKey: TInjectable,
|
||||||
|
) => ({
|
||||||
|
createInstance: (): TMaybePromiseInstance extends PromiseLike<any>
|
||||||
|
? Awaited<TMaybePromiseInstance>
|
||||||
|
: TMaybePromiseInstance => {
|
||||||
|
const di = getDiKludge();
|
||||||
|
|
||||||
|
return di.inject(injectableKey);
|
||||||
|
},
|
||||||
|
|
||||||
|
getInstance: (): TMaybePromiseInstance extends PromiseLike<any>
|
||||||
|
? Awaited<TMaybePromiseInstance>
|
||||||
|
: TMaybePromiseInstance => {
|
||||||
|
const di = getDiKludge();
|
||||||
|
|
||||||
|
return di.inject(injectableKey);
|
||||||
|
},
|
||||||
|
|
||||||
|
resetInstance: () => {
|
||||||
|
const di = getDiKludge();
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
return di.purge(injectableKey);
|
||||||
|
},
|
||||||
|
});
|
||||||
@ -20,13 +20,19 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { createContainer } from "@ogre-tools/injectable";
|
import { createContainer } from "@ogre-tools/injectable";
|
||||||
|
import { setDiKludge } from "../common/di-kludge/di-kludge";
|
||||||
|
|
||||||
export const getDi = () =>
|
export const getDi = () => {
|
||||||
createContainer(
|
const di = createContainer(
|
||||||
getRequireContextForMainCode,
|
getRequireContextForMainCode,
|
||||||
getRequireContextForCommonExtensionCode,
|
getRequireContextForCommonExtensionCode,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
setDiKludge(di);
|
||||||
|
|
||||||
|
return di;
|
||||||
|
};
|
||||||
|
|
||||||
const getRequireContextForMainCode = () =>
|
const getRequireContextForMainCode = () =>
|
||||||
require.context("./", true, /\.injectable\.(ts|tsx)$/);
|
require.context("./", true, /\.injectable\.(ts|tsx)$/);
|
||||||
|
|
||||||
|
|||||||
@ -26,10 +26,13 @@ import {
|
|||||||
createContainer,
|
createContainer,
|
||||||
ConfigurableDependencyInjectionContainer,
|
ConfigurableDependencyInjectionContainer,
|
||||||
} from "@ogre-tools/injectable";
|
} from "@ogre-tools/injectable";
|
||||||
|
import { setDiKludge } from "../common/di-kludge/di-kludge";
|
||||||
|
|
||||||
export const getDiForUnitTesting = () => {
|
export const getDiForUnitTesting = () => {
|
||||||
const di: ConfigurableDependencyInjectionContainer = createContainer();
|
const di: ConfigurableDependencyInjectionContainer = createContainer();
|
||||||
|
|
||||||
|
setDiKludge(di);
|
||||||
|
|
||||||
getInjectableFilePaths()
|
getInjectableFilePaths()
|
||||||
.map(key => {
|
.map(key => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
|
|||||||
@ -21,11 +21,14 @@
|
|||||||
|
|
||||||
import { createContainer } from "@ogre-tools/injectable";
|
import { createContainer } from "@ogre-tools/injectable";
|
||||||
import type { ConfigurableDependencyInjectionContainer } from "@ogre-tools/injectable";
|
import type { ConfigurableDependencyInjectionContainer } from "@ogre-tools/injectable";
|
||||||
|
import { setDiKludge } from "../../common/di-kludge/di-kludge";
|
||||||
|
|
||||||
export const getDi = () => {
|
export const getDi = () => {
|
||||||
const di: ConfigurableDependencyInjectionContainer = createContainer(
|
const di: ConfigurableDependencyInjectionContainer = createContainer(
|
||||||
() => require.context("./", true, /\.injectable\.(ts|tsx)$/),
|
() => require.context("./", true, /\.injectable\.(ts|tsx)$/),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
setDiKludge(di);
|
||||||
|
|
||||||
return di;
|
return di;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -26,10 +26,13 @@ import {
|
|||||||
createContainer,
|
createContainer,
|
||||||
ConfigurableDependencyInjectionContainer,
|
ConfigurableDependencyInjectionContainer,
|
||||||
} from "@ogre-tools/injectable";
|
} from "@ogre-tools/injectable";
|
||||||
|
import { setDiKludge } from "../../common/di-kludge/di-kludge";
|
||||||
|
|
||||||
export const getDiForUnitTesting = () => {
|
export const getDiForUnitTesting = () => {
|
||||||
const di: ConfigurableDependencyInjectionContainer = createContainer();
|
const di: ConfigurableDependencyInjectionContainer = createContainer();
|
||||||
|
|
||||||
|
setDiKludge(di);
|
||||||
|
|
||||||
getInjectableFilePaths()
|
getInjectableFilePaths()
|
||||||
.map(key => {
|
.map(key => {
|
||||||
const injectable = require(key).default;
|
const injectable = require(key).default;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user