Skip to content

Add support of nulled references as props of hooks #10

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions database/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const reducer = (state: ReducerState, action: ReducerAction): ReducerState => {
}
};

export default (query: database.Query): ListHook => {
export default (query: database.Query | null | undefined): ListHook => {
const [state, dispatch] = useReducer(reducer, initialState);

const ref = useIsEqualRef(query, () => dispatch({ type: 'reset' }));
Expand Down Expand Up @@ -138,7 +138,11 @@ export default (query: database.Query): ListHook => {

useEffect(
() => {
const query: database.Query = ref.current;
const query: database.Query | null | undefined = ref.current;
if (!query) {
dispatch({ type: 'value' })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neilor Currently this means that when no query is specified, useList will have an empty array as its value. Do you think this is correct? I'm torn as to whether this should be undefined or null as this better represents an absence of a value.

Copy link
Contributor Author

@neilor neilor Apr 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job! yes, I think too.

return;
}
// This is here to indicate that all the data has been successfully received
query.once(
'value',
Expand Down
5 changes: 2 additions & 3 deletions database/useListKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ export type ListKeysHook = {
value: string[];
};

export default (query: database.Query): ListKeysHook => {
export default (query: database.Query | null | undefined): ListKeysHook => {
const { error, loading, value } = useList(query);
// @ts-ignore
return {
error,
loading,
value: value.map(snapshot => snapshot.key),
value: value.map(snapshot => snapshot.key as string),
};
};
2 changes: 1 addition & 1 deletion database/useListVals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ListValsHook<T> = {
};

export default <T>(
query: database.Query,
query: database.Query | null | undefined,
keyField?: string
): ListValsHook<T> => {
const { error, loading, value } = useList(query);
Expand Down
7 changes: 6 additions & 1 deletion database/useObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ObjectHook = {
value?: database.DataSnapshot;
};

export default (query: database.Query): ObjectHook => {
export default (query: database.Query | null | undefined): ObjectHook => {
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
database.DataSnapshot
>();
Expand All @@ -17,6 +17,11 @@ export default (query: database.Query): ObjectHook => {
useEffect(
() => {
const query = ref.current;
if (!query) {
setValue(null);
return;
}

query.on('value', setValue, setError);

return () => {
Expand Down
2 changes: 1 addition & 1 deletion database/useObjectVal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type ObjectValHook<T> = {
value?: T;
};

export default <T>(query: database.Query): ObjectValHook<T> => {
export default <T>(query: database.Query | null | undefined): ObjectValHook<T> => {
const { error, loading, value } = useObject(query);
return {
error,
Expand Down
3 changes: 2 additions & 1 deletion firestore/useCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type CollectionHook = {
};

export default (
query: firestore.Query,
query: firestore.Query | null | undefined,
options?: firestore.SnapshotListenOptions
): CollectionHook => {
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
Expand All @@ -19,6 +19,7 @@ export default (

useEffect(
() => {
if (!ref.current) return;
const listener = options
? ref.current.onSnapshot(options, setValue, setError)
: ref.current.onSnapshot(setValue, setError);
Expand Down
3 changes: 2 additions & 1 deletion firestore/useDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type DocumentHook = {
};

export default (
docRef: firestore.DocumentReference,
docRef: firestore.DocumentReference | null | undefined,
options?: firestore.SnapshotListenOptions
): DocumentHook => {
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
Expand All @@ -19,6 +19,7 @@ export default (

useEffect(
() => {
if (!ref.current) return;
const listener = options
? ref.current.onSnapshot(options, setValue, setError)
: ref.current.onSnapshot(setValue, setError);
Expand Down
14 changes: 8 additions & 6 deletions storage/useDownloadURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ export type DownloadURLHook = {
value?: string;
};

export default (storageRef: storage.Reference): DownloadURLHook => {
export default (storageRef: storage.Reference | null | undefined): DownloadURLHook => {
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
string
>();
const ref = useComparatorRef(
storageRef,
(v1, v2) => v1.fullPath === v2.fullPath,
reset
);
function isEqual(v1: storage.Reference | null | undefined, v2: storage.Reference | null | undefined): boolean {
const bothNull: boolean = !v1 && !v2;
const equal: boolean = !!v1 && !!v2 && v1.fullPath === v2.fullPath;
return bothNull || equal;
}
const ref = useComparatorRef(storageRef, isEqual, reset);

useEffect(
() => {
if (!ref.current) return;
ref.current
.getDownloadURL()
.then(setValue)
Expand Down
21 changes: 13 additions & 8 deletions util/refHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ type RefHook<T> = {
};

export const useComparatorRef = <T>(
value: T,
isEqual: (v1: T, v2: T) => boolean,
value: T | null | undefined,
isEqual: (v1: T | null | undefined, v2: T | null | undefined) => boolean,
onChange?: () => void
): RefHook<T> => {
): RefHook<T | null | undefined> => {
const ref = useRef(value);
useEffect(() => {
if (!isEqual(value, ref.current)) {
Expand All @@ -26,15 +26,20 @@ export interface HasIsEqual<T> {
}

export const useIsEqualRef = <T extends HasIsEqual<T>>(
value: T,
value: T | null | undefined,
onChange?: () => void
): RefHook<T> => {
return useComparatorRef(value, (v1, v2) => v1.isEqual(v2), onChange);
): RefHook<T | null | undefined> => {
function isEqual(v1: T | null | undefined, v2: T | null | undefined): boolean {
const bothNull: boolean = !v1 && !v2;
const equal: boolean = !!v1 && !!v2 && v1.isEqual(v2);
return bothNull || equal;
}
return useComparatorRef(value, isEqual, onChange);
};

export const useIdentifyRef = <T>(
value: T,
value: T | null | undefined,
onChange?: () => void
): RefHook<T> => {
): RefHook<T | null | undefined> => {
return useComparatorRef(value, (v1, v2) => v1 === v2, onChange);
};