-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: useQueriesOnce & useQueriesDataOnce (#239)
- Loading branch information
1 parent
f9c30ea
commit 0380229
Showing
20 changed files
with
229 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { DocumentData, FirestoreError, Query, SnapshotOptions } from "firebase/firestore"; | ||
import { useCallback } from "react"; | ||
import type { ValueHookResult } from "../common/types.js"; | ||
import { useMultiGet } from "../internal/useMultiGet.js"; | ||
import { getDocsFromSource, isQueryEqual } from "./internal.js"; | ||
import type { Source } from "./types.js"; | ||
|
||
export type UseQueriesDataOnceResult<Values extends ReadonlyArray<DocumentData> = ReadonlyArray<DocumentData>> = { | ||
[Index in keyof Values]: ValueHookResult<Values[Index], FirestoreError>; | ||
} & { length: Values["length"] }; | ||
|
||
/** | ||
* Options to configure the subscription | ||
*/ | ||
export interface UseQueriesDataOnceOptions { | ||
source?: Source; | ||
snapshotOptions?: SnapshotOptions; | ||
} | ||
|
||
/** | ||
* Returns the data of multiple Firestore queries. Does not update the data once initially fetched | ||
* @template Values Tuple of types of the collection data | ||
* @param queries Firestore queries that will be fetched | ||
* @param options Options to configure how the queries are fetched | ||
* @returns Array with tuple for each query: | ||
* value: Query data; `undefined` if query is currently being fetched, or an error occurred | ||
* loading: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred | ||
* error: `undefined` if no error occurred | ||
*/ | ||
export function useQueriesDataOnce<Values extends ReadonlyArray<DocumentData> = ReadonlyArray<DocumentData>>( | ||
queries: { [Index in keyof Values]: Query<Values[Index]> }, | ||
options?: UseQueriesDataOnceOptions, | ||
): UseQueriesDataOnceResult<Values> { | ||
const { source = "default", snapshotOptions } = options ?? {}; | ||
const { serverTimestamps } = snapshotOptions ?? {}; | ||
|
||
const getData = useCallback( | ||
async (stableQuery: Query<Values[number]>) => { | ||
const snap = await getDocsFromSource(stableQuery, source); | ||
return snap.docs.map((doc) => doc.data({ serverTimestamps })); | ||
}, | ||
[source, serverTimestamps], | ||
); | ||
|
||
// @ts-expect-error `useMultiGet` assumes a single value type | ||
return useMultiGet(queries, getData, isQueryEqual); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { DocumentData, FirestoreError, Query, SnapshotOptions } from "firebase/firestore"; | ||
import { useCallback } from "react"; | ||
import type { ValueHookResult } from "../common/types.js"; | ||
import { useMultiGet } from "../internal/useMultiGet.js"; | ||
import { getDocsFromSource, isQueryEqual } from "./internal.js"; | ||
import type { Source } from "./types.js"; | ||
|
||
export type UseQueriesOnceResult<Values extends ReadonlyArray<DocumentData> = ReadonlyArray<DocumentData>> = { | ||
[Index in keyof Values]: ValueHookResult<Values[Index], FirestoreError>; | ||
} & { length: Values["length"] }; | ||
|
||
/** | ||
* Options to configure the subscription | ||
*/ | ||
export interface UseQueriesOnceOptions { | ||
source?: Source; | ||
snapshotOptions?: SnapshotOptions; | ||
} | ||
|
||
/** | ||
* Returns the QuerySnapshot of multiple Firestore queries. Does not update the data once initially fetched | ||
* @template Values Tuple of types of the collection data | ||
* @param queries Firestore queries that will be fetched | ||
* @param options Options to configure how the queries are fetched | ||
* @returns Array with tuple for each query: | ||
* value: QuerySnapshot; `undefined` if query is currently being fetched, or an error occurred | ||
* loading: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred | ||
* error: `undefined` if no error occurred | ||
*/ | ||
export function useQueriesOnce<Values extends ReadonlyArray<DocumentData> = ReadonlyArray<DocumentData>>( | ||
queries: { [Index in keyof Values]: Query<Values[Index]> }, | ||
options?: UseQueriesOnceOptions, | ||
): UseQueriesOnceResult<Values> { | ||
const { source = "default" } = options ?? {}; | ||
|
||
const getData = useCallback( | ||
async (stableQuery: Query<Values[number]>) => getDocsFromSource(stableQuery, source), | ||
[source], | ||
); | ||
|
||
// @ts-expect-error `useMultiGet` assumes a single value type | ||
return useMultiGet(queries, getData, isQueryEqual); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.