Skip to content

Commit 1233a1c

Browse files
committed
Firestore/lite
1 parent d72623d commit 1233a1c

File tree

12 files changed

+128
-19
lines changed

12 files changed

+128
-19
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ publish.sh
2525

2626
# auto-gen files
2727
src/compat/**/base.ts
28-
src/**/rxfire.ts
28+
src/**/rxfire.ts

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"inquirer-autocomplete-prompt": "^1.0.1",
6464
"lodash.isequal": "^4.5.0",
6565
"open": "^7.0.3",
66-
"rxfire": "6.0.0-canary.cee1afe",
66+
"rxfire": "6.0.0-canary.92c6c26",
6767
"rxjs": "~6.6.0",
6868
"semver": "^7.1.3",
6969
"tslib": "^2.1.0",

sample/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@angular/router": "^12.0.0",
2525
"firebase": "9.0.0-beta.7",
2626
"lodash.isequal": "^4.5.0",
27-
"rxfire": "6.0.0-canary.cee1afe",
27+
"rxfire": "6.0.0-canary.92c6c26",
2828
"rxjs": "~6.6.0",
2929
"tslib": "^2.1.0",
3030
"zone.js": "~0.11.4"

sample/src/app/app.component.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { ApplicationRef, Component, NgZone, Optional } from '@angular/core';
22
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
33
import { Auth, AuthInstances, authState } from '@angular/fire/auth';
4-
import { Firestore, FirestoreInstances, firestoreInstance$ } from '@angular/fire/firestore';
4+
import { Firestore, FirestoreInstances } from '@angular/fire/firestore/lite';
5+
import { firestoreInstance$ } from '@angular/fire/firestore';
56
import { debounceTime } from 'rxjs/operators';
67
import { initializeFirestore$ } from './firestore';
78

