@@ -14,19 +14,42 @@ import type {
14
14
DocumentSnapshot ,
15
15
FirestoreDataConverter ,
16
16
Query ,
17
+ SnapshotListenOptions ,
18
+ SnapshotOptions ,
17
19
} from 'firebase/firestore'
18
20
import { onSnapshot } from 'firebase/firestore'
19
21
20
22
export interface FirestoreOptions {
21
23
maxRefDepth ?: number
22
24
reset ?: boolean | ( ( ) => any )
23
25
26
+ // FIXME: should only be possible in global options
24
27
converter ?: FirestoreDataConverter < unknown >
25
28
29
+ initialValue ?: unknown
30
+
31
+ snapshotOptions ?: SnapshotOptions
32
+
33
+ /**
34
+ * @inheritdoc {SnapshotListenOptions}
35
+ */
36
+ snapshotListenOptions ?: SnapshotListenOptions
37
+
26
38
wait ?: boolean
27
39
}
28
40
29
- const DEFAULT_OPTIONS : Required < FirestoreOptions > = {
41
+ export interface _GlobalFirestoreOptions extends FirestoreOptions {
42
+ maxRefDepth : number
43
+ reset : boolean | ( ( ) => any )
44
+ converter : FirestoreDataConverter < unknown >
45
+ wait : boolean
46
+ }
47
+
48
+ export interface VueFireFirestoreOptions extends FirestoreOptions {
49
+ converter ?: FirestoreDataConverter < unknown >
50
+ }
51
+
52
+ const DEFAULT_OPTIONS : _GlobalFirestoreOptions = {
30
53
maxRefDepth : 2 ,
31
54
reset : true ,
32
55
converter : firestoreDefaultConverter ,
@@ -50,7 +73,7 @@ function unsubscribeAll(subs: Record<string, FirestoreSubscription>) {
50
73
}
51
74
52
75
function updateDataFromDocumentSnapshot < T > (
53
- options : Required < FirestoreOptions > ,
76
+ options : _GlobalFirestoreOptions ,
54
77
target : Ref < T > ,
55
78
path : string ,
56
79
snapshot : DocumentSnapshot < T > ,
@@ -81,7 +104,7 @@ interface SubscribeToDocumentParamater {
81
104
82
105
function subscribeToDocument (
83
106
{ ref, target, path, depth, resolve, ops } : SubscribeToDocumentParamater ,
84
- options : Required < FirestoreOptions >
107
+ options : _GlobalFirestoreOptions
85
108
) {
86
109
const subs = Object . create ( null )
87
110
const unbind = onSnapshot ( ref , ( snapshot ) => {
@@ -122,7 +145,7 @@ function subscribeToDocument(
122
145
// first one is calling the other on every ref and subscribeToDocument may call
123
146
// updateDataFromDocumentSnapshot which may call subscribeToRefs as well
124
147
function subscribeToRefs (
125
- options : Required < FirestoreOptions > ,
148
+ options : _GlobalFirestoreOptions ,
126
149
target : CommonBindOptionsParameter [ 'target' ] ,
127
150
path : string | number ,
128
151
subs : Record < string , FirestoreSubscription > ,
@@ -183,6 +206,7 @@ function subscribeToRefs(
183
206
} )
184
207
}
185
208
209
+ // TODO: get rid of the any
186
210
interface CommonBindOptionsParameter {
187
211
// vm: Record<string, any>
188
212
target : Ref < any >
@@ -193,20 +217,18 @@ interface CommonBindOptionsParameter {
193
217
ops : OperationsType
194
218
}
195
219
196
- interface BindCollectionParameter extends CommonBindOptionsParameter {
197
- collection : CollectionReference | Query
198
- }
199
-
200
220
export function bindCollection < T = unknown > (
201
- target : BindCollectionParameter [ 'target' ] ,
221
+ target : CommonBindOptionsParameter [ 'target' ] ,
202
222
collection : CollectionReference < T > | Query < T > ,
203
- ops : BindCollectionParameter [ 'ops' ] ,
204
- resolve : BindCollectionParameter [ 'resolve' ] ,
205
- reject : BindCollectionParameter [ 'reject' ] ,
223
+ ops : CommonBindOptionsParameter [ 'ops' ] ,
224
+ resolve : CommonBindOptionsParameter [ 'resolve' ] ,
225
+ reject : CommonBindOptionsParameter [ 'reject' ] ,
206
226
extraOptions : FirestoreOptions = DEFAULT_OPTIONS
207
227
) {
208
228
const options = Object . assign ( { } , DEFAULT_OPTIONS , extraOptions ) // fill default values
209
229
230
+ const { snapshotListenOptions, snapshotOptions, wait } = options
231
+
210
232
if ( ! collection . converter ) {
211
233
// @ts -expect-error: seems like a ts error
212
234
collection = collection . withConverter (
@@ -216,8 +238,8 @@ export function bindCollection<T = unknown>(
216
238
}
217
239
218
240
const key = 'value'
219
- if ( ! options . wait ) ops . set ( target , key , [ ] )
220
- let arrayRef = ref ( options . wait ? [ ] : target [ key ] )
241
+ if ( ! wait ) ops . set ( target , key , [ ] )
242
+ let arrayRef = ref ( wait ? [ ] : target [ key ] )
221
243
const originalResolve = resolve
222
244
let isResolved : boolean
223
245
@@ -229,10 +251,9 @@ export function bindCollection<T = unknown>(
229
251
added : ( { newIndex, doc } : DocumentChange < T > ) => {
230
252
arraySubs . splice ( newIndex , 0 , Object . create ( null ) )
231
253
const subs = arraySubs [ newIndex ]
232
- // FIXME: wrong cast, needs better types
233
- // TODO: pass SnapshotOptions
234
254
const [ data , refs ] = extractRefs (
235
- doc . data ( ) as DocumentData ,
255
+ // @ts -expect-error: FIXME: wrong cast, needs better types
256
+ doc . data ( snapshotOptions ) ,
236
257
undefined ,
237
258
subs
238
259
)
@@ -253,8 +274,7 @@ export function bindCollection<T = unknown>(
253
274
const subs = arraySubs [ oldIndex ]
254
275
const oldData = array [ oldIndex ]
255
276
// @ts -expect-error: FIXME: Better types
256
- // TODO: pass SnapshotOptions
257
- const [ data , refs ] = extractRefs ( doc . data ( ) , oldData , subs )
277
+ const [ data , refs ] = extractRefs ( doc . data ( snapshotOptions ) , oldData , subs )
258
278
// only move things around after extracting refs
259
279
// only move things around after extracting refs
260
280
arraySubs . splice ( newIndex , 0 , subs )
@@ -287,7 +307,7 @@ export function bindCollection<T = unknown>(
287
307
// from the query appearing as added
288
308
// (https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots)
289
309
290
- const docChanges = snapshot . docChanges ( )
310
+ const docChanges = snapshot . docChanges ( snapshotListenOptions )
291
311
292
312
if ( ! isResolved && docChanges . length ) {
293
313
// isResolved is only meant to make sure we do the check only once
0 commit comments