use common/utils/singleton class for the stores

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-03 16:57:31 +03:00
1 parent d7cffeb7a8
commit 776fc4462c
14 files changed
+263 -307

No files matched your search

+27
View File
@@ -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;
}
}