Skip to content

Commit 24f34f6

Browse files
committed
Add Firestore Once listeners
1 parent 9b18357 commit 24f34f6

File tree

7 files changed

+132
-6
lines changed

7 files changed

+132
-6
lines changed

firestore/index.js.flow

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type {
33
DocumentReference,
44
DocumentSnapshot,
5+
GetOptions,
56
Query,
67
QuerySnapshot,
78
SnapshotListenOptions,
@@ -32,15 +33,35 @@ declare export function useCollection(
3233
query?: Query | null,
3334
options?: SnapshotListenOptions
3435
): CollectionHook;
35-
declare export function useCollectionData(
36+
declare export function useCollectionOnce(
3637
query?: Query | null,
38+
options?: GetOptions
39+
): CollectionHook;
40+
declare export function useCollectionData<T>(
41+
query?: Query | null,
42+
options?: SnapshotListenOptions,
3743
idField?: string
38-
): CollectionDataHook;
44+
): CollectionDataHook<T>;
45+
declare export function useCollectionDataOnce<T>(
46+
query?: Query | null,
47+
options?: GetOptions,
48+
idField?: string
49+
): CollectionDataHook<T>;
3950
declare export function useDocument(
4051
ref?: DocumentReference | null,
4152
options?: SnapshotListenOptions
4253
): DocumentHook;
43-
declare export function useDocumentData(
54+
declare export function useDocumentOnce(
4455
ref?: DocumentReference | null,
45-
idField?: string
56+
options?: GetOptions
4657
): DocumentHook;
58+
declare export function useDocumentData<T>(
59+
ref?: DocumentReference | null,
60+
options?: SnapshotListenOptions,
61+
idField?: string
62+
): DocumentDataHook<T>;
63+
declare export function useDocumentDataOnce<T>(
64+
ref?: DocumentReference | null,
65+
options?: GetOptions,
66+
idField?: string
67+
): DocumentDataHook<T>;

firestore/useCollectionData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export type CollectionDataHook<T> = {
1010

1111
export default <T>(
1212
query?: firestore.Query | null,
13+
options?: firestore.SnapshotListenOptions,
1314
idField?: string
1415
): CollectionDataHook<T> => {
15-
const { error, loading, value } = useCollection(query);
16+
const { error, loading, value } = useCollection(query, options);
1617
return {
1718
error,
1819
loading,

firestore/useCollectionDataOnce.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { firestore } from 'firebase';
2+
import useCollectionOnce from './useCollectionOnce';
3+
import { snapshotToData } from './helpers';
4+
import { CollectionDataHook } from './useCollectionData';
5+
6+
export default <T>(
7+
query?: firestore.Query | null,
8+
options?: firestore.GetOptions,
9+
idField?: string
10+
): CollectionDataHook<T> => {
11+
const { error, loading, value } = useCollectionOnce(query, options);
12+
return {
13+
error,
14+
loading,
15+
value: (value
16+
? value.docs.map(doc => snapshotToData(doc, idField))
17+
: undefined) as T[],
18+
};
19+
};

firestore/useCollectionOnce.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { firestore } from 'firebase';
2+
import { useEffect } from 'react';
3+
import { useIsEqualRef, useLoadingValue } from '../util';
4+
import { CollectionHook } from './useCollection';
5+
6+
export default (
7+
query?: firestore.Query | null,
8+
options?: firestore.GetOptions
9+
): CollectionHook => {
10+
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
11+
firestore.QuerySnapshot
12+
>();
13+
const ref = useIsEqualRef(query, reset);
14+
15+
useEffect(
16+
() => {
17+
if (!ref.current) {
18+
setValue(undefined);
19+
return;
20+
}
21+
ref.current
22+
.get(options)
23+
.then(setValue)
24+
.catch(setError);
25+
},
26+
[ref.current]
27+
);
28+
29+
return {
30+
error,
31+
loading,
32+
value,
33+
};
34+
};

firestore/useDocumentData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export type DocumentDataHook<T> = {
1010

1111
export default <T>(
1212
docRef?: firestore.DocumentReference | null,
13+
options?: firestore.SnapshotListenOptions,
1314
idField?: string
1415
): DocumentDataHook<T> => {
15-
const { error, loading, value } = useDocument(docRef);
16+
const { error, loading, value } = useDocument(docRef, options);
1617
return {
1718
error,
1819
loading,

firestore/useDocumentDataOnce.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { firestore } from 'firebase';
2+
import useDocumentOnce from './useDocumentOnce';
3+
import { snapshotToData } from './helpers';
4+
import { DocumentDataHook } from './useDocumentData';
5+
6+
export default <T>(
7+
docRef?: firestore.DocumentReference | null,
8+
idField?: string
9+
): DocumentDataHook<T> => {
10+
const { error, loading, value } = useDocumentOnce(docRef);
11+
return {
12+
error,
13+
loading,
14+
value: (value ? snapshotToData(value, idField) : undefined) as T,
15+
};
16+
};

firestore/useDocumentOnce.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { firestore } from 'firebase';
2+
import { useEffect } from 'react';
3+
import { useIsEqualRef, useLoadingValue } from '../util';
4+
import { DocumentHook } from './useDocument';
5+
6+
export default (
7+
docRef?: firestore.DocumentReference | null,
8+
options?: firestore.GetOptions
9+
): DocumentHook => {
10+
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
11+
firestore.DocumentSnapshot
12+
>();
13+
const ref = useIsEqualRef(docRef, reset);
14+
15+
useEffect(
16+
() => {
17+
if (!ref.current) {
18+
setValue(undefined);
19+
return;
20+
}
21+
ref.current
22+
.get(options)
23+
.then(setValue)
24+
.catch(setError);
25+
},
26+
[ref.current]
27+
);
28+
29+
return {
30+
error,
31+
loading,
32+
value,
33+
};
34+
};

0 commit comments

Comments
 (0)