projects/wms-framework/src/lib/helpers/TypeResolver.ts
Class that helps in the resoluction of types/classes
Properties |
|
Methods |
|
Static classInfoDictionary |
Type : any
|
Default value : {}
|
Dictionary that stores the information of classes |
Static typesDictionary |
Type : any
|
Default value : {}
|
Dictionary that stores the information of types |
Static getClassInfo | ||||||
getClassInfo(key: string)
|
||||||
Gets the class information for the corresponding key
Parameters :
Returns :
ClassInfoConfig
The class information |
Static getClassInterfaces | ||||||
getClassInterfaces(key: string)
|
||||||
Registers a new type in the typesDictionary.
Parameters :
Returns :
string[]
The list of the interfaces implemented by the class |
Static getClassType | ||||||
getClassType(key: string)
|
||||||
Gets the class information for the corresponding key
Parameters :
Returns :
any
The class type |
Static getType | ||||||
getType(key: string)
|
||||||
Gets a type by the corresponding key
Parameters :
Returns :
any
The type |
Static getTypesRegisteredForAssembly | ||||||
getTypesRegisteredForAssembly(assemblyName: string)
|
||||||
Gets all the registered types that are registered in the given class
Parameters :
Returns :
Iterable<any>
|
Static isImplemented |
isImplemented(key: string, intefaceName: string)
|
Checks if a class implements an interface.
Returns :
boolean
True if the class implements the interface, false if not |
Static registrateType |
registrateType(key: string, type: any)
|
Registers a new type in the typesDictionary.
Returns :
void
|
Static regitrateClassInfo | ||||||||||||
regitrateClassInfo(key: string, info: ClassInfoConfig)
|
||||||||||||
Registers a new class info in the classInfoDictionary.
Parameters :
Returns :
void
|
import { ClassInfoConfig } from './ClassInfoConfig';
/**
* Class that helps in the resoluction of types/classes
*
* @export
* @class TypeResolver
*/
export class TypeResolver {
/**
* Dictionary that stores the information of types
*
* @type {any}
* @memberof TypeResolver
*/
static typesDictionary: any = {};
/**
* Dictionary that stores the information of classes
*
* @type {any}
* @memberof TypeResolver
*/
static classInfoDictionary: any = {};
/**
* Registers a new type in the typesDictionary.
*
* @param {string} key
* @param {any} type
* @memberof TypeResolver
* @static
*/
static registrateType(key: string, type: any): void {
TypeResolver.typesDictionary[key] = type;
}
/**
* Gets a type by the corresponding key
*
* @param {string} key
* @memberof TypeResolver
* @static
* @returns {any} The type
*/
static getType(key: string): any {
return TypeResolver.typesDictionary[key];
}
/**
* Registers a new class info in the classInfoDictionary.
*
* @param {string} key
* @param {ClassInfoConfig} info Class information
* @memberof TypeResolver
* @static
*/
static regitrateClassInfo(key: string, info: ClassInfoConfig) {
TypeResolver.classInfoDictionary[key] = info;
}
/**
* Gets the class information for the corresponding key
*
* @param {string} key
* @memberof TypeResolver
* @static
* @returns {ClassInfoConfig} The class information
*/
static getClassInfo(key: string): ClassInfoConfig {
return TypeResolver.classInfoDictionary[key];
}
/**
* Gets the class information for the corresponding key
*
* @param {string} key
* @memberof TypeResolver
* @static
* @returns {any} The class type
*/
static getClassType(key: string): any {
const classInfo: ClassInfoConfig = TypeResolver.classInfoDictionary[key];
return classInfo?.type;
}
/**
* Registers a new type in the typesDictionary.
*
* @param {string} key
* @memberof TypeResolver
* @static
* @returns {string[]} The list of the interfaces implemented by the class
*/
static getClassInterfaces(key: string): string[] {
const classInfo: ClassInfoConfig = TypeResolver.classInfoDictionary[key];
return classInfo.implements;
}
/**
* Checks if a class implements an interface.
*
* @param {string} key
* @param {string} intefaceName
* @memberof TypeResolver
* @static
* @returns {boolean} True if the class implements the interface, false if not
*/
static isImplemented(key: string, intefaceName: string): boolean {
const classInfo: ClassInfoConfig = TypeResolver.classInfoDictionary[key];
return classInfo.implements.includes(intefaceName);
}
/**
* Gets all the registered types that are registered in the given class
*
* @static
* @param {string} assemblyName
* @memberof TypeResolver
*/
static *getTypesRegisteredForAssembly(assemblyName: string): Iterable<any> {
for (const infoKey of Object.keys(TypeResolver.classInfoDictionary)) {
const info = TypeResolver.classInfoDictionary[infoKey];
if (
typeof info?.assembly === 'string' &&
info.assembly === assemblyName &&
info.type
) {
yield info.type;
}
}
}
}