1
1
import { Inject , Injectable , InjectionToken , NgZone , Optional , PLATFORM_ID } from '@angular/core' ;
2
- import { empty , Observable , of , Subscription } from 'rxjs' ;
2
+ import { EMPTY , empty , Observable , of , Subscription } from 'rxjs' ;
3
3
import { map , shareReplay , switchMap , tap } from 'rxjs/operators' ;
4
4
import { performance } from 'firebase/app' ;
5
5
import { FirebaseApp , ɵlazySDKProxy , ɵPromiseProxy } from '@angular/fire' ;
@@ -10,7 +10,8 @@ export const AUTOMATICALLY_TRACE_CORE_NG_METRICS = new InjectionToken<boolean>('
10
10
export const INSTRUMENTATION_ENABLED = new InjectionToken < boolean > ( 'angularfire2.performance.instrumentationEnabled' ) ;
11
11
export const DATA_COLLECTION_ENABLED = new InjectionToken < boolean > ( 'angularfire2.performance.dataCollectionEnabled' ) ;
12
12
13
- export interface AngularFirePerformance extends ɵPromiseProxy < performance . Performance > { }
13
+ export interface AngularFirePerformance extends ɵPromiseProxy < performance . Performance > {
14
+ }
14
15
15
16
@Injectable ( {
16
17
providedIn : 'any'
@@ -21,20 +22,25 @@ export class AngularFirePerformance {
21
22
22
23
constructor (
23
24
app : FirebaseApp ,
24
- @Optional ( ) @Inject ( INSTRUMENTATION_ENABLED ) instrumentationEnabled : boolean | null ,
25
- @Optional ( ) @Inject ( DATA_COLLECTION_ENABLED ) dataCollectionEnabled : boolean | null ,
25
+ @Optional ( ) @Inject ( INSTRUMENTATION_ENABLED ) instrumentationEnabled : boolean | null ,
26
+ @Optional ( ) @Inject ( DATA_COLLECTION_ENABLED ) dataCollectionEnabled : boolean | null ,
26
27
private zone : NgZone ,
28
+ // tslint:disable-next-line:ban-types
27
29
@Inject ( PLATFORM_ID ) platformId : Object
28
30
) {
29
31
30
32
this . performance = of ( undefined ) . pipe (
31
- switchMap ( ( ) => isPlatformBrowser ( platformId ) ? zone . runOutsideAngular ( ( ) => import ( 'firebase/performance' ) ) : empty ( ) ) ,
33
+ switchMap ( ( ) => isPlatformBrowser ( platformId ) ? zone . runOutsideAngular ( ( ) => import ( 'firebase/performance' ) ) : EMPTY ) ,
32
34
map ( ( ) => zone . runOutsideAngular ( ( ) => app . performance ( ) ) ) ,
33
35
tap ( performance => {
34
- if ( instrumentationEnabled == false ) { performance . instrumentationEnabled = false ; }
35
- if ( dataCollectionEnabled == false ) { performance . dataCollectionEnabled = false ; }
36
+ if ( instrumentationEnabled !== true ) {
37
+ performance . instrumentationEnabled = false ;
38
+ }
39
+ if ( dataCollectionEnabled !== true ) {
40
+ performance . dataCollectionEnabled = false ;
41
+ }
36
42
} ) ,
37
- shareReplay ( { bufferSize : 1 , refCount : false } ) ,
43
+ shareReplay ( { bufferSize : 1 , refCount : false } )
38
44
) ;
39
45
40
46
return ɵlazySDKProxy ( this , this . performance , zone ) ;
@@ -51,73 +57,93 @@ const trace$ = (traceId: string) => {
51
57
return new Observable < void > ( emitter => {
52
58
window . performance . mark ( startMarkName ) ;
53
59
emitter . next ( ) ;
54
- return { unsubscribe : ( ) => {
55
- window . performance . mark ( endMarkName ) ;
56
- window . performance . measure ( traceId , startMarkName , endMarkName ) ;
57
- } } ;
60
+ return {
61
+ unsubscribe : ( ) => {
62
+ window . performance . mark ( endMarkName ) ;
63
+ window . performance . measure ( traceId , startMarkName , endMarkName ) ;
64
+ }
65
+ } ;
58
66
} ) ;
59
67
} else {
60
68
return empty ( ) ;
61
69
}
62
70
} ;
63
71
64
- export const traceUntil = < T = any > ( name : string , test : ( a : T ) => boolean , options ?: { orComplete ?: boolean } ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
72
+ export const traceUntil = < T = any > (
73
+ name : string ,
74
+ test : ( a : T ) => boolean ,
75
+ options ?: { orComplete ?: boolean }
76
+ ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
65
77
const traceSubscription = trace$ ( name ) . subscribe ( ) ;
66
78
return source$ . pipe (
67
79
tap (
68
- a => test ( a ) && traceSubscription . unsubscribe ( ) ,
69
- ( ) => { } ,
80
+ a => test ( a ) && traceSubscription . unsubscribe ( ) ,
81
+ ( ) => {
82
+ } ,
70
83
( ) => options && options . orComplete && traceSubscription . unsubscribe ( )
71
84
)
72
85
) . subscribe ( subscriber ) ;
73
86
} ) ;
74
87
75
- export const traceWhile = < T = any > ( name : string , test : ( a : T ) => boolean , options ?: { orComplete ?: boolean } ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
76
- let traceSubscription : Subscription | undefined ;
88
+ export const traceWhile = < T = any > (
89
+ name : string ,
90
+ test : ( a : T ) => boolean ,
91
+ options ?: { orComplete ?: boolean }
92
+ ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
93
+ let traceSubscription : Subscription | undefined ;
77
94
return source$ . pipe (
78
95
tap (
79
- a => {
96
+ a => {
80
97
if ( test ( a ) ) {
81
98
traceSubscription = traceSubscription || trace$ ( name ) . subscribe ( ) ;
82
99
} else {
83
- traceSubscription && traceSubscription . unsubscribe ( ) ;
100
+ if ( traceSubscription ) {
101
+ traceSubscription . unsubscribe ( ) ;
102
+ }
103
+
84
104
traceSubscription = undefined ;
85
105
}
86
106
} ,
87
- ( ) => { } ,
107
+ ( ) => {
108
+ } ,
88
109
( ) => options && options . orComplete && traceSubscription && traceSubscription . unsubscribe ( )
89
110
)
90
111
) . subscribe ( subscriber ) ;
91
112
} ) ;
92
113
93
- export const traceUntilComplete = < T = any > ( name : string ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
114
+ export const traceUntilComplete = < T = any > ( name : string ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
94
115
const traceSubscription = trace$ ( name ) . subscribe ( ) ;
95
116
return source$ . pipe (
96
117
tap (
97
- ( ) => { } ,
98
- ( ) => { } ,
118
+ ( ) => {
119
+ } ,
120
+ ( ) => {
121
+ } ,
99
122
( ) => traceSubscription . unsubscribe ( )
100
123
)
101
124
) . subscribe ( subscriber ) ;
102
125
} ) ;
103
126
104
- export const traceUntilFirst = < T = any > ( name : string ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
127
+ export const traceUntilFirst = < T = any > ( name : string ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
105
128
const traceSubscription = trace$ ( name ) . subscribe ( ) ;
106
129
return source$ . pipe (
107
130
tap (
108
131
( ) => traceSubscription . unsubscribe ( ) ,
109
- ( ) => { } ,
110
- ( ) => { }
132
+ ( ) => {
133
+ } ,
134
+ ( ) => {
135
+ }
111
136
)
112
137
) . subscribe ( subscriber ) ;
113
138
} ) ;
114
139
115
- export const trace = < T = any > ( name : string ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
140
+ export const trace = < T = any > ( name : string ) => ( source$ : Observable < T > ) => new Observable < T > ( subscriber => {
116
141
const traceSubscription = trace$ ( name ) . subscribe ( ) ;
117
142
return source$ . pipe (
118
143
tap (
119
144
( ) => traceSubscription . unsubscribe ( ) ,
120
- ( ) => { } ,
145
+ ( ) => {
146
+ } ,
121
147
( ) => traceSubscription . unsubscribe ( )
122
148
)
123
149
) . subscribe ( subscriber ) ;
0 commit comments