11import firebase from 'firebase/app' ;
22import { useEffect , useMemo } from 'react' ;
33import { snapshotToData } from './helpers' ;
4+ import {
5+ Data ,
6+ DocumentHook ,
7+ DocumentDataHook ,
8+ OnceOptions ,
9+ OnceDataOptions ,
10+ } from './types' ;
411import { LoadingHook , useIsEqualRef , useLoadingValue } from '../util' ;
512
6- export type DocumentOnceHook < T > = LoadingHook <
7- firebase . firestore . DocumentSnapshot < T > ,
8- Error
9- > ;
1013export type DocumentDataOnceHook < T > = LoadingHook < T , Error > ;
1114
1215export const useDocumentOnce = < T > (
1316 docRef ?: firebase . firestore . DocumentReference | null ,
14- options ?: {
15- getOptions ?: firebase . firestore . GetOptions ;
16- }
17- ) : DocumentOnceHook < T > => {
17+ options ?: OnceOptions
18+ ) : DocumentHook < T > => {
1819 const { error, loading, reset, setError, setValue, value } = useLoadingValue <
1920 firebase . firestore . DocumentSnapshot ,
2021 Error
@@ -32,31 +33,38 @@ export const useDocumentOnce = <T>(
3233 . catch ( setError ) ;
3334 } , [ ref . current ] ) ;
3435
35- const resArray : DocumentOnceHook < T > = [
36+ const resArray : DocumentHook < T > = [
3637 value as firebase . firestore . DocumentSnapshot < T > ,
3738 loading ,
3839 error ,
3940 ] ;
4041 return useMemo ( ( ) => resArray , resArray ) ;
4142} ;
4243
43- export const useDocumentDataOnce = < T > (
44+ export const useDocumentDataOnce = <
45+ T ,
46+ IDField extends string = '' ,
47+ RefField extends string = ''
48+ > (
4449 docRef ?: firebase . firestore . DocumentReference | null ,
45- options ?: {
46- getOptions ?: firebase . firestore . GetOptions ;
47- idField ?: string ;
48- refField ?: string ;
49- }
50- ) : DocumentDataOnceHook < T > => {
50+ options ?: OnceDataOptions
51+ ) : DocumentDataHook < T , IDField , RefField > => {
5152 const idField = options ? options . idField : undefined ;
5253 const refField = options ? options . refField : undefined ;
5354 const getOptions = options ? options . getOptions : undefined ;
5455 const [ snapshot , loading , error ] = useDocumentOnce < T > ( docRef , { getOptions } ) ;
5556 const value = useMemo (
56- ( ) => ( snapshot ? snapshotToData ( snapshot , idField , refField ) : undefined ) as T ,
57+ ( ) =>
58+ ( snapshot
59+ ? snapshotToData ( snapshot , idField , refField )
60+ : undefined ) as Data < T , IDField , RefField > ,
5761 [ snapshot , idField , refField ]
5862 ) ;
5963
60- const resArray : DocumentDataOnceHook < T > = [ value , loading , error ] ;
64+ const resArray : DocumentDataHook < T , IDField , RefField > = [
65+ value ,
66+ loading ,
67+ error ,
68+ ] ;
6169 return useMemo ( ( ) => resArray , resArray ) ;
6270} ;
0 commit comments