Repository navigation
Expand file tree
/
Copy pathperformance.ts
More file actions
155 lines (138 loc) · 5.06 KB
/
Copy pathperformance.ts
File metadata and controls
155 lines (138 loc) · 5.06 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Inject, Injectable, InjectionToken, NgZone, Optional, PLATFORM_ID } from '@angular/core';
import { EMPTY, Observable, of, Subscription } from 'rxjs';
import { map, shareReplay, switchMap, tap } from 'rxjs/operators';
import firebase from 'firebase/app';
import { FirebaseApp, ɵapplyMixins, ɵlazySDKProxy, ɵPromiseProxy } from '@angular/fire';
import { isPlatformBrowser } from '@angular/common';
import { proxyPolyfillCompat } from './base';
import { ɵfetchInstance } from '@angular/fire';
// SEMVER @ v6, drop and move core ng metrics to a service
export const AUTOMATICALLY_TRACE_CORE_NG_METRICS = new InjectionToken<boolean>('angularfire2.performance.auto_trace');
export const INSTRUMENTATION_ENABLED = new InjectionToken<boolean>('angularfire2.performance.instrumentationEnabled');
export const DATA_COLLECTION_ENABLED = new InjectionToken<boolean>('angularfire2.performance.dataCollectionEnabled');
export interface AngularFirePerformance extends ɵPromiseProxy<firebase.performance.Performance> {
}
@Injectable({
providedIn: 'any'
})
export class AngularFirePerformance {
private readonly performance: Observable<firebase.performance.Performance>;
constructor(
app: FirebaseApp,
@Optional() @Inject(INSTRUMENTATION_ENABLED) instrumentationEnabled: boolean | null,
@Optional() @Inject(DATA_COLLECTION_ENABLED) dataCollectionEnabled: boolean | null,
private zone: NgZone,
// tslint:disable-next-line:ban-types
@Inject(PLATFORM_ID) platformId: Object
) {
this.performance = of(undefined).pipe(
switchMap(() => isPlatformBrowser(platformId) ? zone.runOutsideAngular(() => import('firebase/performance')) : EMPTY),
map(() => ɵfetchInstance(`performance`, 'AngularFirePerformance', app, () => {
const performance = zone.runOutsideAngular(() => app.performance());
if (instrumentationEnabled === false) {
performance.instrumentationEnabled = false;
}
if (dataCollectionEnabled === false) {
performance.dataCollectionEnabled = false;
}
return performance;
}, [instrumentationEnabled, dataCollectionEnabled])),
shareReplay({ bufferSize: 1, refCount: false })
);
return ɵlazySDKProxy(this, this.performance, zone);
}
}
const trace$ = (traceId: string) => {
if (typeof window !== 'undefined' && window.performance) {
const entries = window.performance.getEntriesByName(traceId, 'measure') || [];
const startMarkName = `_${traceId}Start[${entries.length}]`;
const endMarkName = `_${traceId}End[${entries.length}]`;
return new Observable<void>(emitter => {
window.performance.mark(startMarkName);
emitter.next();
return {
unsubscribe: () => {
window.performance.mark(endMarkName);
window.performance.measure(traceId, startMarkName, endMarkName);
}
};
});
} else {
return EMPTY;
}
};
export const traceUntil = <T = any>(
name: string,
test: (a: T) => boolean,
options?: { orComplete?: boolean }
) => (source$: Observable<T>) => new Observable<T>(subscriber => {
const traceSubscription = trace$(name).subscribe();
return source$.pipe(
tap(
a => test(a) && traceSubscription.unsubscribe(),
() => {
},
() => options && options.orComplete && traceSubscription.unsubscribe()
)
).subscribe(subscriber);
});
export const traceWhile = <T = any>(
name: string,
test: (a: T) => boolean,
options?: { orComplete?: boolean }
) => (source$: Observable<T>) => new Observable<T>(subscriber => {
let traceSubscription: Subscription | undefined;
return source$.pipe(
tap(
a => {
if (test(a)) {
traceSubscription = traceSubscription || trace$(name).subscribe();
} else {
if (traceSubscription) {
traceSubscription.unsubscribe();
}
traceSubscription = undefined;
}
},
() => {
},
() => options && options.orComplete && traceSubscription && traceSubscription.unsubscribe()
)
).subscribe(subscriber);
});
export const traceUntilComplete = <T = any>(name: string) => (source$: Observable<T>) => new Observable<T>(subscriber => {
const traceSubscription = trace$(name).subscribe();
return source$.pipe(
tap(
() => {
},
() => {
},
() => traceSubscription.unsubscribe()
)
).subscribe(subscriber);
});
export const traceUntilFirst = <T = any>(name: string) => (source$: Observable<T>) => new Observable<T>(subscriber => {
const traceSubscription = trace$(name).subscribe();
return source$.pipe(
tap(
() => traceSubscription.unsubscribe(),
() => {
},
() => {
}
)
).subscribe(subscriber);
});
export const trace = <T = any>(name: string) => (source$: Observable<T>) => new Observable<T>(subscriber => {
const traceSubscription = trace$(name).subscribe();
return source$.pipe(
tap(
() => traceSubscription.unsubscribe(),
() => {
},
() => traceSubscription.unsubscribe()
)
).subscribe(subscriber);
});
ɵapplyMixins(AngularFirePerformance, [proxyPolyfillCompat]);