-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathcore.ts
43 lines (38 loc) · 1.97 KB
/
core.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
36
37
38
39
40
41
42
43
import { Version } from '@angular/core';
import { ComponentContainer } from '@firebase/component';
import { FirebaseApp, getApps } from 'firebase/app';
export const VERSION = new Version('ANGULARFIRE2_VERSION');
export const ɵisSupportedError = (module: string) =>
`The APP_INITIALIZER that is "making" isSupported() sync for the sake of convenient DI has not resolved in this
context. Rather than injecting ${module} in the constructor, first ensure that ${module} is supported by calling
\`await isSupported()\`, then retrieve the instance from the injector manually \`injector.get(${module})\`.`;
// TODO is there a better way to get at the internal types?
interface FirebaseAppWithContainer extends FirebaseApp {
container: ComponentContainer;
}
export function ɵgetDefaultInstanceOf<T= unknown>(identifier: string, provided: T[]|undefined, defaultApp: FirebaseApp): T|undefined {
if (provided) {
// Was provide* only called once? If so grab that
if (provided.length === 1) { return provided[0]; }
const providedUsingDefaultApp = provided.filter((it: any) => it.app === defaultApp);
// Was provide* only called once, using the default app? If so use that
if (providedUsingDefaultApp.length === 1) { return providedUsingDefaultApp[0]; }
}
// Grab the default instance from the defaultApp
const defaultAppWithContainer: FirebaseAppWithContainer = defaultApp as any;
const provider = defaultAppWithContainer.container.getProvider(identifier as never);
return provider.getImmediate({ optional: true });
}
export const ɵgetAllInstancesOf = <T= unknown>(identifier: string, app?: FirebaseApp): T[] => {
const apps = app ? [app] : getApps();
const instances: any[] = [];
apps.forEach((app: FirebaseAppWithContainer) => {
const provider: any = app.container.getProvider(identifier as never);
provider.instances.forEach((instance: any) => {
if (!instances.includes(instance)) {
instances.push(instance);
}
});
});
return instances;
};