Don't debounce if loading subsequent posts

This commit is contained in:
Lim Chee Aun 2023-02-02 15:48:12 +08:00
parent 0bb921a6d7
commit eb2f80162a

View file

@ -119,27 +119,27 @@ function Home({ hidden }) {
} }
const loadingStatuses = useRef(false); const loadingStatuses = useRef(false);
const loadStatuses = useDebouncedCallback( const loadStatuses = (firstLoad) => {
(firstLoad) => { if (loadingStatuses.current) return;
if (loadingStatuses.current) return; loadingStatuses.current = true;
loadingStatuses.current = true; setUIState('loading');
setUIState('loading'); (async () => {
(async () => { try {
try { const { done } = await fetchStatuses(firstLoad);
const { done } = await fetchStatuses(firstLoad); setShowMore(!done);
setShowMore(!done); setUIState('default');
setUIState('default'); } catch (e) {
} catch (e) { console.warn(e);
console.warn(e); setUIState('error');
setUIState('error'); } finally {
} finally { loadingStatuses.current = false;
loadingStatuses.current = false; }
} })();
})(); };
}, const debouncedLoadStatuses = useDebouncedCallback(loadStatuses, 3000, {
3000, leading: true,
{ leading: true, trailing: false }, trailing: false,
); });
useEffect(() => { useEffect(() => {
loadStatuses(true); loadStatuses(true);
@ -284,7 +284,7 @@ function Home({ hidden }) {
useEffect(() => { useEffect(() => {
if (reachStart) { if (reachStart) {
loadStatuses(true); debouncedLoadStatuses(true);
} }
}, [reachStart]); }, [reachStart]);
@ -324,7 +324,7 @@ function Home({ hidden }) {
scrollableRef.current?.scrollTo({ top: 0, behavior: 'smooth' }); scrollableRef.current?.scrollTo({ top: 0, behavior: 'smooth' });
}} }}
onDblClick={() => { onDblClick={() => {
loadStatuses(true); debouncedLoadStatuses(true);
}} }}
> >
<div class="header-side"> <div class="header-side">
@ -372,7 +372,7 @@ function Home({ hidden }) {
); );
states.home.unshift(...uniqueHomeNew); states.home.unshift(...uniqueHomeNew);
} }
loadStatuses(true); debouncedLoadStatuses(true);
states.homeNew = []; states.homeNew = [];
scrollableRef.current?.scrollTo({ scrollableRef.current?.scrollTo({
@ -443,7 +443,7 @@ function Home({ hidden }) {
<button <button
type="button" type="button"
onClick={() => { onClick={() => {
loadStatuses(true); debouncedLoadStatuses(true);
}} }}
> >
Try again Try again