-
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(firestore): add useCountFromServer (#147)
- Loading branch information
1 parent
1768b50
commit b04a692
Showing
3 changed files
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { FirestoreError, Query } from "firebase/firestore"; | ||
import type { ValueHookResult } from "../common/types.js"; | ||
import { useOnce } from "../internal/useOnce.js"; | ||
import { isQueryEqual } from "./internal.js"; | ||
|
||
export type UseCountFromServerResult = ValueHookResult<number, FirestoreError>; | ||
|
||
async function getData(stableQuery: Query<unknown>) { | ||
const firestoreModule = await import("firebase/firestore"); | ||
|
||
// @ts-expect-error `getCountFromServer` is only available from 9.11.0 | ||
const { getCountFromServer } = firestoreModule; | ||
|
||
const snap = await getCountFromServer(stableQuery); | ||
return snap.data().count; | ||
} | ||
|
||
/** | ||
* Returns the number of documents in the result set of of a Firestore Query. Does not update the count once initially calculated. | ||
* | ||
* Requires firebase 9.11.0 or later | ||
* | ||
* @param {Query<unknown> | undefined | null} query Firestore query whose result set size is calculated | ||
* @returns {UseCountFromServerResult} Size of the result set, loading state, and error | ||
* * value: Size of the result set; `undefined` if the result set size is currently being calculated, or an error occurred | ||
* * loading: `true` while calculating the result size set; `false` if the result size set was calculated successfully or an error occurred | ||
* * error: `undefined` if no error occurred | ||
*/ | ||
export function useCountFromServer(query: Query<unknown> | undefined | null): UseCountFromServerResult { | ||
return useOnce(query ?? undefined, getData, isQueryEqual); | ||
} |