55 OperationsType ,
66 _MaybeRef ,
77 ResetOption ,
8+ _DataSourceOptions ,
89} from '../shared'
910import { ref , Ref , unref } from 'vue-demi'
1011import type {
@@ -20,12 +21,15 @@ import type {
2021} from 'firebase/firestore'
2122import { onSnapshot } from 'firebase/firestore'
2223
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+ */
2432 maxRefDepth ?: number
25- reset ?: ResetOption
26-
27- // FIXME: should only be possible in global options
28- converter ?: FirestoreDataConverter < unknown >
2933
3034 initialValue ?: unknown
3135
@@ -35,26 +39,38 @@ export interface FirestoreOptions {
3539 * @inheritDoc {SnapshotListenOptions}
3640 */
3741 snapshotListenOptions ?: SnapshotListenOptions
38-
39- wait ?: boolean
4042}
4143
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+ */
4452 reset : ResetOption
45- converter : FirestoreDataConverter < unknown >
53+ /**
54+ * @defaultValue `true`
55+ */
4656 wait : boolean
47- }
4857
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 >
5167}
5268
53- const DEFAULT_OPTIONS : _GlobalFirestoreOptions = {
69+ const DEFAULT_OPTIONS : _GlobalFirestoreRefOptions = {
70+ reset : false ,
71+ wait : true ,
5472 maxRefDepth : 2 ,
55- reset : true ,
5673 converter : firestoreDefaultConverter ,
57- wait : false ,
5874}
5975export { DEFAULT_OPTIONS as firestoreOptions }
6076
@@ -74,7 +90,7 @@ function unsubscribeAll(subs: Record<string, FirestoreSubscription>) {
7490}
7591
7692function updateDataFromDocumentSnapshot < T > (
77- options : _GlobalFirestoreOptions ,
93+ options : _GlobalFirestoreRefOptions ,
7894 target : Ref < T > ,
7995 path : string ,
8096 snapshot : DocumentSnapshot < T > ,
@@ -94,8 +110,8 @@ function updateDataFromDocumentSnapshot<T>(
94110 subscribeToRefs ( options , target , path , subs , refs , ops , depth , resolve )
95111}
96112
97- interface SubscribeToDocumentParamater {
98- target : CommonBindOptionsParameter [ 'target' ]
113+ interface SubscribeToDocumentParameter {
114+ target : Ref < unknown >
99115 path : string
100116 depth : number
101117 resolve : ( ) => void
@@ -104,8 +120,8 @@ interface SubscribeToDocumentParamater {
104120}
105121
106122function subscribeToDocument (
107- { ref, target, path, depth, resolve, ops } : SubscribeToDocumentParamater ,
108- options : _GlobalFirestoreOptions
123+ { ref, target, path, depth, resolve, ops } : SubscribeToDocumentParameter ,
124+ options : _GlobalFirestoreRefOptions
109125) {
110126 const subs = Object . create ( null )
111127 const unbind = onSnapshot ( ref , ( snapshot ) => {
@@ -132,22 +148,12 @@ function subscribeToDocument(
132148 }
133149}
134150
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-
145151// NOTE: not convinced by the naming of subscribeToRefs and subscribeToDocument
146152// first one is calling the other on every ref and subscribeToDocument may call
147153// updateDataFromDocumentSnapshot which may call subscribeToRefs as well
148154function subscribeToRefs (
149- options : _GlobalFirestoreOptions ,
150- target : CommonBindOptionsParameter [ 'target' ] ,
155+ options : _GlobalFirestoreRefOptions ,
156+ target : Ref < unknown > ,
151157 path : string | number ,
152158 subs : Record < string , FirestoreSubscription > ,
153159 refs : Record < string , DocumentReference > ,
@@ -219,12 +225,12 @@ interface CommonBindOptionsParameter {
219225}
220226
221227export function bindCollection < T = unknown > (
222- target : CommonBindOptionsParameter [ 'target' ] ,
228+ target : Ref < unknown [ ] > ,
223229 collection : CollectionReference < T > | Query < T > ,
224230 ops : CommonBindOptionsParameter [ 'ops' ] ,
225231 resolve : CommonBindOptionsParameter [ 'resolve' ] ,
226232 reject : CommonBindOptionsParameter [ 'reject' ] ,
227- extraOptions ?: FirestoreOptions
233+ extraOptions ?: FirestoreRefOptions
228234) {
229235 // FIXME: can be removed now
230236 const options = Object . assign ( { } , DEFAULT_OPTIONS , extraOptions ) // fill default values
@@ -348,7 +354,7 @@ export function bindCollection<T = unknown>(
348354 reject
349355 )
350356
351- return ( reset ?: FirestoreOptions [ 'reset' ] ) => {
357+ return ( reset ?: FirestoreRefOptions [ 'reset' ] ) => {
352358 unbind ( )
353359 if ( reset !== false ) {
354360 const value = typeof reset === 'function' ? reset ( ) : [ ]
@@ -368,12 +374,12 @@ interface BindDocumentParameter extends CommonBindOptionsParameter {
368374 * @param extraOptions
369375 */
370376export function bindDocument < T > (
371- target : BindDocumentParameter [ 'target' ] ,
377+ target : Ref < unknown > ,
372378 document : DocumentReference < T > ,
373379 ops : BindDocumentParameter [ 'ops' ] ,
374380 resolve : BindDocumentParameter [ 'resolve' ] ,
375381 reject : BindDocumentParameter [ 'reject' ] ,
376- extraOptions ?: FirestoreOptions
382+ extraOptions ?: FirestoreRefOptions
377383) {
378384 const options = Object . assign ( { } , DEFAULT_OPTIONS , extraOptions ) // fill default values
379385 const key = 'value'
@@ -406,7 +412,7 @@ export function bindDocument<T>(
406412 reject
407413 )
408414
409- return ( reset ?: FirestoreOptions [ 'reset' ] ) => {
415+ return ( reset ?: FirestoreRefOptions [ 'reset' ] ) => {
410416 _unbind ( )
411417 if ( reset !== false ) {
412418 const value = typeof reset === 'function' ? reset ( ) : null
0 commit comments