Total Complexity | 4 |
Complexity/F | 2 |
Lines of Code | 24 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
1 | import { logger } from '@/core'; |
||
2 | |||
3 | function getSingleton(singletonClass: any) { |
||
4 | 9 | if (singletonClass) { |
|
5 | 8 | if (singletonClass.instance) { |
|
6 | 6 | return singletonClass.instance; |
|
7 | } |
||
8 | 2 | throw new Error(`Cannot get instance from ${singletonClass.name}. Make sure that ${singletonClass.name} has @Bean as class decorator`); |
|
9 | } |
||
10 | 1 | throw new Error('Please specify the type of Bean. Example: @InjectBean(BeanClass)'); |
|
11 | } |
||
12 | |||
13 | /** |
||
14 | * Gets singleton instance by its static property |
||
15 | * @decorator |
||
16 | * @param singletonClass |
||
17 | */ |
||
18 | export function InjectBean<T>(singletonClass: T) { |
||
19 | 6 | return (_value: any, context: ClassFieldDecoratorContext) => () => { |
|
20 | 6 | logger.debug(`initializing ${String(context.name)} with instance of bean ${getSingleton(singletonClass).className}`); |
|
21 | 3 | return getSingleton(singletonClass); |
|
22 | }; |
||
23 | } |
||
24 |