Repository navigation
Expand file tree
/
Copy pathperformance.module.ts
More file actions
77 lines (70 loc) · 2.48 KB
/
Copy pathperformance.module.ts
File metadata and controls
77 lines (70 loc) · 2.48 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { NgModule, Optional, NgZone, InjectionToken, ModuleWithProviders, PLATFORM_ID, Injector } from '@angular/core';
import { FirebasePerformance } from 'firebase/performance';
import { ɵgetDefaultInstanceOf, ɵAngularFireSchedulers, VERSION } from '@angular/fire';
import { Performance, PerformanceInstances, PERFORMANCE_PROVIDER_NAME } from './performance';
import { FirebaseApps, FirebaseApp } from '@angular/fire/app';
import { registerVersion } from 'firebase/app';
import { isPlatformBrowser } from '@angular/common';
export const PROVIDED_PERFORMANCE_INSTANCES = new InjectionToken<Performance[]>('angularfire2.performance-instances');
export function defaultPerformanceInstanceFactory(
provided: FirebasePerformance[]|undefined,
defaultApp: FirebaseApp,
// tslint:disable-next-line:ban-types
platform: Object
) {
if (!isPlatformBrowser(platform)) { return null; }
const defaultPerformance = ɵgetDefaultInstanceOf<FirebasePerformance>(PERFORMANCE_PROVIDER_NAME, provided, defaultApp);
return defaultPerformance && new Performance(defaultPerformance);
}
export function performanceInstanceFactory(fn: (injector: Injector) => FirebasePerformance) {
// tslint:disable-next-line:ban-types
return (zone: NgZone, platform: Object, injector: Injector) => {
if (!isPlatformBrowser(platform)) { return null; }
const performance = zone.runOutsideAngular(() => fn(injector));
return new Performance(performance);
};
}
const PERFORMANCE_INSTANCES_PROVIDER = {
provide: PerformanceInstances,
deps: [
[new Optional(), PROVIDED_PERFORMANCE_INSTANCES ],
]
};
const DEFAULT_PERFORMANCE_INSTANCE_PROVIDER = {
provide: Performance,
useFactory: defaultPerformanceInstanceFactory,
deps: [
[new Optional(), PROVIDED_PERFORMANCE_INSTANCES ],
FirebaseApp,
PLATFORM_ID,
]
};
@NgModule({
providers: [
DEFAULT_PERFORMANCE_INSTANCE_PROVIDER,
PERFORMANCE_INSTANCES_PROVIDER,
]
})
export class PerformanceModule {
constructor() {
registerVersion('angularfire', VERSION.full, 'perf');
}
}
export function providePerformance(fn: (injector: Injector) => FirebasePerformance, ...deps: any[]): ModuleWithProviders<PerformanceModule> {
return {
ngModule: PerformanceModule,
providers: [{
provide: PROVIDED_PERFORMANCE_INSTANCES,
useFactory: performanceInstanceFactory(fn),
multi: true,
deps: [
NgZone,
PLATFORM_ID,
Injector,
ɵAngularFireSchedulers,
FirebaseApps,
...deps,
]
}]
};
}