Too many OpenLayers moveend triggers when resizing the window

I’m using OpenLayers inside the React environment (background info). I want to control what the map shows with React – using context and other components. For example, zoom in to the map if something is selected in the react components. I am using the latest OpenLayers (10.1) and React(18.3.1), and the problem I have is that the moveEnd event triggers too many times. For a simple window resize, I’m getting 20+ move-end triggers:

My problem is that I’m trying to load new data for the map, and this triggers too many requests against the backend. So I thought that perhaps Moveend was not the best event, but other similar events, like Dragend, also triggered the same number of events.

I’m also thinking, that perhaps in React environment the way I do things is not the best, as map resizes trigger data (re)loads, but data loads also trigger re-renders.

The image is taken before I simplified my whole setup, but currently it’s like that:

'use client';

import { useRef, useState, useCallback, useContext } from 'react';
import { useQuery } from '@tanstack/react-query';

import { Feature } from 'ol';
import { fromLonLat, toLonLat } from 'ol/proj';
import { getBottomLeft, getTopRight } from 'ol/extent';

import { Point } from 'ol/geom';
import { Icon, Style } from 'ol/style';

import { fetcher } from 'utils/axios';
import { MapContext } from 'context/ResizeableMapContext';
import OSMTileLayer from 'components/atoms/OSMTileLayer';
import MapComponent from 'components/atoms/Map';
import VectorLayer from 'components/atoms/VectorLayer';

const AuthenticatedMap = function () {
    const { mapArea, mapObject, setMapObject } = useContext(MapContext);
    const [mapBox, setMapBox] = useState({});

    const onMoveEnd = useCallback((e) => {
        console.log('ON MOVE END TRIGGERS');
        const { map } = e;
        const extent = map.getView().calculateExtent(map.getSize());
        const bottomLeft = toLonLat(getBottomLeft(extent));
        const topRight = toLonLat(getTopRight(extent));
        setMapBox({
            tr: {
                lon: topRight[0],
                lat: topRight[1],
            },
            bl: {
                lon: bottomLeft[0],
                lat: bottomLeft[1],
            },
        });
    }, []);

    const getUrlAndKey = () => {
        const baseUrl = `/api/ships/visible-ships/?`;
        const baseKey = 'k:';
        if ('tr' in mapBox && 'bl' in mapBox) {
            const {
                tr: { lon: trLon, lat: trLat },
                bl: { lon: blLon, lat: blLat },
            } = mapBox;
            return [
                `${baseUrl}min_lon=${blLon}&min_lat=${blLat}&max_lon=${trLon}&max_lat=${trLat}`,
                `${blLon}&${blLat}&${trLon}&${trLat}`,
            ];
        }
        return [baseUrl, baseKey];
    };

    const [url, key] = getUrlAndKey();

    const { isPending, isError, data } = useQuery({
        queryKey: ['publiclyAvailableShips', key],
        refetchInterval: 1000 * 60,
        queryFn: () => fetcher(url),
        enabled: 'tr' in mapBox && 'bl' in mapBox,
    });

    const features = [];
    if (!isPending && !isError && data.length) {
        data.forEach((ship) => {
            const feature = new Feature({
                geometry: new Point(fromLonLat([ship.latest_position.lon, ship.latest_position.lat])),
            });
            feature.setStyle(
                new Style({
                    image: new Icon({
                        src:
                            "data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' " +
                            "width='32' height='32' viewBox='0 0 24 24' fill='%23e5f2fe' stroke='%23000' " +
                            "stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpath " +
                            "d='M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7Z'/%3e%3ccircle " +
                            "cx='12' cy='9' r='3'/%3e%3c/svg%3e",
                        className: 'shadow-xl',
                        scale: 1,
                    }),
                }),
            );
            feature.set('name', ship.name);

            features.push(feature);
        });
    }

    return (
        <MapComponent
            zoom={10}
            center={fromLonLat([24.637865, 59.511668])}
            width={mapArea}
            map={mapObject}
            setMap={setMapObject}
            moveEndCallback={onMoveEnd}
        >
            <OSMTileLayer />
            <VectorLayer>{features}</VectorLayer>
        </MapComponent>
    );
};

export default AuthenticatedMap;

Now, my question is, how can I stop excessive move-end triggers? I mean, I move the window size, like 100 pixels, in 1-2 seconds. That should not trigger the callback 20+ times. Is the solution really to implement my own delay that makes sure the callback is only triggered once per 250ms?

Edit: My current solution is to implement a delay in MapComponent, but is that really the best solution here?

            mapObj.on('moveend', (event) => {
                // Clear previous timeout to reset the delay
                if (moveEndTimeoutRef.current) {
                    clearTimeout(moveEndTimeoutRef.current);
                }

                // Set a new timeout to call the moveEndCallback after 100ms
                moveEndTimeoutRef.current = setTimeout(() => {
                    moveEndCallback(event);
                }, 150); // 150ms delay
            });

All the best,
Alan

I guess OpenLayers is not very popular, or perhaps this site indeed is dying ( where will LLM’s get their information now, I wonder) I’ll post my own answer as “THE” answer:

Add delay to the callback like this:


import { useRef } from 'react';
....

const moveEndTimeoutRef = useRef();

....
            mapObj.on('moveend', (event) => {
                // Clear previous timeout to reset the delay
                if (moveEndTimeoutRef.current) {
                    clearTimeout(moveEndTimeoutRef.current);
                }

                // Set a new timeout to call the moveEndCallback after 100ms
                moveEndTimeoutRef.current = setTimeout(() => {
                    moveEndCallback(event);
                }, 150); // 150ms delay
            });

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật