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