Skip to content

Commit 23ab383

Browse files
FrozenPandazdavideast
authored andcommitted
fix(afs): fix di warning (#1401)
1 parent 7a34bae commit 23ab383

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { InjectionToken } from '@angular/core';
2+
3+
/**
4+
* The value of this token determines whether or not the firestore will have persistance enabled
5+
*/
6+
export const EnablePersistenceToken = new InjectionToken<boolean>('EnablePersistenceToken');

src/firestore/firestore.module.ts

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
import { NgModule, InjectionToken } from '@angular/core';
1+
import { InjectionToken, ModuleWithProviders, NgModule } from '@angular/core';
22
import { FirebaseApp, AngularFireModule } from 'angularfire2';
33
import { AngularFirestore } from './firestore';
44
import { from } from 'rxjs/observable/from';
55

6-
export const EnablePersistenceToken = new InjectionToken<boolean>('EnablePersistenceToken');
7-
8-
export function _getAngularFirestore(app: FirebaseApp, enablePersistence: boolean) {
9-
return new AngularFirestore(app, enablePersistence);
10-
}
11-
12-
export const AngularFirestoreProvider = {
13-
provide: AngularFirestore,
14-
useFactory: _getAngularFirestore,
15-
deps: [ FirebaseApp, EnablePersistenceToken ]
16-
};
17-
18-
export const FIRESTORE_PROVIDERS = [
19-
AngularFirestoreProvider,
20-
{ provide: EnablePersistenceToken, useValue: false },
21-
];
6+
import { EnablePersistenceToken } from './enable-persistance-token';
227

238
@NgModule({
249
imports: [ AngularFireModule ],
25-
providers: [ FIRESTORE_PROVIDERS ]
10+
providers: [
11+
AngularFirestore,
12+
]
2613
})
2714
export class AngularFirestoreModule {
28-
static enablePersistence() {
15+
/**
16+
* Attempt to enable persistent storage, if possible
17+
*/
18+
static enablePersistence(): ModuleWithProviders {
2919
return {
30-
ngModule: AngularFireModule,
20+
ngModule: AngularFirestoreModule,
3121
providers: [
3222
{ provide: EnablePersistenceToken, useValue: true },
33-
AngularFirestoreProvider
3423
]
3524
}
3625
}

src/firestore/firestore.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,29 @@ describe('AngularFirestore', () => {
7979
});
8080

8181
});
82+
83+
describe('AngularFirestore without persistance', () => {
84+
let app: FBApp;
85+
let afs: AngularFirestore;
86+
87+
beforeEach(() => {
88+
TestBed.configureTestingModule({
89+
imports: [
90+
AngularFireModule.initializeApp(COMMON_CONFIG),
91+
AngularFirestoreModule
92+
]
93+
});
94+
inject([FirebaseApp, AngularFirestore], (_app: FBApp, _afs: AngularFirestore) => {
95+
app = _app;
96+
afs = _afs;
97+
})();
98+
});
99+
100+
it('should not enable persistence', (done) => {
101+
afs.persistenceEnabled$.subscribe(isEnabled => {
102+
expect(isEnabled).toBe(false);
103+
done();
104+
});
105+
});
106+
107+
});

src/firestore/firestore.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { Subscriber } from 'rxjs/Subscriber';
44
import { from } from 'rxjs/observable/from';
55
import 'rxjs/add/operator/map';
66

7-
import { Injectable } from '@angular/core';
7+
import { Injectable, Inject, Optional } from '@angular/core';
88
import { FirebaseApp } from 'angularfire2';
99

1010
import { QueryFn, AssociatedReference } from './interfaces';
1111
import { AngularFirestoreDocument } from './document/document';
1212
import { AngularFirestoreCollection } from './collection/collection';
13+
import { EnablePersistenceToken } from './enable-persistance-token';
1314

1415

1516
/**
@@ -96,7 +97,7 @@ export class AngularFirestore {
9697
* apps and use multiple apps.
9798
* @param app
9899
*/
99-
constructor(public app: FirebaseApp, shouldEnablePersistence) {
100+
constructor(public app: FirebaseApp, @Optional() @Inject(EnablePersistenceToken) shouldEnablePersistence: boolean) {
100101
this.firestore = app.firestore();
101102

102103
this.persistenceEnabled$ = shouldEnablePersistence ?

0 commit comments

Comments
 (0)