@@ -39,8 +40,8 @@ export class AppComponent {
3940
public auth: Auth, // default Firbase Auth
4041
public apps: FirebaseApps, // all initialized App instances
4142
public authInstances: AuthInstances, // all initialized Auth instances
42-
@Optional() public firestore: Firestore,
43-
@Optional() public firestoreInstances: FirestoreInstances,
43+
public firestore: Firestore,
44+
public firestoreInstances: FirestoreInstances,
4445
appRef: ApplicationRef,
4546
zone: NgZone,
4647
) {
@@ -49,7 +50,7 @@ export class AppComponent {
4950
// onAuthStateChanged(auth, it => console.log('onAuthStateChanged', it));
5051
authState(auth).subscribe(it => console.log('authState', it));
5152
appRef.isStable.pipe(debounceTime(200)).subscribe(it => console.log('isStable', it));
52-
console.log((app as any).container.providers.keys());
53+
console.log((app as any).container.providers);
5354
firestoreInstance$.subscribe(it => console.log('$', it));
5455
initializeFirestore$.subscribe(it => console.log('init', it));
5556
}

sample/src/app/app.module.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ 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 { provideFirestore } from '@angular/fire/firestore/lite';
11+
import { getFirestore } from 'firebase/firestore/lite';
1012

1113
@NgModule({
1214
declarations: [
@@ -22,6 +24,7 @@ import { environment } from '../environments/environment';
2224
return app;
2325
}),
2426
provideAuth(() => initializeAuth(getApp())),
27+
provideFirestore(() => getFirestore()),
2528
],
2629
providers: [ ],
2730
bootstrap: [AppComponent]

sample/yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -9162,10 +9162,10 @@ run-parallel@^1.1.9:
91629162
dependencies:
91639163
queue-microtask "^1.2.2"
91649164

9165-
9166-
version "6.0.0-canary.cee1afe"
9167-
resolved "https://registry.yarnpkg.com/rxfire/-/rxfire-6.0.0-canary.cee1afe.tgz#212318f2ccc034c25581f079c51f0d952164b4db"
9168-
integrity sha512-PySJryeUfnuMuYlC8U+X83PIe53G1s+OpQuzjjaMUQ3gc1z6aD4ZEWc/O3LzVQ3ywwO/X4AAO9hv4F0Q3+y79A==
9165+
9166+
version "6.0.0-canary.92c6c26"
9167+
resolved "https://registry.yarnpkg.com/rxfire/-/rxfire-6.0.0-canary.92c6c26.tgz#de888ac0ec975eb7860b4cee54aa15df90b9fec0"
9168+
integrity sha512-zqZZFTCFXqGNRIN/zciPVzylSw3drlkM3mojTZj4GALeUBtMI59/hmAEevbAxBQjNk4eiWj/4vFsPFJtzgT5qA==
91699169
dependencies:
91709170
tslib "^1.9.0 || ~2.1.0"
91719171

src/firestore/lite/lite.module.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { NgModule, Optional, NgZone, InjectionToken, ModuleWithProviders } from '@angular/core';
2+
import { FirebaseFirestore } from 'firebase/firestore/lite';
3+
import { AuthInstances } from '@angular/fire/auth';
4+
import { ɵmemoizeInstance, ɵgetDefaultInstanceOf, ɵAngularFireSchedulers } from '@angular/fire';
5+
import { Firestore, FirestoreInstances, FIRESTORE_PROVIDER_NAME } from './lite';
6+
import { FirebaseApps } from '@angular/fire/app';
7+
8+
export const PROVIDED_FIRESTORE_INSTANCES = new InjectionToken<Firestore[]>('angularfire2.firestore-lite-instances');
9+
10+
export function defaultFirestoreInstanceFactory(_: Firestore[]) {
11+
const defaultFirestore = ɵgetDefaultInstanceOf<FirebaseFirestore>(FIRESTORE_PROVIDER_NAME);
12+
return new Firestore(defaultFirestore);
13+
}
14+
15+
export function firestoreInstanceFactory(fn: () => FirebaseFirestore) {
16+
return (zone: NgZone) => {
17+
const firestore = ɵmemoizeInstance<FirebaseFirestore>(fn, zone);
18+
return new Firestore(firestore);
19+
};
20+
}
21+
22+
const FIRESTORE_INSTANCES_PROVIDER = {
23+
provide: FirestoreInstances,
24+
deps: [
25+
[new Optional(), PROVIDED_FIRESTORE_INSTANCES ],
26+
]
27+
};
28+
29+
const DEFAULT_FIRESTORE_INSTANCE_PROVIDER = {
30+
provide: Firestore,
31+
useFactory: defaultFirestoreInstanceFactory,
32+
deps: [
33+
NgZone,
34+
[new Optional(), PROVIDED_FIRESTORE_INSTANCES ],
35+
]
36+
};
37+
38+
@NgModule({
39+
providers: [
40+
DEFAULT_FIRESTORE_INSTANCE_PROVIDER,
41+
FIRESTORE_INSTANCES_PROVIDER,
42+
]
43+
})
44+
export class FirestoreModule {
45+
}
46+
47+
export function provideFirestore(fn: () => FirebaseFirestore): ModuleWithProviders<FirestoreModule> {
48+
return {
49+
ngModule: FirestoreModule,
50+
providers: [{
51+
provide: PROVIDED_FIRESTORE_INSTANCES,
52+
useFactory: firestoreInstanceFactory(fn),
53+
multi: true,
54+
deps: [
55+
NgZone,
56+
ɵAngularFireSchedulers,
57+
FirebaseApps,
58+
// Firestore+Auth work better if Auth is loaded first
59+
[new Optional(), AuthInstances ],
60+
]
61+
}]
62+
};
63+
}

src/firestore/lite/lite.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { FirebaseFirestore } from 'firebase/firestore/lite';
2+
import { ɵgetAllInstancesOf } from '@angular/fire';
3+
import { from, timer } from 'rxjs';
4+
import { concatMap, distinct } from 'rxjs/operators';
5+
6+
// see notes in core/firebase.app.module.ts for why we're building the class like this
7+
// tslint:disable-next-line:no-empty-interface
8+
export interface Firestore extends FirebaseFirestore {}
9+
10+
export class Firestore {
11+
constructor(firestore: FirebaseFirestore) {
12+
return firestore;
13+
}
14+
}
15+
16+
export const FIRESTORE_PROVIDER_NAME = 'firestore/lite';
17+
18+
// tslint:disable-next-line:no-empty-interface
19+
export interface FirestoreInstances extends Array<FirebaseFirestore> {}
20+
21+
export class FirestoreInstances {
22+
constructor() {
23+
return ɵgetAllInstancesOf<FirebaseFirestore>(FIRESTORE_PROVIDER_NAME);
24+
}
25+
}
26+
27+
export const firestoreInstance$ = timer(0, 300).pipe(
28+
concatMap(() => from(ɵgetAllInstancesOf<FirebaseFirestore>(FIRESTORE_PROVIDER_NAME))),
29+
distinct(),
30+
);

src/firestore/lite/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "../../../node_modules/ng-packagr/package.schema.json",
3+
"ngPackage": {
4+
"lib": {
5+
"umdModuleIds": {
6+
"rxfire/firestore-lite": "rxfire-firestore-lite"
7+
},
8+
"entryFile": "public_api.ts"
9+
}
10+
}
11+
}

