Skip to content

Commit 343dbde

Browse files
committed
Cleaning things up
1 parent f572bc9 commit 343dbde

35 files changed

+270
-249
lines changed

sample/src/app/app.component.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { ApplicationRef, Component, Inject, NgZone, Optional } from '@angular/core';
2-
import { FirebaseApp, Auth, FirebaseApps, AuthInstances, FunctionsInstances,
3-
Functions, FirestoreInstances, Firestore } from '@angular/fire';
4-
import { authState } from '@angular/fire/auth';
5-
import { debounceTime, distinct } from 'rxjs/operators';
6-
import { getApps } from '@firebase/app';
7-
import { Observable } from 'rxjs';
1+
import { ApplicationRef, Component, NgZone } from '@angular/core';
2+
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
3+
import { Auth, AuthInstances, authState } from '@angular/fire/auth';
4+
import { Firestore, FirestoreInstances } from '@angular/fire/firestore';
5+
import { functionInstance$ } from '@angular/fire/functions';
6+
import { debounceTime } from 'rxjs/operators';
87

98
@Component({
109
selector: 'app-root',
@@ -36,18 +35,16 @@ import { Observable } from 'rxjs';
3635
export class AppComponent {
3736
title = 'sample';
3837
constructor(
39-
public app: FirebaseApp, // default Firebase App
38+
public app: FirebaseApp, // default Firebase App
4039
public auth: Auth, // default Firbase Auth
4140
public apps: FirebaseApps, // all initialized App instances
4241
public authInstances: AuthInstances, // all initialized Auth instances
43-
public functions: Functions,
44-
public functionsInstances: FunctionsInstances,
4542
public firestore: Firestore,
4643
public firestoreInstances: FirestoreInstances,
4744
appRef: ApplicationRef,
4845
zone: NgZone,
4946
) {
50-
console.log({app, auth, apps, authInstances, functions, functionsInstances, firestore, firestoreInstances});
47+
console.log({app, auth, apps, authInstances, firestore, firestoreInstances });
5148
// onAuthStateChanged should destablize the zone
5249
// onAuthStateChanged(auth, it => console.log('onAuthStateChanged', it));
5350
authState(auth).subscribe(it => console.log('authState', it));
@@ -63,5 +60,6 @@ export class AppComponent {
6360
functions.getFunctions(app, 'asdf');
6461
}, 10000);
6562
});
63+
functionInstance$.subscribe(it => console.log('$', it));
6664
}
6765
}

sample/src/app/app.module.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { NgModule } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
3-
import { provideFirebaseApp, provideAuth, FirestoreModule } from '@angular/fire';
3+
import { provideFirebaseApp } from '@angular/fire/app';
4+
import { provideAuth } from '@angular/fire/auth';
45
import { initializeApp, getApp } from 'firebase/app';
5-
import { initializeAuth, getAuth } from '@firebase/auth';
6-
6+
import { initializeAuth } from 'firebase/auth';
77
import { AppRoutingModule } from './app-routing.module';
88
import { AppComponent } from './app.component';
99
import { environment } from '../environments/environment';
10-
import { FunctionsModule } from '@angular/fire';
10+
import { initializeFirestore } from 'firebase/firestore';
11+
import { provideFirestore } from '@angular/fire/firestore';
1112

1213
@NgModule({
1314
declarations: [
@@ -22,14 +23,8 @@ import { FunctionsModule } from '@angular/fire';
2223
app.automaticDataCollectionEnabled = false;
2324
return app;
2425
}),
25-
provideAuth(() => getAuth()),
26-
provideAuth(() => {
27-
const auth = initializeAuth(getApp('second'));
28-
auth.useDeviceLanguage();
29-
return auth;
30-
}),
31-
FunctionsModule,
32-
FirestoreModule,
26+
provideAuth(() => initializeAuth(getApp())),
27+
provideFirestore(() => initializeFirestore(getApp(), {}))
3328
],
3429
providers: [ ],
3530
bootstrap: [AppComponent]

src/analytics/analytics.module.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
import { NgModule, Optional, NgZone, InjectionToken, ModuleWithProviders } from '@angular/core';
2-
import { Analytics as FirebaseAnalytics } from 'firebase/analytics';
3-
import { ɵgetDefaultInstanceOf, ɵmemoizeInstance } from '../core';
2+
import { Analytics as FirebaseAnalytics, initializeAnalytics } from 'firebase/analytics';
3+
import { ɵgetDefaultInstanceOf, ɵmemoizeInstance, ɵAngularFireSchedulers } from '@angular/fire';
44
import { Analytics, ANALYTICS_PROVIDER_NAME, AnalyticsInstances } from './analytics';
5-
import { PROVIDED_FIREBASE_APPS } from '../app/app.module';
6-
import { ɵAngularFireSchedulers } from '../zones';
5+
import { getApp } from 'firebase/app';
6+
import { FirebaseApps } from '@angular/fire/app';
77

