Skip to content

Commit 9b870a9

Browse files
committed
Make perf lazy and fix both perf & messaging on Node
1 parent bc2a669 commit 9b870a9

File tree

4 files changed

+62
-46
lines changed

4 files changed

+62
-46
lines changed

src/messaging/messaging.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ export class AngularFireMessaging {
2323
zone: NgZone
2424
) {
2525

26-
if (!isPlatformServer(platformId)) {
26+
// @ts-ignore zapping in the UMD in the build script
27+
const requireMessaging = from(import('firebase/messaging'));
2728

28-
// @ts-ignore
29-
const requireMessaging = from(import('firebase/messaging'));
29+
this.messaging = requireMessaging.pipe(
30+
map(() => _firebaseAppFactory(options, nameOrConfig)),
31+
map(app => app.messaging()),
32+
runOutsideAngular(zone)
33+
);
3034

31-
this.messaging = requireMessaging.pipe(
32-
map(() => _firebaseAppFactory(options, nameOrConfig)),
33-
map(app => app.messaging()),
34-
runOutsideAngular(zone)
35-
);
35+
if (!isPlatformServer(platformId)) {
3636

3737
this.requestPermission = this.messaging.pipe(
3838
switchMap(messaging => messaging.requestPermission()),
@@ -41,7 +41,6 @@ export class AngularFireMessaging {
4141

4242
} else {
4343

44-
this.messaging = empty();
4544
this.requestPermission = throwError('Not available on server platform.');
4645

4746
}

src/performance/performance.module.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { NgModule } from '@angular/core';
22
import { AngularFirePerformance } from './performance';
33

4-
import 'firebase/performance';
5-
64
@NgModule({
75
providers: [ AngularFirePerformance ]
86
})

src/performance/performance.ts

+47-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
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';
44
import { performance } from 'firebase/app';
55
import { FirebaseApp } from '@angular/fire';
66

@@ -19,7 +19,7 @@ export type TraceOptions = {
1919
@Injectable()
2020
export 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();

tools/build.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { rollup } = require('rollup');
22
const { spawn } = require('child_process');
33
const { Observable, from, forkJoin } = require('rxjs');
44
const { switchMap, switchMapTo, tap } = require('rxjs/operators');
5-
const { copy, readFileSync, writeFile, statSync } = require('fs-extra');
5+
const { copy, readFileSync, writeFile, writeFileSync, statSync } = require('fs-extra');
66
const { prettySize } = require('pretty-size');
77
const gzipSize = require('gzip-size');
88
const resolve = require('rollup-plugin-node-resolve');
@@ -282,6 +282,11 @@ function copySchematicFiles() {
282282
]);
283283
}
284284

285+
function replaceDynamicImportsForUMD() {
286+
writeFileSync('./dist/packages-dist/bundles/performance.umd.js', readFileSync('./dist/packages-dist/bundles/performance.umd.js', 'utf8').replace("rxjs.from(import('firebase/performance'))", "rxjs.empty()"));
287+
writeFileSync('./dist/packages-dist/bundles/messaging.umd.js', readFileSync('./dist/packages-dist/bundles/messaging.umd.js', 'utf8').replace("rxjs.from(import('firebase/messaging'))", "rxjs.empty()"));
288+
}
289+
285290
function measure(module) {
286291
const path = `${process.cwd()}/dist/packages-dist/bundles/${module}.umd.js`;
287292
const file = readFileSync(path);
@@ -376,6 +381,7 @@ function buildLibrary(globals) {
376381
switchMap(() => replaceVersionsObservable('firebase-node', VERSIONS)),
377382
switchMap(() => from(createTestUmd(globals))),
378383
tap(() => {
384+
replaceDynamicImportsForUMD();
379385
const coreStats = measure('core');
380386
const authStats = measure('auth');
381387
const authGuardStats = measure('auth-guard');

0 commit comments

Comments
 (0)