src/firestore/lite/public_api.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { Firestore, FirestoreInstances, firestoreInstance$ } from './lite';
2+
export { FirestoreModule, provideFirestore } from './lite.module';
3+
export * from './rxfire';

tools/build.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import { dirname, join } from 'path';
66
import { keys as tsKeys } from 'ts-transformer-keys';
77
import firebase from 'firebase/compat/app';
88

9-
const yada = tsKeys<typeof import('rxfire/database')>();
10-
const yada2 = tsKeys<typeof import('rxfire/auth')>();
11-
129
// TODO infer these from the package.json
1310
const MODULES = [
1411
'core', 'app', 'compat', 'analytics', 'auth', 'database', 'firestore', 'functions',
1512
'remote-config', 'storage', 'messaging', 'performance', 'compat/analytics',
1613
'compat/auth-guard', 'compat/auth', 'compat/database', 'compat/firestore',
1714
'compat/functions', 'compat/remote-config', 'compat/storage', 'compat/messaging',
18-
'compat/performance'
15+
'compat/performance', 'firestore/lite',
1916
];
2017
const LAZY_MODULES = ['compat/analytics', 'compat/auth', 'compat/functions', 'compat/messaging', 'compat/remote-config'];
2118
const UMD_NAMES = MODULES.map(m => m === 'core' ? 'angular-fire' : `angular-fire-${m.replace('/', '-')}`);
@@ -45,6 +42,7 @@ export function ${exportName}(..._: Parameters<Before['${exportName}']>) {
4542
reexport('remote-config', 'rxfire', 'rxfire/remote-config', tsKeys<typeof import('rxfire/remote-config')>()),
4643
reexport('storage', 'rxfire', 'rxfire/storage', tsKeys<typeof import('rxfire/storage')>()),
4744
reexport('performance', 'rxfire', 'rxfire/performance', tsKeys<typeof import('rxfire/performance')>()),
45+
reexport('firestore/lite', 'rxfire', 'rxfire/firestore/lite', tsKeys<typeof import('rxfire/firestore/lite')>()),
4846
]);
4947
}
5048

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -11678,10 +11678,10 @@ [email protected]:
1167811678
resolved "https://registry.yarnpkg.com/rx/-/rx-2.3.24.tgz#14f950a4217d7e35daa71bbcbe58eff68ea4b2b7"
1167911679
integrity sha1-FPlQpCF9fjXapxu8vljv9o6ksrc=
1168011680

11681-
11682-
version "6.0.0-canary.cee1afe"
11683-
resolved "https://registry.yarnpkg.com/rxfire/-/rxfire-6.0.0-canary.cee1afe.tgz#212318f2ccc034c25581f079c51f0d952164b4db"
11684-
integrity sha512-PySJryeUfnuMuYlC8U+X83PIe53G1s+OpQuzjjaMUQ3gc1z6aD4ZEWc/O3LzVQ3ywwO/X4AAO9hv4F0Q3+y79A==
11681+
11682+
version "6.0.0-canary.92c6c26"
11683+
resolved "https://registry.yarnpkg.com/rxfire/-/rxfire-6.0.0-canary.92c6c26.tgz#de888ac0ec975eb7860b4cee54aa15df90b9fec0"
11684+
integrity sha512-zqZZFTCFXqGNRIN/zciPVzylSw3drlkM3mojTZj4GALeUBtMI59/hmAEevbAxBQjNk4eiWj/4vFsPFJtzgT5qA==
1168511685
dependencies:
1168611686
tslib "^1.9.0 || ~2.1.0"
1168711687

0 commit comments

Comments
 (0)