11import { 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' ;
44import { performance } from 'firebase/app' ;
55import { FirebaseApp } from '@angular/fire' ;
66
@@ -19,7 +19,7 @@ export type TraceOptions = {
1919@Injectable ( )
2020export class AngularFirePerformance {
2121
22- performance : performance . Performance ;
22+ performance : Observable < performance . Performance > ;
2323
2424 constructor (
2525 app : FirebaseApp ,
@@ -30,11 +30,19 @@ export class AngularFirePerformance {
3030 private zone : NgZone
3131 ) {
3232
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+ ) ;
3445
35- if ( instrumentationEnabled == false ) { this . performance . instrumentationEnabled = false }
36- if ( dataCollectionEnabled == false ) { this . performance . dataCollectionEnabled = false }
37-
3846 if ( automaticallyTraceCoreNgMetrics != false ) {
3947
4048 // TODO determine more built in metrics
@@ -47,33 +55,38 @@ export class AngularFirePerformance {
4755
4856 }
4957
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+ ) ;
7790
7891 traceUntil = < T = any > ( name :string , test : ( a :T ) => boolean , options ?: TraceOptions & { orComplete ?: boolean } ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
7992 const traceSubscription = this . trace$ ( name , options ) . subscribe ( ) ;
0 commit comments