-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathperformance.ts
More file actions
147 lines (127 loc) · 4.86 KB
/
performance.ts
File metadata and controls
147 lines (127 loc) · 4.86 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
import { isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, InjectionToken, NgZone, Optional, PLATFORM_ID } from '@angular/core';
import { ɵPromiseProxy, ɵapplyMixins, ɵcacheInstance, ɵlazySDKProxy } from '@angular/fire/compat';
import { FirebaseApp } from '@angular/fire/compat';
import firebase from 'firebase/compat/app';
import { EMPTY, Observable, Subscription, of } from 'rxjs';
import { map, shareReplay, switchMap, tap } from 'rxjs/operators';
import { proxyPolyfillCompat } from './base';
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,
@Inject(PLATFORM_ID) platformId: object
) {
this.performance = of(undefined).pipe(
switchMap(() => isPlatformBrowser(platformId) ? zone.runOutsideAngular(() => import('firebase/compat/performance')) : EMPTY),
map(() => ɵcacheInstance(`performance`, 'AngularFirePerformance', app.name, () => {
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?.mark) {
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(),
() => undefined,
() => 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;
}
},
() => undefined,
() => options?.orComplete && 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(
() => undefined,
() => undefined,
() => 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(),
() => undefined,
() => undefined )
).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(),
() => undefined,
() => traceSubscription.unsubscribe()
)
).subscribe(subscriber);
});
ɵapplyMixins(AngularFirePerformance, [proxyPolyfillCompat]);