Prevent stale fetches on filter change
This commit is contained in:
parent
8fca9af710
commit
df242014bc
|
|
@ -376,6 +376,8 @@ export function usePostsEngine({
|
|||
}) {
|
||||
const allPostsRef = useRef([]);
|
||||
const lastFetchRef = useRef({ center: null, radiusKm: null, filterKey: "", timeFilter: "", bounds: null });
|
||||
const pendingFilterRef = useRef("");
|
||||
const requestSeqRef = useRef(0);
|
||||
const delayedFetchRef = useRef(null);
|
||||
const moveDebounceRef = useRef(null);
|
||||
const analyticsDebounceRef = useRef(0);
|
||||
|
|
@ -431,6 +433,7 @@ export function usePostsEngine({
|
|||
useEffect(() => {
|
||||
const map = mapRef.current;
|
||||
replacePoolRef.current = true;
|
||||
pendingFilterRef.current = `${mainFilter}|${subFilter}|${timeFilter}`;
|
||||
if (map) {
|
||||
map.__swForceFetchAt = Date.now();
|
||||
setTimeout(() => {
|
||||
|
|
@ -929,6 +932,7 @@ export function usePostsEngine({
|
|||
|
||||
// On filter/search changes: recompute + sync (NO clear-all rebuild)
|
||||
useEffect(() => {
|
||||
if (pendingFilterRef.current) return;
|
||||
refreshVisibleAndMarkers(timeFilter);
|
||||
}, [timeFilter, mainFilter, subFilter, searchQuery, clusterFocus, refreshVisibleAndMarkers]);
|
||||
|
||||
|
|
@ -981,6 +985,8 @@ export function usePostsEngine({
|
|||
const [lng, lat] = resolvedView.center;
|
||||
const radiusKm = resolvedView.radiusKm || 750;
|
||||
const filterKey = `${mainFilter}|${subFilter}`;
|
||||
const currentKey = `${filterKey}|${timeFilter}`;
|
||||
const reqId = ++requestSeqRef.current;
|
||||
let bounds = null;
|
||||
try {
|
||||
const b = mapObj.getBounds();
|
||||
|
|
@ -1060,7 +1066,7 @@ export function usePostsEngine({
|
|||
sub_category: subCatParam || undefined,
|
||||
});
|
||||
|
||||
if (cancelled) return;
|
||||
if (cancelled || reqId !== requestSeqRef.current) return;
|
||||
|
||||
const existing = replacePoolRef.current ? [] : allPostsRef.current || [];
|
||||
const byId = new Map();
|
||||
|
|
@ -1087,6 +1093,9 @@ export function usePostsEngine({
|
|||
allPostsRef.current = prunePosts(Array.from(byId.values()), [lng, lat], MAX_POOL_POSTS, bounds);
|
||||
replacePoolRef.current = false;
|
||||
lastFetchRef.current = { center: [...resolvedView.center], radiusKm, filterKey, timeFilter, bounds };
|
||||
if (pendingFilterRef.current === currentKey) {
|
||||
pendingFilterRef.current = "";
|
||||
}
|
||||
|
||||
// Smooth update: sync markers/clusters + wall without clearing everything
|
||||
refreshVisibleAndMarkers(timeFilter);
|
||||
|
|
|
|||
Loading…
Reference in New Issue