1
1
import { Injectable , NgZone , ApplicationRef , InjectionToken , Inject , Optional } from '@angular/core' ;
2
- import { Observable , Subscription } from 'rxjs' ;
3
- import { first , tap } from 'rxjs/operators' ;
2
+ import { Observable , Subscription , from } from 'rxjs' ;
3
+ import { first , tap , map , shareReplay , switchMap } from 'rxjs/operators' ;
4
4
import { performance } from 'firebase/app' ;
5
5
import { FirebaseApp } from '@angular/fire' ;
6
6
@@ -19,7 +19,7 @@ export type TraceOptions = {
19
19
@Injectable ( )
20
20
export class AngularFirePerformance {
21
21
22
- performance : performance . Performance ;
22
+ performance : Observable < performance . Performance > ;
23
23
24
24
constructor (
25
25
app : FirebaseApp ,
@@ -30,11 +30,19 @@ export class AngularFirePerformance {
30
30
private zone : NgZone
31
31
) {
32
32
33
- this . performance = zone . runOutsideAngular ( ( ) => app . performance ( ) ) ;
33
+ // @ts -ignore zapping in the UMD in the build script
34
+ const requirePerformance = from ( import ( 'firebase/performance' ) ) ;
35
+
36
+ this . performance = requirePerformance . pipe (
37
+ // SEMVER while < 6 need to type, drop next major
38
+ map ( ( ) => zone . runOutsideAngular ( ( ) => < performance . Performance > app . performance ( ) ) ) ,
39
+ tap ( performance => {
40
+ if ( instrumentationEnabled == false ) { performance . instrumentationEnabled = false }
41
+ if ( dataCollectionEnabled == false ) { performance . dataCollectionEnabled = false }
42
+ } ) ,
43
+ shareReplay ( 1 )
44
+ ) ;
34
45
35
- if ( instrumentationEnabled == false ) { this . performance . instrumentationEnabled = false }
36
- if ( dataCollectionEnabled == false ) { this . performance . dataCollectionEnabled = false }
37
-
38
46
if ( automaticallyTraceCoreNgMetrics != false ) {
39
47
40
48
// TODO determine more built in metrics
@@ -47,33 +55,38 @@ export class AngularFirePerformance {
47
55
48
56
}
49
57
50
- trace$ = ( name :string , options ?: TraceOptions ) => new Observable < void > ( emitter =>
51
- this . zone . runOutsideAngular ( ( ) => {
52
- const trace = this . performance . trace ( name ) ;
53
- options && options . metrics && Object . keys ( options . metrics ) . forEach ( metric => {
54
- trace . putMetric ( metric , options ! . metrics ! [ metric ] )
55
- } ) ;
56
- options && options . attributes && Object . keys ( options . attributes ) . forEach ( attribute => {
57
- trace . putAttribute ( attribute , options ! . attributes ! [ attribute ] )
58
- } ) ;
59
- const attributeSubscriptions = options && options . attribute$ ? Object . keys ( options . attribute$ ) . map ( attribute =>
60
- options ! . attribute$ ! [ attribute ] . subscribe ( next => trace . putAttribute ( attribute , next ) )
61
- ) : [ ] ;
62
- const metricSubscriptions = options && options . metric$ ? Object . keys ( options . metric$ ) . map ( metric =>
63
- options ! . metric$ ! [ metric ] . subscribe ( next => trace . putMetric ( metric , next ) )
64
- ) : [ ] ;
65
- const incrementOnSubscriptions = options && options . incrementMetric$ ? Object . keys ( options . incrementMetric$ ) . map ( metric =>
66
- options ! . incrementMetric$ ! [ metric ] . subscribe ( next => trace . incrementMetric ( metric , next || undefined ) )
67
- ) : [ ] ;
68
- emitter . next ( trace . start ( ) ) ;
69
- return { unsubscribe : ( ) => {
70
- trace . stop ( ) ;
71
- metricSubscriptions . forEach ( m => m . unsubscribe ( ) ) ;
72
- incrementOnSubscriptions . forEach ( m => m . unsubscribe ( ) ) ;
73
- attributeSubscriptions . forEach ( m => m . unsubscribe ( ) ) ;
74
- } } ;
75
- } )
76
- ) ;
58
+ trace$ = ( name :string , options ?: TraceOptions ) =>
59
+ this . performance . pipe (
60
+ switchMap ( performance =>
61
+ new Observable < void > ( emitter =>
62
+ this . zone . runOutsideAngular ( ( ) => {
63
+ const trace = performance . trace ( name ) ;
64
+ options && options . metrics && Object . keys ( options . metrics ) . forEach ( metric => {
65
+ trace . putMetric ( metric , options ! . metrics ! [ metric ] )
66
+ } ) ;
67
+ options && options . attributes && Object . keys ( options . attributes ) . forEach ( attribute => {
68
+ trace . putAttribute ( attribute , options ! . attributes ! [ attribute ] )
69
+ } ) ;
70
+ const attributeSubscriptions = options && options . attribute$ ? Object . keys ( options . attribute$ ) . map ( attribute =>
71
+ options ! . attribute$ ! [ attribute ] . subscribe ( next => trace . putAttribute ( attribute , next ) )
72
+ ) : [ ] ;
73
+ const metricSubscriptions = options && options . metric$ ? Object . keys ( options . metric$ ) . map ( metric =>
74
+ options ! . metric$ ! [ metric ] . subscribe ( next => trace . putMetric ( metric , next ) )
75
+ ) : [ ] ;
76
+ const incrementOnSubscriptions = options && options . incrementMetric$ ? Object . keys ( options . incrementMetric$ ) . map ( metric =>
77
+ options ! . incrementMetric$ ! [ metric ] . subscribe ( next => trace . incrementMetric ( metric , next || undefined ) )
78
+ ) : [ ] ;
79
+ emitter . next ( trace . start ( ) ) ;
80
+ return { unsubscribe : ( ) => {
81
+ trace . stop ( ) ;
82
+ metricSubscriptions . forEach ( m => m . unsubscribe ( ) ) ;
83
+ incrementOnSubscriptions . forEach ( m => m . unsubscribe ( ) ) ;
84
+ attributeSubscriptions . forEach ( m => m . unsubscribe ( ) ) ;
85
+ } } ;
86
+ } )
87
+ )
88
+ )
89
+ ) ;
77
90
78
91
traceUntil = < T = any > ( name :string , test : ( a :T ) => boolean , options ?: TraceOptions & { orComplete ?: boolean } ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
79
92
const traceSubscription = this . trace$ ( name , options ) . subscribe ( ) ;
0 commit comments