1
0
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:
Janne Savolainen 2021-12-09 08:09:49 +02:00
parent f9cf08b4a8
commit 34a47f5f44
No known key found for this signature in database
GPG Key ID: 5F465B5672372402
6 changed files with 109 additions and 2 deletions

View 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;

View File

@ -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);
},
});

View File

@ -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)$/);

View File

@ -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

View File

@ -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;
};

View File

@ -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;