@@ -361,7 +361,7 @@ export function useDocument<
361
361
> (
362
362
documentRef : R ,
363
363
options ?: UseDocumentOptions
364
- ) : Ref < _InferReferenceType < R > | null >
364
+ ) : _RefWithState < _InferReferenceType < R > | null >
365
365
366
366
/**
367
367
* Creates a reactive collection (usually an array) of documents from a collection ref or a query from Firestore.
@@ -374,18 +374,28 @@ export function useDocument<
374
374
export function useDocument < T > (
375
375
documentRef : DocumentReference ,
376
376
options ?: UseDocumentOptions
377
- ) : Ref < T | null >
377
+ ) : _RefWithState < T | null >
378
378
379
379
export function useDocument < T > (
380
380
documentRef : DocumentReference < unknown > ,
381
381
options ?: UseDocumentOptions
382
- ) : Ref < _InferReferenceType < T > | null > | Ref < T | null > {
383
- const data = ref < T | null > ( null )
382
+ ) : _RefWithState < _InferReferenceType < T > | null > | _RefWithState < T | null > {
383
+ const data = ref < T | null > ( null ) as Ref < T >
384
+ const pending = ref ( true )
385
+ // TODO: can this error type come from firebase?
386
+ const error = ref < Error > ( )
384
387
385
388
let unbind ! : ReturnType < typeof bindDocument >
386
389
const promise = new Promise ( ( resolve , reject ) => {
387
390
unbind = bindDocument ( data , documentRef , ops , resolve , reject , options )
388
391
} )
392
+ promise
393
+ . catch ( reason => {
394
+ error . value = reason
395
+ } )
396
+ . finally ( ( ) => {
397
+ pending . value = false
398
+ } )
389
399
390
400
// TODO: refactor in a function
391
401
if ( getCurrentScope ( ) ) {
@@ -396,8 +406,40 @@ export function useDocument<T>(
396
406
} )
397
407
}
398
408
409
+ Object . defineProperties ( data , {
410
+ error : {
411
+ get : ( ) => error ,
412
+ } ,
413
+ data : {
414
+ get : ( ) => data ,
415
+ } ,
416
+ pending : {
417
+ get : ( ) => pending ,
418
+ } ,
419
+ promise : {
420
+ get : ( ) => promise ,
421
+ } ,
422
+ unbind : {
423
+ get : ( ) => unbind ,
424
+ } ,
425
+ } )
426
+
399
427
// no unwrapRef to have a simpler type
400
- return data as Ref < T | null >
428
+ return data as _RefWithState < T >
429
+ }
430
+
431
+ /**
432
+ * @internal
433
+ */
434
+ export interface _RefWithState < T > extends Ref < T > {
435
+ get data ( ) : Ref < T >
436
+ get error ( ) : Ref < Error | undefined >
437
+ get pending ( ) : Ref < boolean >
438
+
439
+ // TODO: is it really void?
440
+ promise : Promise < void >
441
+ // TODO: extract type from bindDocument and bindCollection
442
+ unbind : ( ) => void
401
443
}
402
444
403
445
export const unbind = ( target : Ref , reset ?: FirestoreOptions [ 'reset' ] ) =>
0 commit comments