Skip to content

Commit 75821e4

Browse files
committed
Consolidate shared firestore hooks logic
1 parent 5182e86 commit 75821e4

File tree

5 files changed

+143
-182
lines changed

5 files changed

+143
-182
lines changed

firestore/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
export { useCollection, useCollectionData } from './useCollection';
2-
export { useCollectionOnce, useCollectionDataOnce } from './useCollectionOnce';
3-
export { useDocument, useDocumentData } from './useDocument';
4-
export { useDocumentOnce, useDocumentDataOnce } from './useDocumentOnce';
1+
export {
2+
useCollection,
3+
useCollectionOnce,
4+
useCollectionData,
5+
useCollectionDataOnce,
6+
} from './useCollection';
7+
export {
8+
useDocument,
9+
useDocumentData,
10+
useDocumentOnce,
11+
useDocumentDataOnce,
12+
} from './useDocument';
513
export {
614
CollectionHook,
715
CollectionDataHook,

firestore/useCollection.ts

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
CollectionDataHook,
77
Data,
88
DataOptions,
9+
OnceOptions,
10+
OnceDataOptions,
911
Options,
1012
} from './types';
1113
import { useIsEqualRef, useLoadingValue } from '../util';
@@ -14,9 +16,46 @@ export const useCollection = <T = firebase.firestore.DocumentData>(
1416
query?: firebase.firestore.Query<T> | null,
1517
options?: Options
1618
): CollectionHook<T> => {
19+
return useCollectionInternal(true, query, options);
20+
};
21+
22+
export const useCollectionOnce = <T = firebase.firestore.DocumentData>(
23+
query?: firebase.firestore.Query<T> | null,
24+
options?: OnceOptions
25+
): CollectionHook<T> => {
26+
return useCollectionInternal(false, query, options);
27+
};
28+
29+
export const useCollectionData = <
30+
T = firebase.firestore.DocumentData,
31+
IDField extends string = '',
32+
RefField extends string = ''
33+
>(
34+
query?: firebase.firestore.Query<T> | null,
35+
options?: DataOptions
36+
): CollectionDataHook<T, IDField, RefField> => {
37+
return useCollectionDataInternal(true, query, options);
38+
};
39+
40+
export const useCollectionDataOnce = <
41+
T = firebase.firestore.DocumentData,
42+
IDField extends string = '',
43+
RefField extends string = ''
44+
>(
45+
query?: firebase.firestore.Query<T> | null,
46+
options?: OnceDataOptions
47+
): CollectionDataHook<T, IDField, RefField> => {
48+
return useCollectionDataInternal(false, query, options);
49+
};
50+
51+
const useCollectionInternal = <T = firebase.firestore.DocumentData>(
52+
listen: boolean,
53+
query?: firebase.firestore.Query<T> | null,
54+
options?: Options & OnceOptions
55+
) => {
1756
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
1857
firebase.firestore.QuerySnapshot,
19-
Error
58+
firebase.FirebaseError
2059
>();
2160
const ref = useIsEqualRef(query, reset);
2261

@@ -25,18 +64,25 @@ export const useCollection = <T = firebase.firestore.DocumentData>(
2564
setValue(undefined);
2665
return;
2766
}
28-
const listener =
29-
options && options.snapshotListenOptions
30-
? ref.current.onSnapshot(
31-
options.snapshotListenOptions,
32-
setValue,
33-
setError
34-
)
35-
: ref.current.onSnapshot(setValue, setError);
67+
if (listen) {
68+
const listener =
69+
options && options.snapshotListenOptions
70+
? ref.current.onSnapshot(
71+
options.snapshotListenOptions,
72+
setValue,
73+
setError
74+
)
75+
: ref.current.onSnapshot(setValue, setError);
3676

37-
return () => {
38-
listener();
39-
};
77+
return () => {
78+
listener();
79+
};
80+
} else {
81+
ref.current
82+
.get(options ? options.getOptions : undefined)
83+
.then(setValue)
84+
.catch(setError);
85+
}
4086
}, [ref.current]);
4187

4288
const resArray: CollectionHook<T> = [
@@ -47,22 +93,22 @@ export const useCollection = <T = firebase.firestore.DocumentData>(
4793
return useMemo(() => resArray, resArray);
4894
};
4995

50-
export const useCollectionData = <
96+
const useCollectionDataInternal = <
5197
T = firebase.firestore.DocumentData,
5298
IDField extends string = '',
5399
RefField extends string = ''
54100
>(
101+
listen: boolean,
55102
query?: firebase.firestore.Query<T> | null,
56-
options?: DataOptions
103+
options?: DataOptions & OnceDataOptions
57104
): CollectionDataHook<T, IDField, RefField> => {
58105
const idField = options ? options.idField : undefined;
59106
const refField = options ? options.refField : undefined;
60-
const snapshotListenOptions = options
61-
? options.snapshotListenOptions
62-
: undefined;
63-
const [snapshots, loading, error] = useCollection<T>(query, {
64-
snapshotListenOptions,
65-
});
107+
const [snapshots, loading, error] = useCollectionInternal<T>(
108+
listen,
109+
query,
110+
options
111+
);
66112
const values = useMemo(
67113
() =>
68114
(snapshots

firestore/useCollectionOnce.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

firestore/useDocument.ts

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,56 @@ import {
66
DataOptions,
77
DocumentHook,
88
DocumentDataHook,
9+
OnceOptions,
10+
OnceDataOptions,
911
Options,
1012
} from './types';
1113
import { useIsEqualRef, useLoadingValue } from '../util';
1214

1315
export const useDocument = <T = firebase.firestore.DocumentData>(
1416
docRef?: firebase.firestore.DocumentReference<T> | null,
1517
options?: Options
18+
): DocumentHook<T> => {
19+
return useDocumentInternal(true, docRef, options);
20+
};
21+
22+
export const useDocumentOnce = <T = firebase.firestore.DocumentData>(
23+
docRef?: firebase.firestore.DocumentReference<T> | null,
24+
options?: OnceOptions
25+
): DocumentHook<T> => {
26+
return useDocumentInternal(false, docRef, options);
27+
};
28+
29+
export const useDocumentData = <
30+
T = firebase.firestore.DocumentData,
31+
IDField extends string = '',
32+
RefField extends string = ''
33+
>(
34+
docRef?: firebase.firestore.DocumentReference<T> | null,
35+
options?: DataOptions
36+
): DocumentDataHook<T, IDField, RefField> => {
37+
return useDocumentDataInternal(true, docRef, options);
38+
};
39+
40+
export const useDocumentDataOnce = <
41+
T = firebase.firestore.DocumentData,
42+
IDField extends string = '',
43+
RefField extends string = ''
44+
>(
45+
docRef?: firebase.firestore.DocumentReference<T> | null,
46+
options?: OnceDataOptions
47+
): DocumentDataHook<T, IDField, RefField> => {
48+
return useDocumentDataInternal(false, docRef, options);
49+
};
50+
51+
const useDocumentInternal = <T = firebase.firestore.DocumentData>(
52+
listen: boolean,
53+
docRef?: firebase.firestore.DocumentReference<T> | null,
54+
options?: Options & OnceOptions
1655
): DocumentHook<T> => {
1756
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
1857
firebase.firestore.DocumentSnapshot,
19-
Error
58+
firebase.FirebaseError
2059
>();
2160
const ref = useIsEqualRef(docRef, reset);
2261

@@ -25,18 +64,25 @@ export const useDocument = <T = firebase.firestore.DocumentData>(
2564
setValue(undefined);
2665
return;
2766
}
28-
const listener =
29-
options && options.snapshotListenOptions
30-
? ref.current.onSnapshot(
31-
options.snapshotListenOptions,
32-
setValue,
33-
setError
34-
)
35-
: ref.current.onSnapshot(setValue, setError);
67+
if (listen) {
68+
const listener =
69+
options && options.snapshotListenOptions
70+
? ref.current.onSnapshot(
71+
options.snapshotListenOptions,
72+
setValue,
73+
setError
74+
)
75+
: ref.current.onSnapshot(setValue, setError);
3676

37-
return () => {
38-
listener();
39-
};
77+
return () => {
78+
listener();
79+
};
80+
} else {
81+
ref.current
82+
.get(options ? options.getOptions : undefined)
83+
.then(setValue)
84+
.catch(setError);
85+
}
4086
}, [ref.current]);
4187

4288
const resArray: DocumentHook<T> = [
@@ -47,22 +93,22 @@ export const useDocument = <T = firebase.firestore.DocumentData>(
4793
return useMemo(() => resArray, resArray);
4894
};
4995

50-
export const useDocumentData = <
96+
const useDocumentDataInternal = <
5197
T = firebase.firestore.DocumentData,
5298
IDField extends string = '',
5399
RefField extends string = ''
54100
>(
101+
listen: boolean,
55102
docRef?: firebase.firestore.DocumentReference<T> | null,
56103
options?: DataOptions
57104
): DocumentDataHook<T, IDField, RefField> => {
58105
const idField = options ? options.idField : undefined;
59106
const refField = options ? options.refField : undefined;
60-
const snapshotListenOptions = options
61-
? options.snapshotListenOptions
62-
: undefined;
63-
const [snapshot, loading, error] = useDocument<T>(docRef, {
64-
snapshotListenOptions,
65-
});
107+
const [snapshot, loading, error] = useDocumentInternal<T>(
108+
listen,
109+
docRef,
110+
options
111+
);
66112
const value = useMemo(
67113
() =>
68114
(snapshot

0 commit comments

Comments
 (0)