Skip to content

Commit

Permalink
Make perf lazy and fix both perf & messaging on Node
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed May 30, 2019
1 parent bc2a669 commit 9b870a9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 46 deletions.
17 changes: 8 additions & 9 deletions src/messaging/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ export class AngularFireMessaging {
zone: NgZone
) {

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

// @ts-ignore
const requireMessaging = from(import('firebase/messaging'));
this.messaging = requireMessaging.pipe(
map(() => _firebaseAppFactory(options, nameOrConfig)),
map(app => app.messaging()),
runOutsideAngular(zone)
);

this.messaging = requireMessaging.pipe(
map(() => _firebaseAppFactory(options, nameOrConfig)),
map(app => app.messaging()),
runOutsideAngular(zone)
);
if (!isPlatformServer(platformId)) {

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

} else {

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

}
Expand Down
2 changes: 0 additions & 2 deletions src/performance/performance.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { NgModule } from '@angular/core';
import { AngularFirePerformance } from './performance';

import 'firebase/performance';

@NgModule({
providers: [ AngularFirePerformance ]
})
Expand Down
81 changes: 47 additions & 34 deletions src/performance/performance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, NgZone, ApplicationRef, InjectionToken, Inject, Optional } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { first, tap } from 'rxjs/operators';
import { Observable, Subscription, from } from 'rxjs';
import { first, tap, map, shareReplay, switchMap } from 'rxjs/operators';
import { performance } from 'firebase/app';
import { FirebaseApp } from '@angular/fire';

Expand All @@ -19,7 +19,7 @@ export type TraceOptions = {
@Injectable()
export class AngularFirePerformance {

performance: performance.Performance;
performance: Observable<performance.Performance>;

constructor(
app: FirebaseApp,
Expand All @@ -30,11 +30,19 @@ export class AngularFirePerformance {
private zone: NgZone
) {

this.performance = zone.runOutsideAngular(() => app.performance());
// @ts-ignore zapping in the UMD in the build script
const requirePerformance = from(import('firebase/performance'));

this.performance = requirePerformance.pipe(
// SEMVER while < 6 need to type, drop next major
map(() => zone.runOutsideAngular(() => <performance.Performance>app.performance())),
tap(performance => {
if (instrumentationEnabled == false) { performance.instrumentationEnabled = false }
if (dataCollectionEnabled == false) { performance.dataCollectionEnabled = false }
}),
shareReplay(1)
);

if (instrumentationEnabled == false) { this.performance.instrumentationEnabled = false }
if (dataCollectionEnabled == false) { this.performance.dataCollectionEnabled = false }

if (automaticallyTraceCoreNgMetrics != false) {

// TODO determine more built in metrics
Expand All @@ -47,33 +55,38 @@ export class AngularFirePerformance {

}

trace$ = (name:string, options?: TraceOptions) => new Observable<void>(emitter =>
this.zone.runOutsideAngular(() => {
const trace = this.performance.trace(name);
options && options.metrics && Object.keys(options.metrics).forEach(metric => {
trace.putMetric(metric, options!.metrics![metric])
});
options && options.attributes && Object.keys(options.attributes).forEach(attribute => {
trace.putAttribute(attribute, options!.attributes![attribute])
});
const attributeSubscriptions = options && options.attribute$ ? Object.keys(options.attribute$).map(attribute =>
options!.attribute$![attribute].subscribe(next => trace.putAttribute(attribute, next))
) : [];
const metricSubscriptions = options && options.metric$ ? Object.keys(options.metric$).map(metric =>
options!.metric$![metric].subscribe(next => trace.putMetric(metric, next))
) : [];
const incrementOnSubscriptions = options && options.incrementMetric$ ? Object.keys(options.incrementMetric$).map(metric =>
options!.incrementMetric$![metric].subscribe(next => trace.incrementMetric(metric, next || undefined))
) : [];
emitter.next(trace.start());
return { unsubscribe: () => {
trace.stop();
metricSubscriptions.forEach(m => m.unsubscribe());
incrementOnSubscriptions.forEach(m => m.unsubscribe());
attributeSubscriptions.forEach(m => m.unsubscribe());
}};
})
);
trace$ = (name:string, options?: TraceOptions) =>
this.performance.pipe(
switchMap(performance =>
new Observable<void>(emitter =>
this.zone.runOutsideAngular(() => {
const trace = performance.trace(name);
options && options.metrics && Object.keys(options.metrics).forEach(metric => {
trace.putMetric(metric, options!.metrics![metric])
});
options && options.attributes && Object.keys(options.attributes).forEach(attribute => {
trace.putAttribute(attribute, options!.attributes![attribute])
});
const attributeSubscriptions = options && options.attribute$ ? Object.keys(options.attribute$).map(attribute =>
options!.attribute$![attribute].subscribe(next => trace.putAttribute(attribute, next))
) : [];
const metricSubscriptions = options && options.metric$ ? Object.keys(options.metric$).map(metric =>
options!.metric$![metric].subscribe(next => trace.putMetric(metric, next))
) : [];
const incrementOnSubscriptions = options && options.incrementMetric$ ? Object.keys(options.incrementMetric$).map(metric =>
options!.incrementMetric$![metric].subscribe(next => trace.incrementMetric(metric, next || undefined))
) : [];
emitter.next(trace.start());
return { unsubscribe: () => {
trace.stop();
metricSubscriptions.forEach(m => m.unsubscribe());
incrementOnSubscriptions.forEach(m => m.unsubscribe());
attributeSubscriptions.forEach(m => m.unsubscribe());
}};
})
)
)
);

traceUntil = <T=any>(name:string, test: (a:T) => boolean, options?: TraceOptions & { orComplete?: boolean }) => (source$: Observable<T>) => new Observable<T>(subscriber => {
const traceSubscription = this.trace$(name, options).subscribe();
Expand Down
8 changes: 7 additions & 1 deletion tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { rollup } = require('rollup');
const { spawn } = require('child_process');
const { Observable, from, forkJoin } = require('rxjs');
const { switchMap, switchMapTo, tap } = require('rxjs/operators');
const { copy, readFileSync, writeFile, statSync } = require('fs-extra');
const { copy, readFileSync, writeFile, writeFileSync, statSync } = require('fs-extra');
const { prettySize } = require('pretty-size');
const gzipSize = require('gzip-size');
const resolve = require('rollup-plugin-node-resolve');
Expand Down Expand Up @@ -282,6 +282,11 @@ function copySchematicFiles() {
]);
}

function replaceDynamicImportsForUMD() {
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()"));
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()"));
}

function measure(module) {
const path = `${process.cwd()}/dist/packages-dist/bundles/${module}.umd.js`;
const file = readFileSync(path);
Expand Down Expand Up @@ -376,6 +381,7 @@ function buildLibrary(globals) {
switchMap(() => replaceVersionsObservable('firebase-node', VERSIONS)),
switchMap(() => from(createTestUmd(globals))),
tap(() => {
replaceDynamicImportsForUMD();
const coreStats = measure('core');
const authStats = measure('auth');
const authGuardStats = measure('auth-guard');
Expand Down

0 comments on commit 9b870a9

Please sign in to comment.