27 lines
No EOL
761 B
TypeScript
27 lines
No EOL
761 B
TypeScript
export class Container {
|
|
static instance: Container;
|
|
|
|
private instances: Map<string, object> = new Map();
|
|
|
|
public set<T extends {constructor: {name: string}}>(instance: T, name: string|null = null): void
|
|
{
|
|
const settingName = name ?? instance.constructor.name;
|
|
this.instances.set(settingName.toLowerCase(), instance);
|
|
}
|
|
|
|
public get<T>(name: string): T
|
|
{
|
|
return <T>this.instances.get(name.toLowerCase());
|
|
}
|
|
|
|
static getInstance(): Container {
|
|
if (!Container.instance) {
|
|
Container.instance = new Container();
|
|
}
|
|
|
|
return Container.instance;
|
|
}
|
|
public static get<T>(name: string): T {
|
|
return Container.instance.get<T>(name);
|
|
}
|
|
} |