Skip to content

Commit

Permalink
First.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Apr 26, 2019
1 parent d34bf2f commit 755ab85
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 0 deletions.
Binary file added firebase-performance-0.0.3.tgz
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@angular/core": ">=6.0.0 <8",
"@angular/platform-browser": ">=6.0.0 <8",
"@angular/platform-browser-dynamic": ">=6.0.0 <8",
"@firebase/performance": "file:firebase-performance-0.0.3.tgz",
"firebase": "^5.5.0",
"rxjs": "^6.0.0",
"ws": "^3.3.2",
Expand Down
3 changes: 3 additions & 0 deletions src/core/firebase.app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { InjectionToken, NgModule, Optional } from '@angular/core';
import { app, auth, database, firestore, functions, messaging, storage } from 'firebase/app';
import { PerformanceController } from '@firebase/performance/dist/src/controllers/perf';

// @ts-ignore (https://github.com/firebase/firebase-js-sdk/pull/1206)
import firebase from 'firebase/app'; // once fixed can pull in as "default as firebase" above

Expand Down Expand Up @@ -28,6 +30,7 @@ export class FirebaseApp implements app.App {
// automaticDataCollectionEnabled is now private? _automaticDataCollectionEnabled?
// automaticDataCollectionEnabled: true,
messaging: () => FirebaseMessaging;
performance: () => PerformanceController;
storage: (storageBucket?: string) => FirebaseStorage;
delete: () => Promise<void>;
firestore: () => FirebaseFirestore;
Expand Down
1 change: 1 addition & 0 deletions src/performance/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './performance.spec';
1 change: 1 addition & 0 deletions src/performance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public_api';
31 changes: 31 additions & 0 deletions src/performance/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@angular/fire/performance",
"version": "ANGULARFIRE2_VERSION",
"description": "The performance monitoring module",
"main": "../bundles/performance.umd.js",
"module": "index.js",
"es2015": "./es2015/index.js",
"keywords": [
"angular",
"firebase",
"rxjs"
],
"repository": {
"type": "git",
"url": "git+https://github.com/angular/angularfire2.git"
},
"author": "angular,firebase",
"license": "MIT",
"peerDependencies": {
"@angular/fire": "ANGULARFIRE2_VERSION",
"@angular/common": "ANGULAR_VERSION",
"@angular/core": "ANGULAR_VERSION",
"@angular/platform-browser": "ANGULAR_VERSION",
"@angular/platform-browser-dynamic": "ANGULAR_VERSION",
"@firebase/app": "FIREBASE_APP_VERSION",
"@firebase/messaging": "FIREBASE_MESSAGING_VERSION",
"rxjs": "RXJS_VERSION",
"zone.js": "ZONEJS_VERSION"
},
"typings": "index.d.ts"
}
9 changes: 9 additions & 0 deletions src/performance/performance.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NgModule } from '@angular/core';
import { AngularFirePerformance } from './performance';

import 'firebase/perf';

@NgModule({
providers: [ AngularFirePerformance ]
})
export class AngularFirePerformanceModule { }
1 change: 1 addition & 0 deletions src/performance/performance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
99 changes: 99 additions & 0 deletions src/performance/performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Injectable, Inject, Optional, NgZone, ApplicationRef } from '@angular/core';
import { Observable } from 'rxjs';
import { filter, tap, take } from 'rxjs/operators';
import { FirebaseOptions, FirebaseAppConfig } from '@angular/fire';
import { FirebaseOptionsToken, FirebaseNameOrConfigToken, _firebaseAppFactory } from '@angular/fire';
import { PerformanceController } from '@firebase/performance/dist/src/controllers/perf';

export type TraceOptions = {
metrics: {[key:string]: number},
attributes?:{[key:string]:string},
attribute$?:{[key:string]:Observable<string>},
incrementMetric$:{[key:string]: Observable<number|void|null|undefined>},
metric$?:{[key:string]: Observable<number>}
};

@Injectable()
export class AngularFirePerformance {

performance: PerformanceController;

constructor(
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Optional() @Inject(FirebaseNameOrConfigToken) nameOrConfig:string|FirebaseAppConfig|null|undefined,
appRef: ApplicationRef,
private zone: NgZone
) {

this.performance = zone.runOutsideAngular(() => {
const app = _firebaseAppFactory(options, nameOrConfig);
return app.performance();
});

// TODO detirmine more built in metrics
appRef.isStable.pipe(
this.traceComplete('isStable'),
filter(it => it),
take(1)
).subscribe();

}

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());
}};
})
);

traceUntil = <T=any>(name:string, test: (a:T) => boolean, options?: TraceOptions) => (source$: Observable<T>) => {
const traceSubscription = this.trace$(name, options).subscribe();
return source$.pipe(
tap(a => { if (test(a)) { traceSubscription.unsubscribe() }})
)
};

traceComplete = <T=any>(name:string, options?: TraceOptions) => (source$: Observable<T>) => {
const traceSubscription = this.trace$(name, options).subscribe();
return source$.pipe(
tap(
() => {},
() => {},
() => traceSubscription.unsubscribe()
)
)
};

