Skip to content

Commit 2980baa

Browse files
fix: improvements to firebase rtdb module
1 parent 9d33e3c commit 2980baa

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/modules/firebase/server.firebase.module.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import {
66
SkipSelf
77
} from '@angular/core'
88
import { ServerUniversalRtDbService } from './server.firebase.rtdb.service'
9-
import { HttpClient } from '@angular/common/http'
109
import { UniversalRtDbService } from './browser.firebase.rtdb.service'
11-
import { AngularFireDatabase } from 'angularfire2/database'
1210

1311
export interface LruCache {
1412
readonly get: <T>(key: string) => T
@@ -31,13 +29,7 @@ export class FirebaseServerModule {
3129
providers: [
3230
{
3331
provide: UniversalRtDbService,
34-
useClass: ServerUniversalRtDbService,
35-
deps: [
36-
HttpClient,
37-
AngularFireDatabase,
38-
FIREBASE_USER_AUTH_TOKEN,
39-
LRU_CACHE
40-
]
32+
useClass: ServerUniversalRtDbService
4133
}
4234
]
4335
}

src/modules/firebase/server.firebase.rtdb.service.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ function mapUndefined(err: any) {
3737
return of(undefined)
3838
}
3939

40-
function attempToCacheInLru(key: string, lru?: LruCache) {
40+
function attemptToCacheInLru(key: string, lru?: LruCache) {
4141
return function(response?: any) {
4242
lru && lru.set(sha256(key), response)
4343
}
4444
}
4545

46+
function attemptToGetCachedValue<T>(key: string, lru?: LruCache) {
47+
return lru && lru.get<T>(key)
48+
}
49+
4650
// tslint:disable:no-this
4751
// tslint:disable-next-line:no-class
4852
@Injectable()
@@ -62,15 +66,18 @@ export class ServerUniversalRtDbService implements IUniversalRtdbService {
6266
const url = constructFbUrl(this.afdb, path)
6367
const params = getParams({ auth: this.authToken })
6468
const cacheKey = getFullUrl(url, params)
69+
const cachedValue = attemptToGetCachedValue<T>(cacheKey)
6570

6671
const baseObs = this.authToken
6772
? this.http.get<T>(url, { params })
6873
: this.http.get<T>(url)
6974

70-
return baseObs.pipe(
71-
take(1),
72-
tap(attempToCacheInLru(cacheKey, this.lru)),
73-
catchError(mapUndefined)
74-
)
75+
return cachedValue
76+
? of(cachedValue)
77+
: baseObs.pipe(
78+
take(1),
79+
tap(attemptToCacheInLru(cacheKey, this.lru)),
80+
catchError(mapUndefined)
81+
)
7582
}
7683
}

src/modules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export {
1414
ServerUniversalRtDbService
1515
} from './firebase/server.firebase.rtdb.service'
1616
export { UniversalRtDbService } from './firebase/browser.firebase.rtdb.service'
17+
export { LRU_CACHE } from './firebase/server.firebase.module'

0 commit comments

Comments
 (0)