5
5
OperationsType ,
6
6
_MaybeRef ,
7
7
ResetOption ,
8
+ _DataSourceOptions ,
8
9
} from '../shared'
9
10
import { ref , Ref , unref } from 'vue-demi'
10
11
import type {
@@ -20,12 +21,15 @@ import type {
20
21
} from 'firebase/firestore'
21
22
import { onSnapshot } from 'firebase/firestore'
22
23
23
- export interface FirestoreOptions {
24
+ /**
25
+ * Options when binding a Firestore document or collection.
26
+ */
27
+ export interface FirestoreRefOptions extends _DataSourceOptions {
28
+ /**
29
+ * The maximum depth to bind nested refs. A nested ref that isn't bound will stay as the ref path while a bound ref
30
+ * will contain the same data as if the ref was bound directly.
31
+ */
24
32
maxRefDepth ?: number
25
- reset ?: ResetOption
26
-
27
- // FIXME: should only be possible in global options
28
- converter ?: FirestoreDataConverter < unknown >
29
33
30
34
initialValue ?: unknown
31
35
@@ -35,26 +39,38 @@ export interface FirestoreOptions {
35
39
* @inheritDoc {SnapshotListenOptions}
36
40
*/
37
41
snapshotListenOptions ?: SnapshotListenOptions
38
-
39
- wait ?: boolean
40
42
}
41
43
42
- export interface _GlobalFirestoreOptions extends FirestoreOptions {
43
- maxRefDepth : number
44
+ /**
45
+ * Type of the global options for firestore refs. Some values cannot be `undefined`.
46
+ * @internal
47
+ */
48
+ export interface _GlobalFirestoreRefOptions extends FirestoreRefOptions {
49
+ /**
50
+ * @defaultValue `false`
51
+ */
44
52
reset : ResetOption
45
- converter : FirestoreDataConverter < unknown >
53
+ /**
54
+ * @defaultValue `true`
55
+ */
46
56
wait : boolean
47
- }
48
57
49
- export interface VueFireFirestoreOptions extends FirestoreOptions {
50
- converter ?: FirestoreDataConverter < unknown >
58
+ /**
59
+ * @defaultValue `2`
60
+ */
61
+ maxRefDepth : number
62
+
63
+ /**
64
+ * Default Firestore converter to use with snapshots.
65
+ */
66
+ converter : FirestoreDataConverter < unknown >
51
67
}
52
68
53
- const DEFAULT_OPTIONS : _GlobalFirestoreOptions = {
69
+ const DEFAULT_OPTIONS : _GlobalFirestoreRefOptions = {
70
+ reset : false ,
71
+ wait : true ,
54
72
maxRefDepth : 2 ,
55
- reset : true ,
56
73
converter : firestoreDefaultConverter ,
57
- wait : false ,
58
74
}
59
75
export { DEFAULT_OPTIONS as firestoreOptions }
60
76
@@ -74,7 +90,7 @@ function unsubscribeAll(subs: Record<string, FirestoreSubscription>) {
74
90
}
75
91
76
92
function updateDataFromDocumentSnapshot < T > (
77
- options : _GlobalFirestoreOptions ,
93
+ options : _GlobalFirestoreRefOptions ,
78
94
target : Ref < T > ,
79
95
path : string ,
80
96
snapshot : DocumentSnapshot < T > ,
@@ -94,8 +110,8 @@ function updateDataFromDocumentSnapshot<T>(
94
110
subscribeToRefs ( options , target , path , subs , refs , ops , depth , resolve )
95
111
}
96
112
97
- interface SubscribeToDocumentParamater {
98
- target : CommonBindOptionsParameter [ 'target' ]
113
+ interface SubscribeToDocumentParameter {
114
+ target : Ref < unknown >
99
115
path : string
100
116
depth : number
101
117
resolve : ( ) => void
@@ -104,8 +120,8 @@ interface SubscribeToDocumentParamater {
104
120
}
105
121
106
122
function subscribeToDocument (
107
- { ref, target, path, depth, resolve, ops } : SubscribeToDocumentParamater ,
108
- options : _GlobalFirestoreOptions
123
+ { ref, target, path, depth, resolve, ops } : SubscribeToDocumentParameter ,
124
+ options : _GlobalFirestoreRefOptions
109
125
) {
110
126
const subs = Object . create ( null )
111
127
const unbind = onSnapshot ( ref , ( snapshot ) => {
@@ -132,22 +148,12 @@ function subscribeToDocument(
132
148
}
133
149
}
134
150
135
- // interface SubscribeToRefsParameter {
136
- // subs: Record<string, FirestoreSubscription>
137
- // target: CommonBindOptionsParameter['vm']
138
- // refs: Record<string, DocumentReference>
139
- // path: string | number
140
- // depth: number
141
- // resolve: CommonBindOptionsParameter['resolve']
142
- // ops: CommonBindOptionsParameter['ops']
143
- // }
144
-
145
151
// NOTE: not convinced by the naming of subscribeToRefs and subscribeToDocument
146
152
// first one is calling the other on every ref and subscribeToDocument may call
147
153
// updateDataFromDocumentSnapshot which may call subscribeToRefs as well
148
154
function subscribeToRefs (
149
- options : _GlobalFirestoreOptions ,
150
- target : CommonBindOptionsParameter [ 'target' ] ,
155
+ options : _GlobalFirestoreRefOptions ,
156
+ target : Ref < unknown > ,
151
157
path : string | number ,
152
158
subs : Record < string , FirestoreSubscription > ,
153
159
refs : Record < string , DocumentReference > ,
@@ -219,12 +225,12 @@ interface CommonBindOptionsParameter {
219
225
}
220
226
221
227
export function bindCollection < T = unknown > (
222
- target : CommonBindOptionsParameter [ 'target' ] ,
228
+ target : Ref < unknown [ ] > ,
223
229
collection : CollectionReference < T > | Query < T > ,
224
230
ops : CommonBindOptionsParameter [ 'ops' ] ,
225
231
resolve : CommonBindOptionsParameter [ 'resolve' ] ,
226
232
reject : CommonBindOptionsParameter [ 'reject' ] ,
227
- extraOptions ?: FirestoreOptions
233
+ extraOptions ?: FirestoreRefOptions
228
234
) {
229
235
// FIXME: can be removed now
230
236
const options = Object . assign ( { } , DEFAULT_OPTIONS , extraOptions ) // fill default values
@@ -348,7 +354,7 @@ export function bindCollection<T = unknown>(
348
354
reject
349
355
)
350
356
351
- return ( reset ?: FirestoreOptions [ 'reset' ] ) => {
357
+ return ( reset ?: FirestoreRefOptions [ 'reset' ] ) => {
352
358
unbind ( )
353
359
if ( reset !== false ) {
354
360
const value = typeof reset === 'function' ? reset ( ) : [ ]
@@ -368,12 +374,12 @@ interface BindDocumentParameter extends CommonBindOptionsParameter {
368
374
* @param extraOptions
369
375
*/
370
376
export function bindDocument < T > (
371
- target : BindDocumentParameter [ 'target' ] ,
377
+ target : Ref < unknown > ,
372
378
document : DocumentReference < T > ,
373
379
ops : BindDocumentParameter [ 'ops' ] ,
374
380
resolve : BindDocumentParameter [ 'resolve' ] ,
375
381
reject : BindDocumentParameter [ 'reject' ] ,
376
- extraOptions ?: FirestoreOptions
382
+ extraOptions ?: FirestoreRefOptions
377
383
) {
378
384
const options = Object . assign ( { } , DEFAULT_OPTIONS , extraOptions ) // fill default values
379
385
const key = 'value'
@@ -406,7 +412,7 @@ export function bindDocument<T>(
406
412
reject
407
413
)
408
414
409
- return ( reset ?: FirestoreOptions [ 'reset' ] ) => {
415
+ return ( reset ?: FirestoreRefOptions [ 'reset' ] ) => {
410
416
_unbind ( )
411
417
if ( reset !== false ) {
412
418
const value = typeof reset === 'function' ? reset ( ) : null
0 commit comments