1
1
import * as firebase from 'firebase/app' ;
2
2
import { hasKey , isNil , isAbsoluteUrl , isEmptyObject , isString , ZoneScheduler } from '../utils' ;
3
3
import { checkForUrlOrFirebaseRef , isFirebaseRef } from './utils' ;
4
- import { unwrapSnapshot } from './unwrap_snapshot' ;
4
+ import { unwrapSnapshot as defaultUnwrapSnapshot } from './unwrap_snapshot' ;
5
5
import 'firebase/database' ;
6
6
import { FirebaseListObservable } from './firebase_list_observable' ;
7
7
import { Observer } from 'rxjs/Observer' ;
@@ -13,7 +13,14 @@ import { map } from 'rxjs/operator/map';
13
13
14
14
export function FirebaseListFactory (
15
15
pathRef : PathReference ,
16
- { preserveSnapshot, query = { } } :FirebaseListFactoryOpts = { } ) : FirebaseListObservable < any > {
16
+ { preserveSnapshot, query = { } , unwrapSnapshot } :FirebaseListFactoryOpts = { } ) : FirebaseListObservable < any > {
17
+
18
+ if ( unwrapSnapshot && preserveSnapshot ) {
19
+ throw new Error ( 'Cannot use preserveSnapshot with unwrapSnapshot.' ) ;
20
+ }
21
+ if ( ! unwrapSnapshot ) {
22
+ unwrapSnapshot = defaultUnwrapSnapshot ;
23
+ }
17
24
18
25
let ref : QueryReference ;
19
26
@@ -34,7 +41,7 @@ export function FirebaseListFactory (
34
41
if ( ( isFirebaseRef ( pathRef ) ||
35
42
isString ( pathRef ) ) &&
36
43
isEmptyObject ( query ) ) {
37
- return firebaseListObservable ( ref , { preserveSnapshot } ) ;
44
+ return firebaseListObservable ( ref , { preserveSnapshot, unwrapSnapshot } ) ;
38
45
}
39
46
40
47
const queryObs = observeQuery ( query ) ;
@@ -111,7 +118,7 @@ export function FirebaseListFactory (
111
118
112
119
return queried ;
113
120
} ) , ( queryRef : firebase . database . Reference , ix : number ) => {
114
- return firebaseListObservable ( queryRef , { preserveSnapshot } ) ;
121
+ return firebaseListObservable ( queryRef , { preserveSnapshot, unwrapSnapshot } ) ;
115
122
} )
116
123
. subscribe ( subscriber ) ;
117
124
@@ -126,7 +133,7 @@ export function FirebaseListFactory (
126
133
* asynchonous. It creates a initial array from a promise of ref.once('value'), and then starts
127
134
* listening to child events. When the initial array is loaded, the observable starts emitting values.
128
135
*/
129
- function firebaseListObservable ( ref : firebase . database . Reference | DatabaseQuery , { preserveSnapshot} : FirebaseListFactoryOpts = { } ) : FirebaseListObservable < any > {
136
+ function firebaseListObservable ( ref : firebase . database . Reference | DatabaseQuery , { preserveSnapshot, unwrapSnapshot } : FirebaseListFactoryOpts = { } ) : FirebaseListObservable < any > {
130
137
131
138
const toValue = preserveSnapshot ? ( snapshot => snapshot ) : unwrapSnapshot ;
132
139
const toKey = preserveSnapshot ? ( value => value . key ) : ( value => value . $key ) ;
0 commit comments