Skip to content

Commit 3f1a6c1

Browse files
committed
chore(refactor): Refactor AngularFire module levels. Remove web workers
1 parent 011c877 commit 3f1a6c1

14 files changed

+87
-329
lines changed

src/angularfire2.spec.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import {
88
AngularFire,
99
FirebaseObjectObservable,
1010
FIREBASE_PROVIDERS,
11-
AngularFireAuth,
12-
FirebaseConfig,
1311
FirebaseApp,
14-
defaultFirebase,
1512
AngularFireDatabase,
1613
FirebaseAppConfig,
1714
AngularFireModule
@@ -21,7 +18,7 @@ import { COMMON_CONFIG, ANON_AUTH_CONFIG } from './test-config';
2118

2219
describe('angularfire', () => {
2320
let subscription:Subscription;
24-
let app: firebase.app.App;
21+
let app: FirebaseApp;
2522
let rootRef: firebase.database.Reference;
2623
let questionsRef: firebase.database.Reference;
2724
let listOfQuestionsRef: firebase.database.Reference;
@@ -30,17 +27,20 @@ describe('angularfire', () => {
3027

3128
beforeEach(() => {
3229

30+
const afmod = AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG, APP_NAME);
31+
3332
TestBed.configureTestingModule({
34-
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG, APP_NAME)]
33+
imports: [afmod]
3534
});
3635

37-
inject([FirebaseApp, AngularFire], (firebaseApp: firebase.app.App, _af: AngularFire) => {
36+
inject([FirebaseApp, AngularFire], (firebaseApp: FirebaseApp, _af: AngularFire) => {
3837
angularFire2 = _af;
3938
app = firebaseApp;
4039
rootRef = app.database().ref();
4140
questionsRef = rootRef.child('questions');
4241
listOfQuestionsRef = rootRef.child('list-of-questions');
4342
})();
43+
4444
});
4545

4646
afterEach((done) => {
@@ -51,16 +51,20 @@ describe('angularfire', () => {
5151
app.delete().then(done, done.fail);
5252
});
5353

54+
it('should inject a Firebase App',
55+
inject([FirebaseApp], (app: FirebaseApp) => {
56+
expect(app).toBeDefined();
57+
}));
5458

5559
it('should be injectable via FIREBASE_PROVIDERS', () => {
5660
expect(angularFire2 instanceof AngularFire).toBe(true);
5761
});
5862

59-
describe('.auth', () => {
60-
it('should be an instance of AuthService', inject([AngularFire], (af:AngularFire) => {
61-
expect(af.auth instanceof AngularFireAuth).toBe(true);
62-
}));
63-
});
63+
// describe('.auth', () => {
64+
// it('should be an instance of AuthService', inject([AngularFire], (af:AngularFire) => {
65+
// expect(af.auth instanceof AngularFireAuth).toBe(true);
66+
// }));
67+
// });
6468

6569

6670
describe('.database', () => {
@@ -77,11 +81,4 @@ describe('angularfire', () => {
7781
expect(app.name).toBe(APP_NAME);
7882
})
7983
});
80-
81-
describe('defaultFirebase', () => {
82-
it('should create an array of providers', () => {
83-
const providers = defaultFirebase(COMMON_CONFIG);
84-
expect(providers.length).toBe(2);
85-
});
86-
});
8784
});

src/angularfire2.ts

Lines changed: 56 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,16 @@
11
import * as firebase from 'firebase/app';
22
import * as utils from './utils';
3-
import { FirebaseAppConfig } from './interfaces';
4-
import { AuthConfiguration } from './auth';
5-
import {
6-
FirebaseListFactoryOpts,
7-
FirebaseObjectFactoryOpts,
8-
} from './interfaces';
9-
import {
10-
FirebaseConfig,
11-
FirebaseApp,
12-
WindowLocation,
13-
FirebaseUserConfig,
14-
FirebaseAuthConfig,
15-
FirebaseAppName
16-
} from './tokens';
17-
import {
18-
APP_INITIALIZER,
19-
Inject,
20-
Injectable,
21-
OpaqueToken,
22-
NgModule,
23-
ModuleWithProviders
24-
} from '@angular/core';
25-
import {
26-
FirebaseSdkAuthBackend,
27-
AngularFireAuth,
28-
firebaseAuthConfig,
29-
AuthBackend,
30-
AuthMethods,
31-
AuthProviders,
32-
FirebaseAuthState
33-
} from './auth/index';
34-
import {
35-
FirebaseListObservable,
36-
FirebaseObjectObservable,
37-
FirebaseListFactory,
38-
FirebaseObjectFactory,
39-
AngularFireDatabase
40-
} from './database/index';
3+
import { FirebaseListFactoryOpts, FirebaseObjectFactoryOpts, FirebaseAppConfig } from './interfaces';
4+
import { FirebaseAppName } from './tokens';
5+
import { Injectable, OpaqueToken, NgModule, ModuleWithProviders } from '@angular/core';
6+
import { FirebaseListObservable, FirebaseObjectObservable, FirebaseListFactory, FirebaseObjectFactory, AngularFireDatabase } from './database/index';
417

428
@Injectable()
439
export class AngularFire {
44-
constructor(
45-
@Inject(FirebaseConfig) private firebaseConfig:FirebaseAppConfig,
46-
public auth: AngularFireAuth,
47-
public database: AngularFireDatabase) {}
10+
constructor(public database: AngularFireDatabase) {}
4811
}
4912

50-
export function _getFirebase(config: FirebaseAppConfig, appName?: string): firebase.app.App {
13+
export function _getFirebase(config: FirebaseAppConfig, appName?: string): FirebaseApp {
5114
try {
5215
if (appName) {
5316
return firebase.initializeApp(config, appName);
@@ -60,86 +23,75 @@ export function _getFirebase(config: FirebaseAppConfig, appName?: string): fireb
6023
}
6124
}
6225

63-
export function _getWindowLocation(){
64-
return window.location;
26+
export const FirebaseAppConfigToken = new OpaqueToken('FirebaseAppConfigToken');
27+
28+
@Injectable()
29+
export class FirebaseApp {
30+
name: string;
31+
options: {};
32+
auth: () => firebase.auth.Auth;
33+
database: () => firebase.database.Database;
34+
messaging: () => firebase.messaging.Messaging;
35+
storage: () => firebase.storage.Storage;
36+
delete: () => firebase.Promise<any>;
6537
}
6638

67-
export function _getAuthBackend(app: firebase.app.App): FirebaseSdkAuthBackend {
68-
return new FirebaseSdkAuthBackend(app);
39+
export function _getAngularFire(db: AngularFireDatabase) {
40+
return new AngularFire(db);
6941
}
7042

71-
export function _getDefaultFirebase(config){
72-
// remove a trailing slash from the Database URL if it exists
73-
config.databaseURL = utils.stripTrailingSlash(config.databaseURL);
74-
return config;
43+
export function _getAngularFireDatabase(app: FirebaseApp) {
44+
return new AngularFireDatabase(app);
7545
}
7646

77-
export const COMMON_PROVIDERS: any[] = [
78-
{
79-
provide: FirebaseApp,
80-
useFactory: _getFirebase,
81-
deps: [FirebaseConfig, FirebaseAppName]
82-
},
83-
AngularFireAuth,
84-
AngularFire,
85-
AngularFireDatabase
86-
];
47+
export const FirebaseAppProvider = {
48+
provide: FirebaseApp,
49+
useFactory: _getFirebase,
50+
deps: [ FirebaseAppConfigToken, FirebaseAppName ]
51+
};
52+
53+
export const AngularFireDatabaseProvider = {
54+
provide: AngularFireDatabase,
55+
useFactory: _getAngularFireDatabase,
56+
deps: [ FirebaseApp ]
57+
};
58+
59+
export const AngularFireProvider = {
60+
provide: AngularFire,
61+
useFactory: _getAngularFire,
62+
deps: [ AngularFireDatabase ]
63+
};
8764

8865
export const FIREBASE_PROVIDERS:any[] = [
89-
COMMON_PROVIDERS,
90-
{
91-
provide: AuthBackend,
92-
useFactory: _getAuthBackend,
93-
deps: [FirebaseApp]
94-
},
95-
{
96-
provide: WindowLocation,
97-
useFactory: _getWindowLocation
98-
},
66+
FirebaseAppProvider,
67+
AngularFireDatabaseProvider,
68+
AngularFireProvider
9969
];
10070

101-
/**
102-
* Used to define the default Firebase root location to be
103-
* used throughout an application.
104-
*/
105-
export const defaultFirebase = (config: FirebaseAppConfig): any => {
106-
return [
107-
{ provide: FirebaseUserConfig, useValue: config },
108-
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] }
109-
]
110-
};
71+
export {
72+
AngularFireDatabase,
73+
FirebaseListObservable,
74+
FirebaseObjectObservable,
75+
FirebaseListFactory,
76+
FirebaseObjectFactory
77+
}
78+
79+
export { FirebaseConfig, FirebaseAuthConfig, FirebaseRef, FirebaseUrl, FirebaseUserConfig } from './tokens';
80+
export { FirebaseAppConfig } from './interfaces';
11181

11282
@NgModule({
113-
providers: FIREBASE_PROVIDERS
83+
providers: [FIREBASE_PROVIDERS]
11484
})
11585
export class AngularFireModule {
116-
static initializeApp(config: FirebaseAppConfig, authConfig?: AuthConfiguration, appName?: string): ModuleWithProviders {
86+
static initializeApp(config, authConfig?, appName?): ModuleWithProviders {
11787
return {
11888
ngModule: AngularFireModule,
11989
providers: [
120-
{ provide: FirebaseUserConfig, useValue: config },
121-
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] },
122-
{ provide: FirebaseAuthConfig, useValue: authConfig },
123-
{ provide: FirebaseAppName, useValue: appName }
90+
{ provide: FirebaseAppConfigToken, useValue: config },
91+
{ provide: FirebaseAppName, useValue: appName },
92+
FirebaseAppProvider,
12493
]
12594
}
12695
}
12796
}
12897

129-
export {
130-
AngularFireAuth,
131-
AngularFireDatabase,
132-
FirebaseListObservable,
133-
FirebaseObjectObservable,
134-
FirebaseListFactory,
135-
FirebaseObjectFactory,
136-
firebaseAuthConfig,
137-
FirebaseAuthState,
138-
AuthMethods,
139-
AuthProviders,
140-
WindowLocation
141-
}
142-
143-
export { FirebaseConfig, FirebaseApp, FirebaseAuthConfig, FirebaseRef, FirebaseUrl, FirebaseUserConfig } from './tokens';
144-
export { FirebaseAppConfig } from './interfaces';
145-

src/auth/auth.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import {
1010
import { _do } from 'rxjs/operator/do';
1111

1212
import {
13-
defaultFirebase,
1413
FIREBASE_PROVIDERS,
1514
FirebaseApp,
1615
FirebaseAppConfig,
1716
FirebaseAuthState,
18-
FirebaseConfig,
17+
FirebaseAppConfigToken,
1918
AngularFireAuth,
2019
AuthMethods,
2120
firebaseAuthConfig,
@@ -144,7 +143,7 @@ describe('FirebaseAuth', () => {
144143
(<any>app).auth = () => authSpy;
145144
return app;
146145
},
147-
deps: [FirebaseConfig]
146+
deps: [FirebaseAppConfigToken]
148147
},
149148
{
150149
provide: WindowLocation,

src/auth/firebase_sdk_auth_backend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as firebase from 'firebase/app';
22
import { Injectable, Inject } from '@angular/core';
33
import { Observable } from 'rxjs/Observable';
44
import { Observer } from 'rxjs/Observer';
5-
import { FirebaseApp } from '../tokens';
5+
import { FirebaseApp } from '../angularfire2';
66
import { ZoneScheduler } from '../utils';
77
import {
88
authDataToAuthState,
@@ -34,7 +34,7 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
3434
* https://github.com/angular/angular/issues/12631
3535
* https://github.com/angular/angularfire2/issues/653
3636
**/
37-
constructor(@Inject(FirebaseApp) _fbApp: any) {
37+
constructor(private _fbApp: FirebaseApp) {
3838
super();
3939
this._fbAuth = _fbApp.auth();
4040
}

src/database/database.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
import * as firebase from 'firebase/app';
22
import 'firebase/database';
33
import { Inject, Injectable } from '@angular/core';
4-
import { FirebaseApp, FirebaseConfig } from '../tokens';
5-
import { FirebaseAppConfig } from '../angularfire2';
4+
import { FirebaseAppConfigToken, FirebaseAppConfig, FirebaseApp } from '../angularfire2';
65
import { FirebaseListFactory } from './index';
76
import { FirebaseListFactoryOpts, FirebaseObjectFactoryOpts } from '../interfaces';
87
import * as utils from '../utils';
98
import {
109
FirebaseListObservable,
1110
FirebaseObjectObservable,
12-
FirebaseObjectFactory
11+
FirebaseObjectFactory,
1312
} from './index';
1413

1514
@Injectable()
1615
export class AngularFireDatabase {
17-
constructor(@Inject(FirebaseConfig) private fbConfig:FirebaseAppConfig,
18-
@Inject(FirebaseApp) private fbApp:any) {}
16+
constructor(private fbApp: FirebaseApp) {}
1917
list (urlOrRef:string | firebase.database.Reference, opts?:FirebaseListFactoryOpts):FirebaseListObservable<any[]> {
2018
return utils.checkForUrlOrFirebaseRef(urlOrRef, {
21-
isUrl: () => FirebaseListFactory(this.fbApp.database().refFromURL(getAbsUrl(this.fbConfig, <string>urlOrRef)), opts),
19+
isUrl: () => FirebaseListFactory(this.fbApp.database().ref(<string>urlOrRef), opts),
2220
isRef: () => FirebaseListFactory(<firebase.database.Reference>urlOrRef)
2321
});
2422
}
2523
object(urlOrRef: string | firebase.database.Reference, opts?:FirebaseObjectFactoryOpts):FirebaseObjectObservable<any> {
2624
return utils.checkForUrlOrFirebaseRef(urlOrRef, {
27-
isUrl: () => FirebaseObjectFactory(this.fbApp.database().refFromURL(getAbsUrl(this.fbConfig, <string>urlOrRef)), opts),
25+
isUrl: () => FirebaseObjectFactory(this.fbApp.database().ref(<string>urlOrRef), opts),
2826
isRef: () => FirebaseObjectFactory(urlOrRef)
2927
});
3028
}

src/database/firebase_list_factory.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from './index';
1111
import {
1212
FIREBASE_PROVIDERS,
13-
defaultFirebase,
1413
FirebaseApp,
1514
FirebaseAppConfig,
1615
AngularFire,

src/database/firebase_list_observable.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as firebase from 'firebase/app';
55
import { unwrapMapFn } from '../utils';
66
import {
77
FIREBASE_PROVIDERS,
8-
defaultFirebase,
98
FirebaseApp,
109
FirebaseAppConfig,
1110
AngularFire,

src/database/firebase_object_factory.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from '@angular/core/testing';
88
import {
99
FIREBASE_PROVIDERS,
10-
defaultFirebase,
1110
FirebaseApp,
1211
FirebaseAppConfig,
1312
AngularFire,

src/database/firebase_object_observable.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
} from '@angular/core/testing';
55
import {
66
FIREBASE_PROVIDERS,
7-
defaultFirebase,
87
FirebaseApp,
98
FirebaseAppConfig,
109
AngularFire,

0 commit comments

Comments
 (0)