You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
15 lines
458 B
15 lines
458 B
|
1 week ago
|
type Constructor<T> = new (...args: any[]) => T;
|
||
|
|
|
||
|
|
export class Singleton {
|
||
|
|
private static instances = new Map<Constructor<Singleton>, Singleton>();
|
||
|
|
public static getInstance<T extends Singleton>(
|
||
|
|
this: Constructor<T>
|
||
|
|
): T {
|
||
|
|
let instance = Singleton.instances.get(this);
|
||
|
|
if (!instance) {
|
||
|
|
instance = new this();
|
||
|
|
Singleton.instances.set(this, instance);
|
||
|
|
}
|
||
|
|
return instance as T;
|
||
|
|
}
|
||
|
|
}
|