Skip to content

Commit

Permalink
🩹 fix(makeObservedQueryHook): Move updates to useEffect.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Jul 23, 2024
1 parent 91aec18 commit 8b03ac7
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions imports/api/makeObservedQueryHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {type DependencyList, useEffect, useRef} from 'react';
import {type DependencyList, useEffect, useRef, useState} from 'react';

import useForceUpdate from '../ui/hooks/useForceUpdate';
import useChanged from '../ui/hooks/useChanged';

import type ObservedQueryCacheCollection from './ObservedQueryCacheCollection';
Expand All @@ -19,42 +18,30 @@ const makeObservedQueryHook =
publication: Publication<[string, UserQuery<T>, ObserveOptions | null]>,
): GenericQueryHook<T> =>
(query: UserQuery<T>, deps: DependencyList) => {
const loading = useRef<boolean>(true);
const results = useRef<any[]>([]);
const dirty = useRef<boolean>(false);
const [loading, setLoading] = useState<boolean>(true);
const [results, setResults] = useState<any[]>([]);
const [dirty, setDirty] = useState<boolean>(false);
const handleRef = useRef<SubscriptionHandle | null>(null);
const forceUpdate = useForceUpdate();

const effectWillTrigger = useChanged(deps);

if (effectWillTrigger) {
// This is to make sure we return the correct values on first
// render.
// TODO Find a better way to do this. It may cause problems in
// future concurrent mode.
dirty.current = false;
loading.current = true;
}

useEffect(() => {
dirty.current = false;
loading.current = true;
setDirty(false);
setLoading(true);

const timestamp = Date.now();
const key = JSON.stringify({timestamp, query});
const handle = subscribe(publication, key, query, null, {
onStop() {
if (handleRef.current === handle) {
dirty.current = true;
loading.current = false;
forceUpdate();
setDirty(true);
setLoading(false);
}
},
onReady() {
if (handleRef.current === handle) {
results.current = findOneSync(Collection, {key})?.results ?? [];
loading.current = false;
forceUpdate();
setResults(findOneSync(Collection, {key})?.results ?? []);
setLoading(false);
}
},
});
Expand All @@ -66,9 +53,9 @@ const makeObservedQueryHook =
}, deps);

return {
loading: loading.current,
results: results.current,
dirty: dirty.current,
loading: effectWillTrigger || loading,
results,
dirty: !effectWillTrigger && dirty,
};
};

Expand Down

0 comments on commit 8b03ac7

Please sign in to comment.