-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathcache.ts
35 lines (30 loc) · 1.17 KB
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { isDevMode } from '@angular/core';
export function ɵcacheInstance<T>(cacheKey: any, moduleName: string, appName: string, fn: () => T, deps: any): T {
const [, instance, cachedDeps] = globalThis.ɵAngularfireInstanceCache.find((it: any) => it[0] === cacheKey) || [];
if (instance) {
if (!matchDep(deps, cachedDeps)) {
log('error', `${moduleName} was already initialized on the ${appName} Firebase App with different settings.${IS_HMR ? ' You may need to reload as Firebase is not HMR aware.' : ''}`);
log('warn', {is: deps, was: cachedDeps});
}
return instance;
} else {
const newInstance = fn();
globalThis.ɵAngularfireInstanceCache.push([cacheKey, newInstance, deps]);
return newInstance;
}
}
function matchDep(a: any, b: any) {
try {
return a.toString() === b.toString();
} catch (_) {
return a === b;
}
}
const IS_HMR = typeof module !== 'undefined' && !!(module as any).hot;
const log = (level: 'log'|'error'|'info'|'warn', ...args: any) => {
if (isDevMode() && typeof console !== 'undefined') {
// eslint-disable-next-line no-console
console[level](...args);
}
};
globalThis.ɵAngularfireInstanceCache ||= [];