Skip to content

Commit

Permalink
fix(firestore): remove GetOptions
Browse files Browse the repository at this point in the history
`source` is now not nested under `getOptions` anymore but can be passed
in directly as a hook option
  • Loading branch information
andipaetzold committed Oct 15, 2021
1 parent cb24bc4 commit cd08bd3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/firestore/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
refEqual,
} from "firebase/firestore";
import { useStableValue } from "../util/useStableValue";
import { Source } from "./types";
import type { Source } from "./types";

/**
* @internal
Expand Down
13 changes: 8 additions & 5 deletions src/firestore/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export type Source = "default" | "server" | "cache"

export interface GetOptions {
source?: Source;
}
/**
* Specifies how the document or query should be fetched
*
* * `default`: Attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached.
* * `server`: Reads the document/query from the server. Returns an error if the network is not available.
* * `cache`: Reads the document/query from cache. Returns an error if the document/query is not currently cached.
*/
export type Source = "default" | "server" | "cache";
6 changes: 3 additions & 3 deletions src/firestore/useCollectionDataOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import type { ValueHookResult } from "../common/types";
import { useIsMounted } from "../util/useIsMounted";
import { useLoadingValue } from "../util/useLoadingValue";
import { getDocsFromSource, useStableQuery } from "./internal";
import type { GetOptions } from "./types";
import type { Source } from "./types";

export type UseCollectionDataOnceResult<Value extends DocumentData = DocumentData> = ValueHookResult<
Value[],
FirestoreError
>;

export interface UseCollectionDataOnceOptions {
getOptions?: GetOptions;
source?: Source;
snapshotOptions?: SnapshotOptions;
}

export function useCollectionDataOnce<Value extends DocumentData = DocumentData>(
query: Query<Value> | undefined | null,
{ getOptions: { source = "default" } = {}, snapshotOptions = {} }: UseCollectionDataOnceOptions = {}
{ source = "default", snapshotOptions = {} }: UseCollectionDataOnceOptions = {}
): UseCollectionDataOnceResult<Value> {
const isMounted = useIsMounted();
const { value, setValue, loading, setLoading, error, setError } = useLoadingValue<Value[], FirestoreError>();
Expand Down
8 changes: 4 additions & 4 deletions src/firestore/useCollectionOnce.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { DocumentData, FirestoreError, Query, QuerySnapshot, SnapshotOptions } from "firebase/firestore";
import { DocumentData, FirestoreError, Query, QuerySnapshot } from "firebase/firestore";
import { useEffect, useMemo } from "react";
import type { ValueHookResult } from "../common/types";
import { useIsMounted } from "../util/useIsMounted";
import { useLoadingValue } from "../util/useLoadingValue";
import { getDocsFromSource, useStableQuery } from "./internal";
import type { GetOptions } from "./types";
import type { Source } from "./types";

export type UseCollectionOnceResult<Value extends DocumentData = DocumentData> = ValueHookResult<
QuerySnapshot<Value>,
FirestoreError
>;

export interface UseCollectionOnceOptions {
getOptions?: GetOptions;
source?: Source;
}

export function useCollectionOnce<Value extends DocumentData = DocumentData>(
query: Query<Value> | undefined | null,
{ getOptions: { source = "default" } = {} }: UseCollectionOnceOptions = {}
{ source = "default" }: UseCollectionOnceOptions = {}
): UseCollectionOnceResult<Value> {
const isMounted = useIsMounted();
const { value, setValue, loading, setLoading, error, setError } = useLoadingValue<
Expand Down
6 changes: 3 additions & 3 deletions src/firestore/useDocumentDataOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import type { ValueHookResult } from "../common/types";
import { useIsMounted } from "../util/useIsMounted";
import { useLoadingValue } from "../util/useLoadingValue";
import { getDocFromSource, useStableDocRef } from "./internal";
import type { GetOptions } from "./types";
import type { Source } from "./types";

export type UseDocumentDataOnceResult<Value extends DocumentData = DocumentData> = ValueHookResult<Value, FirestoreError>;

export interface UseDocumentDataOnceOptions {
getOptions?: GetOptions;
source?: Source;
snapshotOptions?: SnapshotOptions;
}

export function useDocumentDataOnce<Value extends DocumentData = DocumentData>(
reference: DocumentReference<Value> | undefined | null,
{ getOptions: { source = "default" } = {}, snapshotOptions }: UseDocumentDataOnceOptions = {}
{ source = "default", snapshotOptions }: UseDocumentDataOnceOptions = {}
): UseDocumentDataOnceResult<Value> {
const isMounted = useIsMounted();
const { value, setValue, loading, setLoading, error, setError } = useLoadingValue<Value, FirestoreError>();
Expand Down
6 changes: 3 additions & 3 deletions src/firestore/useDocumentOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import type { ValueHookResult } from "../common/types";
import { useIsMounted } from "../util/useIsMounted";
import { useLoadingValue } from "../util/useLoadingValue";
import { getDocFromSource, useStableDocRef } from "./internal";
import type { GetOptions } from "./types";
import type { Source } from "./types";

export type UseDocumentOnceResult<Value extends DocumentData = DocumentData> = ValueHookResult<
DocumentSnapshot<Value>,
FirestoreError
>;

export interface UseDocumentOnceOptions {
getOptions?: GetOptions;
source?: Source;
}

export function useDocumentOnce<Value extends DocumentData = DocumentData>(
reference: DocumentReference<Value> | undefined | null,
{ getOptions: { source = "default" } = {} }: UseDocumentOnceOptions = {}
{ source = "default" }: UseDocumentOnceOptions = {}
): UseDocumentOnceResult<Value> {
const isMounted = useIsMounted();
const { value, setValue, loading, setLoading, error, setError } = useLoadingValue<
Expand Down

0 comments on commit cd08bd3

Please sign in to comment.