Skip to content

Commit 65a1e23

Browse files
committed
PR feedback, added some debugAsserts
1 parent b1cb5a2 commit 65a1e23

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages-exp/auth-exp/src/core/util/instantiator.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { debugAssert } from './assert';
19+
1820
/**
1921
* Our API has a lot of one-off constants that are used to do things.
2022
* Unfortunately we can't export these as classes instantiated directly since
@@ -26,14 +28,18 @@ export interface SingletonInstantiator<T> {
2628
new (): T;
2729
}
2830

29-
const persistenceCache: Map<unknown, unknown> = new Map();
31+
const instanceCache: Map<unknown, unknown> = new Map();
3032

3133
export function _getInstance<T>(cls: unknown): T {
32-
if (persistenceCache.has(cls)) {
33-
return persistenceCache.get(cls)! as T;
34+
debugAssert(cls instanceof Function, 'Expected a class definition');
35+
let instance = instanceCache.get(cls) as T|undefined;
36+
37+
if (instance) {
38+
debugAssert(instance instanceof cls, 'Instance stored in cache mismatched with class');
39+
return instance;
3440
}
3541

36-
const persistence = new (cls as SingletonInstantiator<T>)();
37-
persistenceCache.set(cls, persistence);
38-
return persistence;
42+
instance = new (cls as SingletonInstantiator<T>)();
43+
instanceCache.set(cls, instance);
44+
return instance;
3945
}

0 commit comments

Comments
 (0)