@@ -4,6 +4,7 @@ import type {
44 Query ,
55 FirestoreError ,
66 DocumentData ,
7+ FirestoreDataConverter ,
78} from 'firebase/firestore'
89import {
910 getCurrentScope ,
@@ -23,6 +24,7 @@ import {
2324 ResetOption ,
2425 walkSet ,
2526 _MaybeRef ,
27+ _Nullable ,
2628 _RefWithState ,
2729} from '../shared'
2830import { addPendingPromise } from '../ssr/plugin'
@@ -56,11 +58,13 @@ export interface _UseFirestoreRefOptions extends FirestoreRefOptions {
5658 */
5759export function _useFirestoreRef (
5860 docOrCollectionRef : _MaybeRef <
59- DocumentReference < unknown > | Query < unknown > | CollectionReference < unknown >
61+ _Nullable <
62+ DocumentReference < unknown > | Query < unknown > | CollectionReference < unknown >
63+ >
6064 > ,
6165 localOptions ?: _UseFirestoreRefOptions
6266) {
63- let _unbind ! : UnbindType
67+ let _unbind : UnbindType = noop
6468 const options = Object . assign ( { } , firestoreOptions , localOptions )
6569
6670 // TODO: allow passing pending and error refs as option for when this is called using the options api
@@ -74,8 +78,19 @@ export function _useFirestoreRef(
7478 let removePendingPromise = noop
7579
7680 function bindFirestoreRef ( ) {
81+ let docRefValue = unref ( docOrCollectionRef )
82+
7783 const p = new Promise < unknown | null > ( ( resolve , reject ) => {
78- let docRefValue = unref ( docOrCollectionRef )
84+ // stop the previous subscription
85+ _unbind ( options . reset )
86+ // skip if the ref is null or undefined
87+ // we still want to create the new promise
88+ if ( ! docRefValue ) {
89+ _unbind = noop
90+ // TODO: maybe we shouldn't resolve this at all?
91+ return resolve ( null )
92+ }
93+
7994 if ( ! docRefValue . converter ) {
8095 docRefValue = docRefValue . withConverter (
8196 // @ts -expect-error: seems like a ts error
@@ -95,9 +110,9 @@ export function _useFirestoreRef(
95110 } )
96111
97112 // only add the first promise to the pending ones
98- if ( ! isPromiseAdded ) {
113+ if ( ! isPromiseAdded && docRefValue ) {
99114 // TODO: is there a way to make this only for the first render?
100- removePendingPromise = addPendingPromise ( p , unref ( docOrCollectionRef ) )
115+ removePendingPromise = addPendingPromise ( p , docRefValue )
101116 isPromiseAdded = true
102117 }
103118 promise . value = p
@@ -173,7 +188,7 @@ export function useCollection<
173188 R extends CollectionReference < unknown > | Query < unknown >
174189> (
175190 // TODO: add MaybeRef
176- collectionRef : _MaybeRef < R > ,
191+ collectionRef : _MaybeRef < _Nullable < R > > ,
177192 options ?: UseCollectionOptions
178193) : _RefFirestore < _InferReferenceType < R > [ ] >
179194
@@ -186,17 +201,20 @@ export function useCollection<
186201 * @param options - optional options
187202 */
188203export function useCollection < T > (
189- collectionRef : _MaybeRef < CollectionReference | Query > ,
204+ collectionRef : _MaybeRef < _Nullable < CollectionReference | Query > > ,
190205 options ?: UseCollectionOptions
191206) : _RefFirestore < VueFirestoreQueryData < T > >
192207
193208export function useCollection < T > (
194- collectionRef : _MaybeRef < CollectionReference < unknown > | Query < unknown > > ,
209+ collectionRef : _MaybeRef <
210+ _Nullable < CollectionReference < unknown > | Query < unknown > >
211+ > ,
195212 options ?: UseCollectionOptions
196213) : _RefFirestore < VueFirestoreQueryData < T > > {
197- return _useFirestoreRef ( collectionRef , options ) as _RefFirestore <
198- VueFirestoreQueryData < T >
199- >
214+ return _useFirestoreRef ( collectionRef , {
215+ target : ref ( [ ] ) ,
216+ ...options ,
217+ } ) as _RefFirestore < VueFirestoreQueryData < T > >
200218}
201219
202220// TODO: split document and collection into two different parts
@@ -213,7 +231,7 @@ export function useDocument<
213231 // explicit generic as unknown to allow arbitrary types like numbers or strings
214232 R extends DocumentReference < unknown >
215233> (
216- documentRef : _MaybeRef < R > ,
234+ documentRef : _MaybeRef < _Nullable < R > > ,
217235 options ?: UseDocumentOptions
218236) : _RefFirestore < _InferReferenceType < R > > // this one can't be null or should be specified in the converter
219237
@@ -226,12 +244,12 @@ export function useDocument<
226244 * @param options - optional options
227245 */
228246export function useDocument < T > (
229- documentRef : _MaybeRef < DocumentReference > ,
247+ documentRef : _MaybeRef < _Nullable < DocumentReference > > ,
230248 options ?: UseDocumentOptions
231249) : _RefFirestore < VueFirestoreDocumentData < T > >
232250
233251export function useDocument < T > (
234- documentRef : _MaybeRef < DocumentReference < unknown > > ,
252+ documentRef : _MaybeRef < _Nullable < DocumentReference < unknown > > > ,
235253 options ?: UseDocumentOptions
236254) :
237255 | _RefFirestore < VueFirestoreDocumentData < T > | null >
0 commit comments