Skip to content

Commit 31cb0d8

Browse files
committed
throttle map sync [this time AI was good]
I pointed AI to the previous throttle implementation and this time it gave me solution on first time.
1 parent ad50b4d commit 31cb0d8

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/components/Map.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { MapState } from "../types";
88
import { MAP_SOURCES } from "../constants/mapSources";
99
import { useApp } from "../contexts/AppContext";
1010
import { SourceSpecification } from "maplibre-gl";
11+
import { useCallback } from "react";
12+
import throttle from "lodash.throttle";
1113

1214
interface MapProps {
1315
mapState: MapState;
@@ -27,6 +29,21 @@ export function Map({
2729
const { state } = useApp();
2830
const source = MAP_SOURCES[sourceId] || state.customSources[sourceId];
2931

32+
// Create throttled callbacks
33+
const throttledOnMapChange = useCallback(
34+
throttle((newState: Partial<MapState>) => {
35+
onMapChange(newState);
36+
}, 500),
37+
[onMapChange]
38+
);
39+
40+
const throttledOnViewStateChange = useCallback(
41+
throttle((newState: Partial<MapState>) => {
42+
onViewStateChange(newState);
43+
}, 500),
44+
[onViewStateChange]
45+
);
46+
3047
function handleMove(evt: ViewStateChangeEvent) {
3148
const newState: Partial<MapState> = {
3249
center: [evt.viewState.longitude, evt.viewState.latitude] as [
@@ -39,9 +56,9 @@ export function Map({
3956
};
4057

4158
if (synchronized) {
42-
onMapChange(newState);
59+
throttledOnMapChange(newState);
4360
} else {
44-
onViewStateChange(newState);
61+
throttledOnViewStateChange(newState);
4562
}
4663
}
4764

0 commit comments

Comments
 (0)