88
export const PROVIDED_ANALYTICS_INSTANCES = new InjectionToken<Analytics[]>('angularfire2.analytics-instances');
99

10-
export function ɵdefaultAnalyticsInstanceFactory(_: Analytics[]) {
11-
const defaultAnalytics = ɵgetDefaultInstanceOf<FirebaseAnalytics>(ANALYTICS_PROVIDER_NAME);
10+
export function defaultAnalyticsInstanceFactory(_: Analytics[]) {
11+
const defaultAnalytics = ɵgetDefaultInstanceOf<FirebaseAnalytics>(ANALYTICS_PROVIDER_NAME) || initializeAnalytics(getApp());
1212
return new Analytics(defaultAnalytics);
1313
}
1414

15-
// Hack: useFactory doesn't allow us to pass a lambda, so let's bind the arugments
16-
// Going this direction to cut down on DI token noise; also making it easier to support
17-
// multiple Firebase Apps
18-
export function ɵboundAnalyticsInstanceFactory(zone: NgZone) {
19-
const analytics = ɵmemoizeInstance<FirebaseAnalytics>(this, zone);
20-
return new Analytics(analytics);
15+
export function analyticsInstanceFactory(fn: () => FirebaseAnalytics) {
16+
return (zone: NgZone) => {
17+
const analytics = ɵmemoizeInstance<FirebaseAnalytics>(fn, zone);
18+
return new Analytics(analytics);
19+
};
2120
}
2221

23-
2422
const ANALYTICS_INSTANCES_PROVIDER = {
2523
provide: AnalyticsInstances,
2624
deps: [
@@ -30,7 +28,7 @@ const ANALYTICS_INSTANCES_PROVIDER = {
3028

3129
const DEFAULT_ANALYTICS_INSTANCE_PROVIDER = {
3230
provide: Analytics,
33-
useFactory: ɵdefaultAnalyticsInstanceFactory,
31+
useFactory: defaultAnalyticsInstanceFactory,
3432
deps: [
3533
NgZone,
3634
[new Optional(), PROVIDED_ANALYTICS_INSTANCES ],
@@ -43,20 +41,20 @@ const DEFAULT_ANALYTICS_INSTANCE_PROVIDER = {
4341
ANALYTICS_INSTANCES_PROVIDER,
4442
]
4543
})
46-
export class AngularFireAnalyticsModule {
44+
export class AnalyticsModule {
4745
}
4846

49-
export function provideAnalytics(fn: () => FirebaseAnalytics): ModuleWithProviders<AngularFireAnalyticsModule> {
47+
export function provideAnalytics(fn: () => FirebaseAnalytics): ModuleWithProviders<AnalyticsModule> {
5048
return {
51-
ngModule: AngularFireAnalyticsModule,
49+
ngModule: AnalyticsModule,
5250
providers: [{
5351
provide: PROVIDED_ANALYTICS_INSTANCES,
54-
useFactory: ɵboundAnalyticsInstanceFactory.bind(fn),
52+
useFactory: analyticsInstanceFactory(fn),
5553
multi: true,
5654
deps: [
5755
NgZone,
5856
ɵAngularFireSchedulers,
59-
[new Optional(), PROVIDED_FIREBASE_APPS ]
57+
FirebaseApps,
6058
]
6159
}]
6260
};

src/analytics/analytics.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Analytics as FirebaseAnalytics } from 'firebase/analytics';
2-
import { ɵgetAllInstancesOf } from '../core';
2+
import { ɵgetAllInstancesOf } from '@angular/fire';
3+
import { from, timer } from 'rxjs';
4+
import { concatMap, distinct, switchMap } from 'rxjs/operators';
35

46
// see notes in core/firebase.app.module.ts for why we're building the class like this
57
// tslint:disable-next-line:no-empty-interface
@@ -21,3 +23,8 @@ export class AnalyticsInstances {
2123
return ɵgetAllInstancesOf<FirebaseAnalytics>(ANALYTICS_PROVIDER_NAME);
2224
}
2325
}
26+
27+
export const analyticInstance$ = timer(0, 300).pipe(
28+
concatMap(() => from(ɵgetAllInstancesOf<FirebaseAnalytics>(ANALYTICS_PROVIDER_NAME))),
29+
distinct(),
30+
);

src/analytics/public_api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export { Analytics, provideAnalytics, AnalyticsInstances } from '@angular/fire';
1+
export { Analytics, AnalyticsInstances, analyticInstance$ } from './analytics';
2+
export { provideAnalytics, AnalyticsModule } from './analytics.module';

src/app/app.module.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import {
2-
Inject,
32
InjectionToken,
43
ModuleWithProviders,
54
NgModule,
65
NgZone,
76
Optional,
8-
PLATFORM_ID,
97
VERSION as NG_VERSION,
108
} from '@angular/core';
119
import { FirebaseApp as IFirebaseApp, getApp, registerVersion } from 'firebase/app';
1210

1311
import { FirebaseApp, FirebaseApps } from './app';
14-
import { VERSION, ɵmemoizeInstance } from '../core';
15-
import { ɵAngularFireSchedulers } from '../zones';
12+
import { VERSION, ɵmemoizeInstance, ɵAngularFireSchedulers } from '@angular/fire';
1613

17-
export function ɵdefaultFirebaseAppFactory(_: FirebaseApp[]) {
14+
export function defaultFirebaseAppFactory(_: FirebaseApp[]) {
1815
return new FirebaseApp(getApp());
1916
}
2017

@@ -28,7 +25,7 @@ export const PROVIDED_FIREBASE_APPS = new InjectionToken<Array<FirebaseApp>>('an
2825
// from the reserved URL.
2926
const DEFAULT_FIREBASE_APP_PROVIDER = {
3027
provide: FirebaseApp,
31-
useFactory: ɵdefaultFirebaseAppFactory,
28+
useFactory: defaultFirebaseAppFactory,
3229
deps: [
3330
[new Optional(), PROVIDED_FIREBASE_APPS ],
3431
],
@@ -41,12 +38,11 @@ const FIREBASE_APPS_PROVIDER = {
4138
],
4239
};
4340

44-
// Hack: useFactory doesn't allow us to pass a lambda, so let's bind the arugments
45-
// Going this direction to cut down on DI token noise; also making it easier to support
46-
// multiple Firebase Apps
47-
export function ɵboundFirebaseAppFactory(zone: NgZone) {
48-
const app = ɵmemoizeInstance<IFirebaseApp>(this, zone);
49-
return new FirebaseApp(app);
41+
export function firebaseAppFactory(fn: () => IFirebaseApp) {
42+
return (zone: NgZone) => {
43+
const app = ɵmemoizeInstance<IFirebaseApp>(fn, zone);
44+
return new FirebaseApp(app);
45+
};
5046
}
5147

5248
@NgModule({
@@ -55,7 +51,7 @@ export function ɵboundFirebaseAppFactory(zone: NgZone) {
5551
FIREBASE_APPS_PROVIDER,
5652
]
5753
})
58-
export class AngularFireModule {
54+
export class FirebaseAppModule {
5955
constructor() {
6056
registerVersion('angularfire', VERSION.full, 'core');
6157
registerVersion('angular', NG_VERSION.full);
@@ -65,12 +61,12 @@ export class AngularFireModule {
6561
// Calling initializeApp({ ... }, 'name') multiple times will add more FirebaseApps into the FIREBASE_APPS
6662
// injection scope. This allows developers to more easily work with multiple Firebase Applications. Downside
6763
// is that DI for app name and options doesn't really make sense anymore.
68-
export function provideFirebaseApp(fn: () => IFirebaseApp): ModuleWithProviders<AngularFireModule> {
64+
export function provideFirebaseApp(fn: () => IFirebaseApp): ModuleWithProviders<FirebaseAppModule> {
6965
return {
70-
ngModule: AngularFireModule,
66+
ngModule: FirebaseAppModule,
7167
providers: [{
7268
provide: PROVIDED_FIREBASE_APPS,
73-
useFactory: ɵboundFirebaseAppFactory.bind(fn),
69+
useFactory: firebaseAppFactory(fn),
7470
multi: true,
7571
deps: [ NgZone, ɵAngularFireSchedulers ],
7672
}],

src/app/app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { FirebaseApp as IFirebaseApp, getApps } from 'firebase/app';
2+
import { from, timer } from 'rxjs';
3+
import { concatMap, distinct } from 'rxjs/operators';
24

35
// Need to turn the FirebaseApp interface exported by firebase/app into a class
46
// as types don't work in Angular DI. We want developers to be able to inject FirebaseApp like so
@@ -29,3 +31,8 @@ export class FirebaseApps {
2931
return getApps();
3032
}
3133
}
34+
35+
export const firebaseApp$ = timer(0, 300).pipe(
36+
concatMap(() => from(getApps())),
37+
distinct(),
38+
);

src/app/public_api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export { FirebaseApp, provideFirebaseApp, FirebaseApps } from '@angular/fire';
1+
export { FirebaseApp, FirebaseApps, firebaseApp$ } from './app';
2+
export { provideFirebaseApp, FirebaseAppModule } from './app.module';

src/auth/auth.module.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import { NgModule, Optional, NgZone, InjectionToken, ModuleWithProviders } from '@angular/core';
2-
import { Auth as FirebaseAuth } from 'firebase/auth';
3-
import { ɵgetDefaultInstanceOf, ɵmemoizeInstance } from '../core';
2+
import { Auth as FirebaseAuth, initializeAuth } from 'firebase/auth';
3+
import { ɵgetDefaultInstanceOf, ɵmemoizeInstance, ɵAngularFireSchedulers } from '@angular/fire';
44
import { Auth, AuthInstances, AUTH_PROVIDER_NAME } from './auth';
5-
import { ɵAngularFireSchedulers } from '../zones';
6-
import { PROVIDED_FIREBASE_APPS } from '../app/app.module';
5+
import { getApp } from 'firebase/app';
6+
import { FirebaseApps } from '@angular/fire/app';
77

88
export const PROVIDED_AUTH_INSTANCES = new InjectionToken<Auth[]>('angularfire2.auth-instances');
99

10-
export function ɵdefaultAuthInstanceFactory() {
11-
const defaultAuth = ɵgetDefaultInstanceOf<FirebaseAuth>(AUTH_PROVIDER_NAME);
10+
export function defaultAuthInstanceFactory() {
11+
const defaultAuth = ɵgetDefaultInstanceOf<FirebaseAuth>(AUTH_PROVIDER_NAME) || initializeAuth(getApp());
1212
return new Auth(defaultAuth);
1313
}
1414

15-
// Hack: useFactory doesn't allow us to pass a lambda, so let's bind the arugments
16-
// Going this direction to cut down on DI token noise; also making it easier to support
17-
// multiple Firebase Apps
18-
export function ɵboundAuthInstanceFactory(zone: NgZone) {
19-
const auth = ɵmemoizeInstance<FirebaseAuth>(this, zone);
20-
return new Auth(auth);
15+
export function authInstanceFactory(fn: () => FirebaseAuth) {
16+
return (zone: NgZone) => {
17+
const auth = ɵmemoizeInstance<FirebaseAuth>(fn, zone);
18+
return new Auth(auth);
19+
};
2120
}
2221

2322
const AUTH_INSTANCES_PROVIDER = {
@@ -29,7 +28,7 @@ const AUTH_INSTANCES_PROVIDER = {
2928

3029
const DEFAULT_AUTH_INSTANCE_PROVIDER = {
3130
provide: Auth,
32-
useFactory: ɵdefaultAuthInstanceFactory,
31+
useFactory: defaultAuthInstanceFactory,
3332
deps: [
3433
NgZone,
3534
[new Optional(), PROVIDED_AUTH_INSTANCES ],
@@ -50,12 +49,12 @@ export function provideAuth(fn: () => FirebaseAuth): ModuleWithProviders<AuthMod
5049
ngModule: AuthModule,
5150
providers: [{
5251
provide: PROVIDED_AUTH_INSTANCES,
53-
useFactory: ɵboundAuthInstanceFactory.bind(fn),
52+
useFactory: authInstanceFactory(fn),
5453
multi: true,
5554
deps: [
5655
NgZone,
5756
ɵAngularFireSchedulers,
58-
[new Optional(), PROVIDED_FIREBASE_APPS ]
57+
FirebaseApps,
5958
]
6059
}]
6160
};

src/auth/auth.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Auth as FirebaseAuth } from 'firebase/auth';
2-
import { ɵgetAllInstancesOf } from '../core';
2+
import { ɵgetAllInstancesOf } from '@angular/fire';
3+
import { from, timer } from 'rxjs';
4+
import { concatMap, distinct } from 'rxjs/operators';
35

46
export const AUTH_PROVIDER_NAME = 'auth-exp';
57

@@ -21,3 +23,8 @@ export class AuthInstances {
2123
return ɵgetAllInstancesOf<FirebaseAuth>(AUTH_PROVIDER_NAME);
2224
}
2325
}
26+
27+
export const authInstance$ = timer(0, 300).pipe(
28+
concatMap(() => from(ɵgetAllInstancesOf<FirebaseAuth>(AUTH_PROVIDER_NAME))),
29+
distinct(),
30+
);

0 commit comments

Comments
 (0)