trace = <T=any>(name:string, options?: TraceOptions) => (source$: Observable<T>) => {
const traceSubscription = this.trace$(name, options).subscribe();
return source$.pipe(
tap(
() => traceSubscription.unsubscribe(),
() => {},
() => traceSubscription.unsubscribe()
)
)
};

}
2 changes: 2 additions & 0 deletions src/performance/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './performance';
export * from './performance.module';
8 changes: 8 additions & 0 deletions src/performance/test-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export const COMMON_CONFIG = {
apiKey: "AIzaSyBVSy3YpkVGiKXbbxeK0qBnu3-MNZ9UIjA",
authDomain: "angularfire2-test.firebaseapp.com",
databaseURL: "https://angularfire2-test.firebaseio.com",
storageBucket: "angularfire2-test.appspot.com",
messagingSenderId: "920323787688"
};
33 changes: 33 additions & 0 deletions src/performance/tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"baseUrl": ".",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "es2015",
"target": "es2015",
"noImplicitAny": false,
"outDir": "../../dist/packages-dist/performance/es2015",
"rootDir": ".",
"sourceMap": true,
"inlineSources": true,
"declaration": false,
"removeComments": true,
"strictNullChecks": true,
"lib": ["es2015", "dom", "es2015.promise", "es2015.collection", "es2015.iterable"],
"skipLibCheck": true,
"moduleResolution": "node",
"paths": {
"@angular/fire": ["../../dist/packages-dist"]
}
},
"files": [
"index.ts",
"../../node_modules/zone.js/dist/zone.js.d.ts"
],
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableSummariesForJit": false
}
}

19 changes: 19 additions & 0 deletions src/performance/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig-build.json",
"compilerOptions": {
"target": "es5",
"outDir": "../../dist/packages-dist/performance",
"declaration": true
},
"files": [
"public_api.ts",
"../../node_modules/zone.js/dist/zone.js.d.ts"
],
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableSummariesForJit": false,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/fire/performance"
}
}
13 changes: 13 additions & 0 deletions src/performance/tsconfig-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./tsconfig-esm.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@angular/fire": ["../../dist/packages-dist"]
}
},
"files": [
"index.spec.ts",
"../../node_modules/zone.js/dist/zone.js.d.ts"
]
}
11 changes: 11 additions & 0 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const GLOBALS = {
'firebase/messaging': 'firebase',
'firebase/firestore': 'firebase',
'firebase/functions': 'firebase',
'firebase/perf': 'firebase',
'firebase/storage': 'firebase',
'@angular/fire': 'angularfire2',
'@angular/fire/auth': 'angularfire2.auth',
Expand All @@ -32,6 +33,7 @@ const GLOBALS = {
'@angular/fire/functions': 'angularfire2.functions',
'@angular/fire/storage': 'angularfire2.storage',
'@angular/fire/messaging': 'angularfire2.messaging',
'@angular/fire/performance': 'angularfire2.performance'
};

// Map of dependency versions across all packages
Expand All @@ -57,6 +59,7 @@ const MODULE_NAMES = {
functions: 'angularfire2.functions',
storage: 'angularfire2.storage',
messaging: 'angularfire2.messaging',
performance: 'angularfire2.performance'
};

const ENTRIES = {
Expand All @@ -68,6 +71,7 @@ const ENTRIES = {
functions: `${process.cwd()}/dist/packages-dist/functions/index.js`,
storage: `${process.cwd()}/dist/packages-dist/storage/index.js`,
messaging: `${process.cwd()}/dist/packages-dist/messaging/index.js`,
performance: `${process.cwd()}/dist/packages-dist/performance/index.js`
};

const SRC_PKG_PATHS = {
Expand All @@ -80,6 +84,7 @@ const SRC_PKG_PATHS = {
functions: `${process.cwd()}/src/functions/package.json`,
storage: `${process.cwd()}/src/storage/package.json`,
messaging: `${process.cwd()}/src/messaging/package.json`,
performance: `${process.cwd()}/src/performance/package.json`
};

const DEST_PKG_PATHS = {
Expand All @@ -92,6 +97,7 @@ const DEST_PKG_PATHS = {
functions: `${process.cwd()}/dist/packages-dist/functions/package.json`,
storage: `${process.cwd()}/dist/packages-dist/storage/package.json`,
messaging: `${process.cwd()}/dist/packages-dist/messaging/package.json`,
performance: `${process.cwd()}/dist/packages-dist/performance/package.json`
};

// Constants for running typescript commands
Expand Down Expand Up @@ -262,6 +268,7 @@ function getVersions() {
getDestPackageFile('functions'),
getDestPackageFile('storage'),
getDestPackageFile('messaging'),
getDestPackageFile('performance'),
getDestPackageFile('database-deprecated')
];
return paths
Expand Down Expand Up @@ -302,6 +309,7 @@ function buildModules(globals) {
const functions$ = buildModule('functions', globals);
const storage$ = buildModule('storage', globals);
const messaging$ = buildModule('messaging', globals);
const performance$ = buildModule('performance', globals);
const dbdep$ = buildModule('database-deprecated', globals);
return forkJoin(core$, from(copyRootTest())).pipe(
switchMapTo(auth$),
Expand All @@ -310,6 +318,7 @@ function buildModules(globals) {
switchMapTo(functions$),
switchMapTo(storage$),
switchMapTo(messaging$),
switchMapTo(performance$),
switchMapTo(dbdep$)
);
}
Expand All @@ -331,6 +340,7 @@ function buildLibrary(globals) {
const functionsStats = measure('functions');
const storageStats = measure('storage');
const messagingStats = measure('messaging');
const performanceStats = measure('performance');
const dbdepStats = measure('database-deprecated');
console.log(`
core.umd.js - ${coreStats.size}, ${coreStats.gzip}
Expand All @@ -340,6 +350,7 @@ function buildLibrary(globals) {
functions.umd.js - ${functionsStats.size}, ${functionsStats.gzip}
storage.umd.js - ${storageStats.size}, ${storageStats.gzip}
messaging.umd.js - ${messagingStats.size}, ${messagingStats.gzip}
performance.umd.js - ${performanceStats.size}, ${performanceStats.gzip}
database-deprecated.umd.js - ${dbdepStats.size}, ${dbdepStats.gzip}
`);
verifyVersions();
Expand Down

1 comment on commit 755ab85

@jeffbcross
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.