-
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.
- Loading branch information
1 parent
a4a31f3
commit e202767
Showing
11 changed files
with
165 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
|
||
# modules | ||
/auth/ | ||
/database/ | ||
/firestore/ | ||
/storage/ |
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,2 @@ | ||
export * from './useObject'; | ||
export * from './useObjectOnce'; |
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,18 @@ | ||
import { Query } from "firebase/database"; | ||
import { useStableValue } from "../util/useStableValue"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
function isQueryEqual(a: Query | undefined, b: Query | undefined): boolean { | ||
const areBothUndefined = a === undefined || b === undefined; | ||
const areSameRef = a !== undefined && b !== undefined && a.isEqual(b); | ||
return areBothUndefined || areSameRef; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function useStableQuery(query: Query | undefined): Query | undefined { | ||
return useStableValue(query, 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,42 @@ | ||
import { DataSnapshot, get, Query } from "firebase/database"; | ||
import { useEffect, useMemo } from "react"; | ||
import { ValueHookResult } from "../common"; | ||
import { useIsMounted } from "../util/useIsMounted"; | ||
import { useLoadingValue } from "../util/useLoadingValue"; | ||
import { useStableQuery } from "./internal"; | ||
|
||
export type UseObjectOnceResult = ValueHookResult<DataSnapshot, Error>; | ||
|
||
export function useObjectOnce(query: Query | undefined | null): UseObjectOnceResult { | ||
const isMounted = useIsMounted(); | ||
const { error, loading, setLoading, setError, setValue, value } = useLoadingValue<DataSnapshot, Error>(); | ||
|
||
const stableQuery = useStableQuery(query ?? undefined); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
if (stableQuery === undefined) { | ||
setValue(); | ||
} else { | ||
setLoading(); | ||
|
||
try { | ||
const snap = await get(stableQuery); | ||
if (!isMounted.current) { | ||
return; | ||
} | ||
|
||
setValue(snap); | ||
} catch (e) { | ||
if (!isMounted.current) { | ||
return; | ||
} | ||
|
||
setError(e as Error); | ||
} | ||
} | ||
})(); | ||
}, [stableQuery]); | ||
|
||
return useMemo(() => [value, loading, error], [value, loading, error]); | ||
} |
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,26 @@ | ||
import { DataSnapshot, onValue, Query } from "firebase/database"; | ||
import { useEffect, useMemo } from "react"; | ||
import { ValueHookResult } from "../common"; | ||
import { useLoadingValue } from "../util/useLoadingValue"; | ||
import { useStableQuery } from "./internal"; | ||
|
||
export type UseObjectResult = ValueHookResult<DataSnapshot, Error>; | ||
|
||
export function useObject(query: Query | undefined | null): UseObjectResult { | ||
const { error, loading, setLoading, setError, setValue, value } = useLoadingValue<DataSnapshot, Error>(); | ||
|
||
const stableQuery = useStableQuery(query ?? undefined); | ||
|
||
useEffect(() => { | ||
if (stableQuery === undefined) { | ||
setValue(); | ||
} else { | ||
setLoading(); | ||
|
||
const unsubscribe = onValue(stableQuery, setValue, setError); | ||
return () => unsubscribe(); | ||
} | ||
}, [stableQuery]); | ||
|
||
return useMemo(() => [value, loading, error], [value, loading, error]); | ||
} |
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,26 @@ | ||
import { onValue, Query } from "firebase/database"; | ||
import { useEffect, useMemo } from "react"; | ||
import { ValueHookResult } from "../common"; | ||
import { useLoadingValue } from "../util/useLoadingValue"; | ||
import { useStableQuery } from "./internal"; | ||
|
||
export type UseObjectValueResult<Value = unknown> = ValueHookResult<Value, Error>; | ||
|
||
export function useObjectValue<Value = unknown>(query: Query | undefined | null): UseObjectValueResult<Value> { | ||
const { error, loading, setLoading, setError, setValue, value } = useLoadingValue<Value, Error>(); | ||
|
||
const stableQuery = useStableQuery(query ?? undefined); | ||
|
||
useEffect(() => { | ||
if (stableQuery === undefined) { | ||
setValue(); | ||
} else { | ||
setLoading(); | ||
|
||
const unsubscribe = onValue(stableQuery, (snap) => setValue(snap.val()), setError); | ||
return () => unsubscribe(); | ||
} | ||
}, [stableQuery]); | ||
|
||
return useMemo(() => [value, loading, error], [value, loading, error]); | ||
} |
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,42 @@ | ||
import { get, Query } from "firebase/database"; | ||
import { useEffect, useMemo } from "react"; | ||
import { ValueHookResult } from "../common"; | ||
import { useIsMounted } from "../util/useIsMounted"; | ||
import { useLoadingValue } from "../util/useLoadingValue"; | ||
import { useStableQuery } from "./internal"; | ||
|
||
export type UseObjectValueOnceResult<Value = unknown> = ValueHookResult<Value, Error>; | ||
|
||
export function useObjectValueOnce<Value = unknown>(query: Query | undefined | null): UseObjectValueOnceResult<Value> { | ||
const isMounted = useIsMounted(); | ||
const { error, loading, setLoading, setError, setValue, value } = useLoadingValue<Value, Error>(); | ||
|
||
const stableQuery = useStableQuery(query ?? undefined); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
if (stableQuery === undefined) { | ||
setValue(); | ||
} else { | ||
setLoading(); | ||
|
||
try { | ||
const snap = await get(stableQuery); | ||
if (!isMounted.current) { | ||
return; | ||
} | ||
|
||
setValue(snap.val()); | ||
} catch (e) { | ||
if (!isMounted.current) { | ||
return; | ||
} | ||
|
||
setError(e as Error); | ||
} | ||
} | ||
})(); | ||
}, [stableQuery]); | ||
|
||
return useMemo(() => [value, loading, error], [value, loading, error]); | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./auth"; | ||
export * from "./common"; | ||
export * from "./database"; | ||
export * from "./firestore"; | ||
export * from "./storage"; | ||
|
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