Skip to content

Commit

Permalink
fix(afs): fix di warning (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and davideast committed Jan 3, 2018
1 parent 7a34bae commit 23ab383
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/firestore/enable-persistance-token.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>('EnablePersistenceToken');
31 changes: 10 additions & 21 deletions src/firestore/firestore.module.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>('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
]
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/firestore/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

});
5 changes: 3 additions & 2 deletions src/firestore/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


/**
Expand Down Expand Up @@ -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 ?
Expand Down

0 comments on commit 23ab383

Please sign in to comment.