Skip to content

Commit

Permalink
🩹 fix(makeQuery): Fix loading flicker.
Browse files Browse the repository at this point in the history
By leveraging new generic hook `useQuery`.
  • Loading branch information
make-github-pseudonymous-again committed Jan 1, 2025
1 parent 65ea642 commit 4770e7f
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions imports/api/makeQuery.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import assert from 'assert';

import {type DependencyList} from 'react';

import type PublicationEndpoint from './publication/PublicationEndpoint';
import useSubscription from './publication/useSubscription';
import useCursor from './publication/useCursor';
import useQuery from './publication/useQuery';

import type Collection from './Collection';
import type Document from './Document';
Expand All @@ -15,16 +16,17 @@ const makeQuery =
publication: PublicationEndpoint<[UserQuery<T>]>,
) =>
(query: UserQuery<T> | null, deps: DependencyList) => {
const isLoading = useSubscription(publication, [query], query !== null);
const loadingSubscription = isLoading();
const [selector, options] =
query === null ? [] : queryToSelectorOptionsPair(query);
const {loading: loadingResults, results} = useCursor(
() => (query === null ? null : collection.find(selector, options)),
return useQuery(
publication,
[query],
() => {
assert(query !== null);
const [selector, options] = queryToSelectorOptionsPair(query);
return collection.find(selector, options);
},
deps,
query !== null,
);
const loading = loadingSubscription || loadingResults;
return {loading, results};
};

export default makeQuery;

0 comments on commit 4770e7f

Please sign in to comment.