From 23ab383e41e02419f8acd76343aa4402e78af9eb Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Wed, 3 Jan 2018 12:24:24 -0500 Subject: [PATCH] fix(afs): fix di warning (#1401) --- src/firestore/enable-persistance-token.ts | 6 +++++ src/firestore/firestore.module.ts | 31 ++++++++--------------- src/firestore/firestore.spec.ts | 26 +++++++++++++++++++ src/firestore/firestore.ts | 5 ++-- 4 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 src/firestore/enable-persistance-token.ts diff --git a/src/firestore/enable-persistance-token.ts b/src/firestore/enable-persistance-token.ts new file mode 100644 index 000000000..28b2c3708 --- /dev/null +++ b/src/firestore/enable-persistance-token.ts @@ -0,0 +1,6 @@ +import { InjectionToken } from '@angular/core'; + +/** + * The value of this token determines whether or not the firestore will have persistance enabled + */ +export const EnablePersistenceToken = new InjectionToken('EnablePersistenceToken'); diff --git a/src/firestore/firestore.module.ts b/src/firestore/firestore.module.ts index a20b96645..77600561b 100644 --- a/src/firestore/firestore.module.ts +++ b/src/firestore/firestore.module.ts @@ -1,36 +1,25 @@ -import { NgModule, InjectionToken } from '@angular/core'; +import { InjectionToken, ModuleWithProviders, NgModule } from '@angular/core'; import { FirebaseApp, AngularFireModule } from 'angularfire2'; import { AngularFirestore } from './firestore'; import { from } from 'rxjs/observable/from'; -export const EnablePersistenceToken = new InjectionToken('EnablePersistenceToken'); - -export function _getAngularFirestore(app: FirebaseApp, enablePersistence: boolean) { - return new AngularFirestore(app, enablePersistence); -} - -export const AngularFirestoreProvider = { - provide: AngularFirestore, - useFactory: _getAngularFirestore, - deps: [ FirebaseApp, EnablePersistenceToken ] -}; - -export const FIRESTORE_PROVIDERS = [ - AngularFirestoreProvider, - { provide: EnablePersistenceToken, useValue: false }, -]; +import { EnablePersistenceToken } from './enable-persistance-token'; @NgModule({ imports: [ AngularFireModule ], - providers: [ FIRESTORE_PROVIDERS ] + providers: [ + AngularFirestore, + ] }) export class AngularFirestoreModule { - static enablePersistence() { + /** + * Attempt to enable persistent storage, if possible + */ + static enablePersistence(): ModuleWithProviders { return { - ngModule: AngularFireModule, + ngModule: AngularFirestoreModule, providers: [ { provide: EnablePersistenceToken, useValue: true }, - AngularFirestoreProvider ] } } diff --git a/src/firestore/firestore.spec.ts b/src/firestore/firestore.spec.ts index d9f221e84..0d811cdb1 100644 --- a/src/firestore/firestore.spec.ts +++ b/src/firestore/firestore.spec.ts @@ -79,3 +79,29 @@ describe('AngularFirestore', () => { }); }); + +describe('AngularFirestore without persistance', () => { + let app: FBApp; + let afs: AngularFirestore; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + AngularFireModule.initializeApp(COMMON_CONFIG), + AngularFirestoreModule + ] + }); + inject([FirebaseApp, AngularFirestore], (_app: FBApp, _afs: AngularFirestore) => { + app = _app; + afs = _afs; + })(); + }); + + it('should not enable persistence', (done) => { + afs.persistenceEnabled$.subscribe(isEnabled => { + expect(isEnabled).toBe(false); + done(); + }); + }); + +}); diff --git a/src/firestore/firestore.ts b/src/firestore/firestore.ts index 30e08d76d..d9c6da377 100644 --- a/src/firestore/firestore.ts +++ b/src/firestore/firestore.ts @@ -4,12 +4,13 @@ import { Subscriber } from 'rxjs/Subscriber'; import { from } from 'rxjs/observable/from'; import 'rxjs/add/operator/map'; -import { Injectable } from '@angular/core'; +import { Injectable, Inject, Optional } from '@angular/core'; import { FirebaseApp } from 'angularfire2'; import { QueryFn, AssociatedReference } from './interfaces'; import { AngularFirestoreDocument } from './document/document'; import { AngularFirestoreCollection } from './collection/collection'; +import { EnablePersistenceToken } from './enable-persistance-token'; /** @@ -96,7 +97,7 @@ export class AngularFirestore { * apps and use multiple apps. * @param app */ - constructor(public app: FirebaseApp, shouldEnablePersistence) { + constructor(public app: FirebaseApp, @Optional() @Inject(EnablePersistenceToken) shouldEnablePersistence: boolean) { this.firestore = app.firestore(); this.persistenceEnabled$ = shouldEnablePersistence ?