Debounce map fetch and cap feed size

This commit is contained in:
Your Name 2025-12-26 01:01:39 -05:00
parent 903622e523
commit 5685668d18
1 changed files with 19 additions and 5 deletions

View File

@ -113,8 +113,8 @@ function getPostTimeMs(p) {
return d.getTime(); return d.getTime();
} }
const MAX_POOL_POSTS = 150; const MAX_POOL_POSTS = 120;
const MAX_VISIBLE_POSTS = 90; const MAX_VISIBLE_POSTS = 70;
function prunePosts(posts, center, maxCount) { function prunePosts(posts, center, maxCount) {
if (!Array.isArray(posts) || posts.length <= maxCount) return posts || []; if (!Array.isArray(posts) || posts.length <= maxCount) return posts || [];
@ -151,6 +151,8 @@ export function usePostsEngine({
const allPostsRef = useRef([]); const allPostsRef = useRef([]);
const lastFetchRef = useRef({ center: null, radiusKm: null, filterKey: "", timeFilter: "", bounds: null }); const lastFetchRef = useRef({ center: null, radiusKm: null, filterKey: "", timeFilter: "", bounds: null });
const delayedFetchRef = useRef(null); const delayedFetchRef = useRef(null);
const moveDebounceRef = useRef(null);
const initialLoadRef = useRef(true);
const autoWidenRef = useRef(false); const autoWidenRef = useRef(false);
const [mapReady, setMapReady] = useState(false); const [mapReady, setMapReady] = useState(false);
const clusterModeRef = useRef(false); const clusterModeRef = useRef(false);
@ -656,7 +658,7 @@ export function usePostsEngine({
lat, lat,
lon: lng, lon: lng,
radius_km: radiusKm, radius_km: radiusKm,
limit: 50, limit: 40,
bounds, bounds,
username: username || undefined, username: username || undefined,
}); });
@ -725,9 +727,21 @@ export function usePostsEngine({
} }
} }
load(); const delay = initialLoadRef.current ? 0 : 2400;
initialLoadRef.current = false;
if (moveDebounceRef.current) {
clearTimeout(moveDebounceRef.current);
moveDebounceRef.current = null;
}
moveDebounceRef.current = setTimeout(() => {
if (!cancelled) load();
}, delay);
return () => { return () => {
cancelled = true; cancelled = true;
if (moveDebounceRef.current) {
try { clearTimeout(moveDebounceRef.current); } catch {}
moveDebounceRef.current = null;
}
if (delayedFetchRef.current) { if (delayedFetchRef.current) {
try { clearTimeout(delayedFetchRef.current); } catch {} try { clearTimeout(delayedFetchRef.current); } catch {}
delayedFetchRef.current = null; delayedFetchRef.current = null;
@ -755,7 +769,7 @@ export function usePostsEngine({
lat, lat,
lon, lon,
radius_km: viewParams?.radiusKm, radius_km: viewParams?.radiusKm,
limit: 80, limit: 60,
}); });
if (cancelled) return; if (cancelled) return;
const posts = Array.isArray(data?.posts) ? data.posts : []; const posts = Array.isArray(data?.posts) ? data.posts : [];