src/app/core/config/registry/dynamic-registry.ts
A registry is an affordance to register dynamic objects to strings. It is commonly used to dynamically load entities, views or routes from the config
A registry cannot be instantiated directly. Instead, you should subclass from the registry and register it in the AppModule See EntityRegistry for an example
Map
Methods |
|
constructor(beforeAddCheck?: (key?: string,mapping?: T) => void)
|
||||||
Parameters :
|
Public add | |||||||||
add(key: string, mapping: T)
|
|||||||||
Parameters :
Returns :
void
|
Public addAll | ||||||
addAll(tuples: [])
|
||||||
Parameters :
Returns :
void
|
Public allowDuplicates | ||||||||
allowDuplicates(allow: boolean)
|
||||||||
Calling this will allow the same keys to be added multiple times without thrown errors. This is useful for storybook where live-updates re-trigger the decorator while the registry is cached.
Parameters :
Returns :
void
|
Public get | ||||||
get(key: string)
|
||||||
Parameters :
Returns :
T
|
import { environment } from "../../../../environments/environment";
/**
* A registry is an affordance to register dynamic objects to strings.
* It is commonly used to dynamically load entities, views or routes from the config
*
* A registry cannot be instantiated directly. Instead, you should subclass from the registry
* and register it in the {@link AppModule}
* @see EntityRegistry for an example
*/
export abstract class Registry<T> extends Map<string, T> {
// This controls whether the registry will throw an error when a key is added multiple times
private failOnDuplicate = true;
constructor(private beforeAddCheck?: (key: string, mapping: T) => void) {
super();
this.failOnDuplicate = environment.production;
}
public add(key: string, mapping: T) {
this.beforeAddCheck?.(key, mapping);
if (this.has(key) && this.failOnDuplicate) {
throw Error(
`${
this.constructor.name
}: Duplicate entity definition: ${key} is already registered with element ${this.get(
key,
)}`,
);
}
this.set(key, mapping);
}
public addAll(tuples: [string, T][]) {
tuples.forEach(([name, value]) => this.add(name, value));
}
public override get(key: string): T {
if (!this.has(key)) {
throw Error(
`${this.constructor.name}: Requested item "${key}" is not registered. See dynamic-registry.ts for more details.`,
);
// To register a component, add @DynamicComponent("COMPONENTNAME") to the components .ts-file and implement the onInitFromDynamicConfig method, e.g. onInitFromDynamicConfig(config: any) {}
}
return super.get(key);
}
/**
* Calling this will allow the same keys to be added multiple times without thrown errors.
* This is useful for storybook where live-updates re-trigger the decorator while the registry is cached.
*/
public allowDuplicates(allow: boolean = true) {
this.failOnDuplicate = !allow;
}
}