Skip to content

Commit

Permalink
Firestore/lite
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Jul 28, 2021
1 parent d72623d commit 1233a1c
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -25,4 +25,4 @@ publish.sh

# auto-gen files
src/compat/**/base.ts
src/**/rxfire.ts
src/**/rxfire.ts
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -63,7 +63,7 @@
"inquirer-autocomplete-prompt": "^1.0.1",
"lodash.isequal": "^4.5.0",
"open": "^7.0.3",
"rxfire": "6.0.0-canary.cee1afe",
"rxfire": "6.0.0-canary.92c6c26",
"rxjs": "~6.6.0",
"semver": "^7.1.3",
"tslib": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion sample/package.json
Expand Up @@ -24,7 +24,7 @@
"@angular/router": "^12.0.0",
"firebase": "9.0.0-beta.7",
"lodash.isequal": "^4.5.0",
"rxfire": "6.0.0-canary.cee1afe",
"rxfire": "6.0.0-canary.92c6c26",
"rxjs": "~6.6.0",
"tslib": "^2.1.0",
"zone.js": "~0.11.4"
Expand Down
9 changes: 5 additions & 4 deletions sample/src/app/app.component.ts
@@ -1,7 +1,8 @@
import { ApplicationRef, Component, NgZone, Optional } from '@angular/core';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
import { Auth, AuthInstances, authState } from '@angular/fire/auth';
import { Firestore, FirestoreInstances, firestoreInstance$ } from '@angular/fire/firestore';
import { Firestore, FirestoreInstances } from '@angular/fire/firestore/lite';
import { firestoreInstance$ } from '@angular/fire/firestore';
import { debounceTime } from 'rxjs/operators';
import { initializeFirestore$ } from './firestore';

