Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: fix loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ishowta committed Feb 15, 2022
1 parent c233cd6 commit 765ac26
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function useRootQuery<Q extends GraphQuery<{}>>(
query: Q
): [Expand<JoinedDataInner<{}, Q>> | undefined, boolean, Error | undefined] {
const [value, setValue] = useState<Expand<JoinedDataInner<{}, Q>>>();
const [loading, setLoading] = useState(true);
const [error, setError] = useState<FirestoreError>();
const rootListener = useRef<GraphQueryListener>();

Expand All @@ -34,6 +35,7 @@ export function useRootQuery<Q extends GraphQuery<{}>>(
(result) => {
logger.debug('onUpdate', result);
setValue(result.data);
setLoading(false);
}
);
return () => {
Expand All @@ -42,20 +44,23 @@ export function useRootQuery<Q extends GraphQuery<{}>>(
}, []);

// dry run update query and get has loading
const loading = useMemo(() => {
if (rootListener.current) {
const immediateLoading = useMemo(() => {
if (!loading && rootListener.current) {
return rootListener.current.updateQuery(query, true);
}
return true;
return loading;
}, [query]);

useEffect(() => {
if (rootListener.current) {
rootListener.current.updateQuery(query, false);
if (rootListener.current.updateQuery(query, false)) {
logger.debug('queryChanged');
setLoading(true);
}
}
}, [query]);

return [value, loading, error];
return [value, immediateLoading, error];
}

export function useQuery<
Expand Down

0 comments on commit 765ac26

Please sign in to comment.