mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
use common/utils/singleton class for the stores
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
1 parent
d7cffeb7a8
commit
776fc4462c
14 files changed
+263
-307
No files matched your search
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Narrowing class instances to the one.
|
||||
* Use "private" or "protected" modifier for constructor (when overriding) to disallow "new" usage.
|
||||
*
|
||||
* @example
|
||||
* const usersStore: UsersStore = UsersStore.getInstance();
|
||||
*/
|
||||
|
||||
export class Singleton {
|
||||
private static instances = new WeakMap<object, Singleton>();
|
||||
|
||||
// todo: figure out how to infer child class type + arguments
|
||||
static getInstance<T extends Singleton>(...args: any[]): T {
|
||||
if (!Singleton.instances.has(this)) {
|
||||
Singleton.instances.set(this, Reflect.construct(this, args));
|
||||
}
|
||||
return Singleton.instances.get(this) as T;
|
||||
}
|
||||
|
||||
static resetInstance() {
|
||||
Singleton.instances.delete(this);
|
||||
}
|
||||
|
||||
protected constructor() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user