Expand Down Expand Up @@ -39,8 +40,8 @@ export class AppComponent {
public auth: Auth, // default Firbase Auth
public apps: FirebaseApps, // all initialized App instances
public authInstances: AuthInstances, // all initialized Auth instances
@Optional() public firestore: Firestore,
@Optional() public firestoreInstances: FirestoreInstances,
public firestore: Firestore,
public firestoreInstances: FirestoreInstances,
appRef: ApplicationRef,
zone: NgZone,
) {
Expand All @@ -49,7 +50,7 @@ export class AppComponent {
// onAuthStateChanged(auth, it => console.log('onAuthStateChanged', it));
authState(auth).subscribe(it => console.log('authState', it));
appRef.isStable.pipe(debounceTime(200)).subscribe(it => console.log('isStable', it));
console.log((app as any).container.providers.keys());
console.log((app as any).container.providers);
firestoreInstance$.subscribe(it => console.log('$', it));
initializeFirestore$.subscribe(it => console.log('init', it));
}
Expand Down
3 changes: 3 additions & 0 deletions sample/src/app/app.module.ts
Expand Up @@ -7,6 +7,8 @@ import { initializeAuth } from 'firebase/auth';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { environment } from '../environments/environment';
import { provideFirestore } from '@angular/fire/firestore/lite';
import { getFirestore } from 'firebase/firestore/lite';

@NgModule({
declarations: [
Expand All @@ -22,6 +24,7 @@ import { environment } from '../environments/environment';
return app;
}),
provideAuth(() => initializeAuth(getApp())),
provideFirestore(() => getFirestore()),
],
providers: [ ],
bootstrap: [AppComponent]
Expand Down
8 changes: 4 additions & 4 deletions sample/yarn.lock
Expand Up @@ -9162,10 +9162,10 @@ run-parallel@^1.1.9:
dependencies:
queue-microtask "^1.2.2"

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

Expand Down
63 changes: 63 additions & 0 deletions src/firestore/lite/lite.module.ts
@@ -0,0 +1,63 @@
import { NgModule, Optional, NgZone, InjectionToken, ModuleWithProviders } from '@angular/core';
import { FirebaseFirestore } from 'firebase/firestore/lite';
import { AuthInstances } from '@angular/fire/auth';
import { ɵmemoizeInstance, ɵgetDefaultInstanceOf, ɵAngularFireSchedulers } from '@angular/fire';
import { Firestore, FirestoreInstances, FIRESTORE_PROVIDER_NAME } from './lite';
import { FirebaseApps } from '@angular/fire/app';

export const PROVIDED_FIRESTORE_INSTANCES = new InjectionToken<Firestore[]>('angularfire2.firestore-lite-instances');

export function defaultFirestoreInstanceFactory(_: Firestore[]) {
const defaultFirestore = ɵgetDefaultInstanceOf<FirebaseFirestore>(FIRESTORE_PROVIDER_NAME);
return new Firestore(defaultFirestore);
}

export function firestoreInstanceFactory(fn: () => FirebaseFirestore) {
return (zone: NgZone) => {
const firestore = ɵmemoizeInstance<FirebaseFirestore>(fn, zone);
return new Firestore(firestore);
};
}

const FIRESTORE_INSTANCES_PROVIDER = {
provide: FirestoreInstances,
deps: [
[new Optional(), PROVIDED_FIRESTORE_INSTANCES ],
]
};

const DEFAULT_FIRESTORE_INSTANCE_PROVIDER = {
provide: Firestore,
useFactory: defaultFirestoreInstanceFactory,
deps: [
NgZone,
[new Optional(), PROVIDED_FIRESTORE_INSTANCES ],
]
};

@NgModule({
providers: [
DEFAULT_FIRESTORE_INSTANCE_PROVIDER,
FIRESTORE_INSTANCES_PROVIDER,
]
})
export class FirestoreModule {
}

export function provideFirestore(fn: () => FirebaseFirestore): ModuleWithProviders<FirestoreModule> {
return {
ngModule: FirestoreModule,
providers: [{
provide: PROVIDED_FIRESTORE_INSTANCES,
useFactory: firestoreInstanceFactory(fn),
multi: true,
deps: [
NgZone,
ɵAngularFireSchedulers,
FirebaseApps,
// Firestore+Auth work better if Auth is loaded first
[new Optional(), AuthInstances ],
]
}]
};
}
30 changes: 30 additions & 0 deletions src/firestore/lite/lite.ts
@@ -0,0 +1,30 @@
import { FirebaseFirestore } from 'firebase/firestore/lite';
import { ɵgetAllInstancesOf } from '@angular/fire';
import { from, timer } from 'rxjs';
import { concatMap, distinct } from 'rxjs/operators';

// see notes in core/firebase.app.module.ts for why we're building the class like this
// tslint:disable-next-line:no-empty-interface
export interface Firestore extends FirebaseFirestore {}

export class Firestore {
constructor(firestore: FirebaseFirestore) {
return firestore;
}
}

export const FIRESTORE_PROVIDER_NAME = 'firestore/lite';

// tslint:disable-next-line:no-empty-interface
export interface FirestoreInstances extends Array<FirebaseFirestore> {}

export class FirestoreInstances {
constructor() {
return ɵgetAllInstancesOf<FirebaseFirestore>(FIRESTORE_PROVIDER_NAME);
}
}

export const firestoreInstance$ = timer(0, 300).pipe(
concatMap(() => from(ɵgetAllInstancesOf<FirebaseFirestore>(FIRESTORE_PROVIDER_NAME))),
distinct(),
);
11 changes: 11 additions & 0 deletions src/firestore/lite/package.json
@@ -0,0 +1,11 @@
{
"$schema": "../../../node_modules/ng-packagr/package.schema.json",
"ngPackage": {
"lib": {
"umdModuleIds": {
"rxfire/firestore-lite": "rxfire-firestore-lite"
},
"entryFile": "public_api.ts"
}
}
}
3 changes: 3 additions & 0 deletions src/firestore/lite/public_api.ts
@@ -0,0 +1,3 @@
export { Firestore, FirestoreInstances, firestoreInstance$ } from './lite';
export { FirestoreModule, provideFirestore } from './lite.module';
export * from './rxfire';
6 changes: 2 additions & 4 deletions tools/build.ts
Expand Up @@ -6,16 +6,13 @@ import { dirname, join } from 'path';
import { keys as tsKeys } from 'ts-transformer-keys';
import firebase from 'firebase/compat/app';

const yada = tsKeys<typeof import('rxfire/database')>();
const yada2 = tsKeys<typeof import('rxfire/auth')>();

// TODO infer these from the package.json
const MODULES = [
'core', 'app', 'compat', 'analytics', 'auth', 'database', 'firestore', 'functions',
'remote-config', 'storage', 'messaging', 'performance', 'compat/analytics',
'compat/auth-guard', 'compat/auth', 'compat/database', 'compat/firestore',
'compat/functions', 'compat/remote-config', 'compat/storage', 'compat/messaging',
'compat/performance'
'compat/performance', 'firestore/lite',
];
const LAZY_MODULES = ['compat/analytics', 'compat/auth', 'compat/functions', 'compat/messaging', 'compat/remote-config'];
const UMD_NAMES = MODULES.map(m => m === 'core' ? 'angular-fire' : `angular-fire-${m.replace('/', '-')}`);
Expand Down Expand Up @@ -45,6 +42,7 @@ export function ${exportName}(..._: Parameters<Before['${exportName}']>) {
reexport('remote-config', 'rxfire', 'rxfire/remote-config', tsKeys<typeof import('rxfire/remote-config')>()),
reexport('storage', 'rxfire', 'rxfire/storage', tsKeys<typeof import('rxfire/storage')>()),
reexport('performance', 'rxfire', 'rxfire/performance', tsKeys<typeof import('rxfire/performance')>()),
reexport('firestore/lite', 'rxfire', 'rxfire/firestore/lite', tsKeys<typeof import('rxfire/firestore/lite')>()),
]);
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -11678,10 +11678,10 @@ rx@2.3.24:
resolved "https://registry.yarnpkg.com/rx/-/rx-2.3.24.tgz#14f950a4217d7e35daa71bbcbe58eff68ea4b2b7"
integrity sha1-FPlQpCF9fjXapxu8vljv9o6ksrc=

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

Expand Down

0 comments on commit 1233a1c

Please sign in to comment.