Skip to content

Commit

Permalink
feat(): Support FirebaseAppConfig, clean up injection tokens (#1627)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels authored and davideast committed May 12, 2018
1 parent 0c3b215 commit 57906bd
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 150 deletions.
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
"@angular/core": "^5.0.0",
"@angular/platform-browser": "^5.0.0",
"@angular/platform-browser-dynamic": "^5.0.0",
"@firebase/app": "^0.1.6",
"@firebase/app-types": "^0.1.1",
"@firebase/auth": "^0.3.2",
"@firebase/auth-types": "^0.1.1",
"@firebase/database": "^0.1.7",
"@firebase/database-types": "^0.1.1",
"@firebase/firestore": "^0.2.3",
"@firebase/firestore-types": "^0.1.1",
"@firebase/messaging-types": "^0.1.1",
"@firebase/storage": "^0.1.6",
"@firebase/storage-types": "^0.1.1",
"@firebase/app": "^0.2.0",
"@firebase/app-types": "^0.2.0",
"@firebase/auth": "^0.4.2",
"@firebase/auth-types": "^0.2.1",
"@firebase/database": "^0.2.2",
"@firebase/database-types": "^0.2.1",
"@firebase/firestore": "^0.4.1",
"@firebase/firestore-types": "^0.3.0",
"@firebase/messaging-types": "^0.1.3",
"@firebase/storage": "^0.1.9",
"@firebase/storage-types": "^0.1.3",
"bufferutil": "^3.0.3",
"firebase": "^4.8.2",
"firebase": "^4.13.0",
"rxjs": "^5.5.4",
"utf-8-validate": "^4.0.0",
"ws": "^3.3.2",
Expand Down
6 changes: 3 additions & 3 deletions src/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { User } from '@firebase/auth-types';
import { ReflectiveInjector, Provider } from '@angular/core';
import { Observable, Subject } from 'rxjs'
import { TestBed, inject } from '@angular/core/testing';
import { FirebaseApp, FirebaseAppConfig, AngularFireModule, FirebaseAppName } from 'angularfire2';
import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseAppNameToken } from 'angularfire2';
import { AngularFireAuth, AngularFireAuthModule } from 'angularfire2/auth';
import { COMMON_CONFIG } from './test-config';
import { take, skip } from 'rxjs/operators';
Expand Down Expand Up @@ -136,8 +136,8 @@ describe('AngularFireAuth with different app', () => {
AngularFireAuthModule
],
providers: [
{ provide: FirebaseAppName, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseAppConfig, useValue: COMMON_CONFIG }
{ provide: FirebaseAppNameToken, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseOptionsToken, useValue: COMMON_CONFIG }
]
});
inject([FirebaseApp, AngularFireAuth], (app_: FirebaseApp, _afAuth: AngularFireAuth) => {
Expand Down
11 changes: 6 additions & 5 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FirebaseAuth, User } from '@firebase/auth-types';
import { FirebaseOptions } from '@firebase/app-types';
import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
import { Injectable, Inject, Optional, NgZone, PLATFORM_ID } from '@angular/core';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
import { from } from 'rxjs/observable/from';

import { FirebaseAppConfig, FirebaseAppName, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';
import { FirebaseOptionsToken, FirebaseAppConfigToken, FirebaseAppNameToken, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';


@Injectable()
Expand All @@ -28,14 +28,15 @@ export class AngularFireAuth {
public readonly idToken: Observable<string|null>;

constructor(
@Inject(FirebaseAppConfig) config:FirebaseOptions,
@Optional() @Inject(FirebaseAppName) name:string,
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Optional() @Inject(FirebaseAppConfigToken) config:FirebaseAppConfig,
@Optional() @Inject(FirebaseAppNameToken) name:string,
@Inject(PLATFORM_ID) platformId: Object,
private zone: NgZone
) {
const scheduler = new FirebaseZoneScheduler(zone, platformId);
this.auth = zone.runOutsideAngular(() => {
const app = _firebaseAppFactory(config, name);
const app = _firebaseAppFactory(options, name, config);
return app.auth();
});

Expand Down
2 changes: 1 addition & 1 deletion src/core/angularfire2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Reference } from '@firebase/database-types';
import { TestBed, inject, withModule, async } from '@angular/core/testing';
import { ReflectiveInjector, Provider, PlatformRef, NgModule, Compiler, ApplicationRef, CompilerFactory } from '@angular/core';
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { FirebaseApp, AngularFireModule } from 'angularfire2';
import { Subscription } from 'rxjs';
import { COMMON_CONFIG } from './test-config';
import { BrowserModule } from '@angular/platform-browser';
Expand Down
7 changes: 4 additions & 3 deletions src/core/angularfire2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { queue } from 'rxjs/scheduler/queue';
import { first } from 'rxjs/operators';

import firebase from '@firebase/app';
import { FirebaseApp, FirebaseOptions } from '@firebase/app-types';
import { FirebaseApp, FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';

import {} from 'zone.js';

export const FirebaseAppName = new InjectionToken<string>('angularfire2.appName');
export const FirebaseAppConfig = new InjectionToken<FirebaseOptions>('angularfire2.config');
export const FirebaseAppNameToken = new InjectionToken<string|undefined>('angularfire2.app.name');
export const FirebaseOptionsToken = new InjectionToken<FirebaseOptions>('angularfire2.app.options');
export const FirebaseAppConfigToken = new InjectionToken<FirebaseAppConfig|undefined>('angularfire2.app.config');

// Put in database.ts when we drop database-depreciated
export const RealtimeDatabaseURL = new InjectionToken<string>('angularfire2.realtimeDatabaseURL');
Expand Down
29 changes: 18 additions & 11 deletions src/core/firebase.app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { InjectionToken, NgZone, NgModule } from '@angular/core';

import { FirebaseAppConfig, FirebaseAppName } from './angularfire2';
import { FirebaseOptionsToken, FirebaseAppNameToken, FirebaseAppConfigToken } from './angularfire2';

import firebase from '@firebase/app';
import { FirebaseApp as _FirebaseApp, FirebaseOptions } from '@firebase/app-types';
import { FirebaseApp as _FirebaseApp, FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
import { FirebaseAuth } from '@firebase/auth-types';
import { FirebaseDatabase } from '@firebase/database-types';
import { FirebaseMessaging } from '@firebase/messaging-types';
Expand All @@ -22,29 +22,36 @@ export class FirebaseApp implements _FirebaseApp {
firestore: () => FirebaseFirestore;
}

export function _firebaseAppFactory(config: FirebaseOptions, name?: string): FirebaseApp {
const appName = name || '[DEFAULT]';
const existingApp = firebase.apps.filter(app => app.name == appName)[0] as FirebaseApp;
return existingApp || firebase.initializeApp(config, appName) as FirebaseApp;
export function _firebaseAppFactory(options: FirebaseOptions, name?: string, appConfig?: FirebaseAppConfig): FirebaseApp {
const config = appConfig || {};
if (name && config.name && config.name !== name) {
console.warn('FirebaseAppNameToken and FirebaseAppConfigToken.name don\'t match, FirebaseAppNameToken takes precedence.');
}
config.name = name || config.name || '[DEFAULT]';
const existingApp = firebase.apps.filter(app => app.name === config.name)[0];
return (existingApp || firebase.initializeApp(options, config)) as FirebaseApp;
}

const FirebaseAppProvider = {
provide: FirebaseApp,
useFactory: _firebaseAppFactory,
deps: [ FirebaseAppConfig, FirebaseAppName ]
deps: [ FirebaseOptionsToken, FirebaseAppNameToken, FirebaseAppConfigToken ]
};

@NgModule({
providers: [ FirebaseAppProvider ],
})
export class AngularFireModule {
static initializeApp(config: FirebaseOptions, appName?: string) {
static initializeApp(options: FirebaseOptions, appNameOrConfig?: string | FirebaseAppConfig) {
const name = typeof appNameOrConfig === 'string' && appNameOrConfig || undefined
const config = typeof appNameOrConfig === 'object' && appNameOrConfig || undefined
return {
ngModule: AngularFireModule,
providers: [
{ provide: FirebaseAppConfig, useValue: config },
{ provide: FirebaseAppName, useValue: appName }
{ provide: FirebaseOptionsToken, useValue: options },
{ provide: FirebaseAppNameToken, useValue: name },
{ provide: FirebaseAppConfigToken, useValue: config }
]
}
}
}
}
9 changes: 5 additions & 4 deletions src/database-deprecated/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { FirebaseListFactoryOpts, FirebaseObjectFactoryOpts, PathReference } fro
import { FirebaseObjectFactory } from './firebase_object_factory';
import { FirebaseObjectObservable } from './firebase_object_observable';
import * as utils from './utils';
import { FirebaseOptions } from '@firebase/app-types';
import { FirebaseAppConfig, FirebaseAppName, RealtimeDatabaseURL, _firebaseAppFactory } from 'angularfire2';
import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
import { FirebaseAppConfigToken, FirebaseOptionsToken, FirebaseAppNameToken, RealtimeDatabaseURL, _firebaseAppFactory } from 'angularfire2';

@Injectable()
export class AngularFireDatabase {
Expand All @@ -18,8 +18,9 @@ export class AngularFireDatabase {
database: FirebaseDatabase;

constructor(
@Inject(FirebaseAppConfig) config:FirebaseOptions,
@Optional() @Inject(FirebaseAppName) name:string,
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Inject(FirebaseAppConfigToken) config:FirebaseAppConfig,
@Optional() @Inject(FirebaseAppNameToken) name:string,
@Optional() @Inject(RealtimeDatabaseURL) databaseURL:string,
zone: NgZone
) {
Expand Down
10 changes: 5 additions & 5 deletions src/database/database.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FirebaseApp, FirebaseAppConfig, AngularFireModule, FirebaseAppName } from 'angularfire2';
import { FirebaseApp, AngularFireModule, FirebaseOptionsToken, FirebaseAppNameToken } from 'angularfire2';
import { AngularFireDatabase, AngularFireDatabaseModule, RealtimeDatabaseURL } from 'angularfire2/database';
import { TestBed, inject } from '@angular/core/testing';
import { COMMON_CONFIG } from './test-config';
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('AngularFireDatabase', () => {
});

it('should accept a Firebase App in the constructor', () => {
const __db = new AngularFireDatabase(app.options, app.name, null!, {}, zone);
const __db = new AngularFireDatabase(app.options, undefined!, app.name, undefined!, {}, zone);
expect(__db instanceof AngularFireDatabase).toEqual(true);
});

Expand All @@ -69,9 +69,9 @@ describe('AngularFireDatabase w/options', () => {
AngularFireDatabaseModule
],
providers: [
{ provide: FirebaseAppName, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseAppConfig, useValue: COMMON_CONFIG },
{ provide: RealtimeDatabaseURL, useValue: FIREBASE_DB_NAME }
{ provide: FirebaseAppNameToken, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseOptionsToken, useValue: COMMON_CONFIG },
{ provide: RealtimeDatabaseURL, useValue: FIREBASE_DB_NAME }
]
});
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {
Expand Down
11 changes: 6 additions & 5 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ import { FirebaseDatabase } from '@firebase/database-types';
import { PathReference, DatabaseQuery, DatabaseReference, DatabaseSnapshot, ChildEvent, ListenEvent, QueryFn, AngularFireList, AngularFireObject } from './interfaces';
import { getRef } from './utils';
import { InjectionToken } from '@angular/core';
import { FirebaseOptions } from '@firebase/app-types';
import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
import { createListReference } from './list/create-reference';
import { createObjectReference } from './object/create-reference';
import { FirebaseAppConfig, FirebaseAppName, RealtimeDatabaseURL, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';
import { FirebaseOptionsToken, FirebaseAppConfigToken, FirebaseAppNameToken, RealtimeDatabaseURL, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';

@Injectable()
export class AngularFireDatabase {
public readonly database: FirebaseDatabase;
public readonly scheduler: FirebaseZoneScheduler;

constructor(
@Inject(FirebaseAppConfig) config:FirebaseOptions,
@Optional() @Inject(FirebaseAppName) name:string,
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Optional() @Inject(FirebaseAppConfigToken) config:FirebaseAppConfig,
@Optional() @Inject(FirebaseAppNameToken) name:string,
@Optional() @Inject(RealtimeDatabaseURL) databaseURL:string,
@Inject(PLATFORM_ID) platformId: Object,
zone: NgZone
) {
this.scheduler = new FirebaseZoneScheduler(zone, platformId);
this.database = zone.runOutsideAngular(() => {
const app = _firebaseAppFactory(config, name);
const app = _firebaseAppFactory(options, name, config);
return app.database(databaseURL || undefined);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/database/list/changes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Reference } from '@firebase/database-types';
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { FirebaseApp, AngularFireModule } from 'angularfire2';
import { AngularFireDatabase, AngularFireDatabaseModule, listChanges } from 'angularfire2/database';
import { TestBed, inject } from '@angular/core/testing';
import { COMMON_CONFIG } from '../test-config';
Expand Down
2 changes: 1 addition & 1 deletion src/database/list/snapshot-changes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Reference } from '@firebase/database-types';
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { FirebaseApp, AngularFireModule } from 'angularfire2';
import { AngularFireDatabase, AngularFireDatabaseModule, snapshotChanges, ChildEvent } from 'angularfire2/database';
import { TestBed, inject } from '@angular/core/testing';
import { COMMON_CONFIG } from '../test-config';
Expand Down
2 changes: 1 addition & 1 deletion src/database/list/state-changes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Reference } from '@firebase/database-types';
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { FirebaseApp, AngularFireModule } from 'angularfire2';
import { AngularFireDatabase, AngularFireDatabaseModule, stateChanges, ChildEvent } from 'angularfire2/database';
import { TestBed, inject } from '@angular/core/testing';
import { COMMON_CONFIG } from '../test-config';
Expand Down
2 changes: 1 addition & 1 deletion src/firestore/collection/collection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { FirebaseApp, AngularFireModule } from 'angularfire2';
import { AngularFirestore } from '../firestore';
import { AngularFirestoreModule } from '../firestore.module';
import { AngularFirestoreDocument } from '../document/document';
Expand Down
2 changes: 1 addition & 1 deletion src/firestore/document/document.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { FirebaseApp, AngularFireModule } from 'angularfire2';
import { AngularFirestore } from '../firestore';
import { AngularFirestoreModule } from '../firestore.module';
import { AngularFirestoreDocument } from '../document/document';
Expand Down
6 changes: 3 additions & 3 deletions src/firestore/firestore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FirebaseApp, FirebaseAppConfig, AngularFireModule, FirebaseAppName } from 'angularfire2';
import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseAppNameToken } from 'angularfire2';
import { AngularFirestore } from './firestore';
import { AngularFirestoreModule } from './firestore.module';
import { AngularFirestoreDocument } from './document/document';
Expand Down Expand Up @@ -105,8 +105,8 @@ describe('AngularFirestore with different app', () => {
AngularFirestoreModule
],
providers: [
{ provide: FirebaseAppName, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseAppConfig, useValue: COMMON_CONFIG }
{ provide: FirebaseAppNameToken, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseOptionsToken, useValue: COMMON_CONFIG }
]
});
inject([FirebaseApp, AngularFirestore], (app_: FirebaseApp, _afs: AngularFirestore) => {
Expand Down
11 changes: 6 additions & 5 deletions src/firestore/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { Observable, Subscriber } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
import { from } from 'rxjs/observable/from';
import { FirebaseOptions } from '@firebase/app-types';
import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
import { Injectable, Inject, Optional } from '@angular/core';

import { QueryFn, AssociatedReference } from './interfaces';
import { AngularFirestoreDocument } from './document/document';
import { AngularFirestoreCollection } from './collection/collection';

import { FirebaseAppConfig, FirebaseAppName, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';
import { FirebaseAppConfigToken, FirebaseOptionsToken, FirebaseAppNameToken, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';

/**
* The value of this token determines whether or not the firestore will have persistance enabled
Expand Down Expand Up @@ -106,15 +106,16 @@ export class AngularFirestore {
* @param app
*/
constructor(
@Inject(FirebaseAppConfig) config:FirebaseOptions,
@Optional() @Inject(FirebaseAppName) name:string,
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Optional() @Inject(FirebaseAppConfigToken) config:FirebaseAppConfig,
@Optional() @Inject(FirebaseAppNameToken) name:string,
@Optional() @Inject(EnablePersistenceToken) shouldEnablePersistence: boolean,
@Inject(PLATFORM_ID) platformId: Object,
zone: NgZone
) {
this.scheduler = new FirebaseZoneScheduler(zone, platformId);
this.firestore = zone.runOutsideAngular(() => {
const app = _firebaseAppFactory(config, name);
const app = _firebaseAppFactory(options, name, config);
return app.firestore();
});

Expand Down
6 changes: 3 additions & 3 deletions src/storage/storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Observable } from 'rxjs'
import { forkJoin } from 'rxjs/observable/forkJoin';
import { map, mergeMap, tap } from 'rxjs/operators';
import { TestBed, inject } from '@angular/core/testing';
import { FirebaseApp, FirebaseAppConfig, AngularFireModule, FirebaseAppName } from 'angularfire2';
import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseAppNameToken } from 'angularfire2';
import { AngularFireStorageModule, AngularFireStorage, AngularFireUploadTask, StorageBucket } from 'angularfire2/storage';
import { COMMON_CONFIG } from './test-config';

Expand Down Expand Up @@ -140,8 +140,8 @@ describe('AngularFireStorage w/options', () => {
AngularFireStorageModule
],
providers: [
{ provide: FirebaseAppName, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseAppConfig, useValue: COMMON_CONFIG },
{ provide: FirebaseAppNameToken, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseOptionsToken, useValue: COMMON_CONFIG },
{ provide: StorageBucket, useValue: FIREBASE_STORAGE_BUCKET }
]
});
Expand Down
11 changes: 6 additions & 5 deletions src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { FirebaseStorage, UploadMetadata } from '@firebase/storage-types';
import { createStorageRef, AngularFireStorageReference } from './ref';
import { createUploadTask, AngularFireUploadTask } from './task';
import { Observable } from 'rxjs';
import { FirebaseAppConfig, FirebaseAppName, FirebaseZoneScheduler, _firebaseAppFactory } from 'angularfire2';
import { FirebaseOptions } from '@firebase/app-types';
import { FirebaseAppConfigToken, FirebaseOptionsToken, FirebaseAppNameToken, FirebaseZoneScheduler, _firebaseAppFactory } from 'angularfire2';
import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';

export const StorageBucket = new InjectionToken<string>('angularfire2.storageBucket');

Expand All @@ -21,15 +21,16 @@ export class AngularFireStorage {
public readonly scheduler: FirebaseZoneScheduler;

constructor(
@Inject(FirebaseAppConfig) config:FirebaseOptions,
@Optional() @Inject(FirebaseAppName) name:string,
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Optional() @Inject(FirebaseAppConfigToken) config:FirebaseAppConfig,
@Optional() @Inject(FirebaseAppNameToken) name:string,
@Optional() @Inject(StorageBucket) storageBucket:string,
@Inject(PLATFORM_ID) platformId: Object,
zone: NgZone
) {
this.scheduler = new FirebaseZoneScheduler(zone, platformId);
this.storage = zone.runOutsideAngular(() => {
const app = _firebaseAppFactory(config, name);
const app = _firebaseAppFactory(options, name, config);
return app.storage(storageBucket || undefined);
});
}
Expand Down
Loading

0 comments on commit 57906bd

Please sign in to comment.