File tree Expand file tree Collapse file tree 1 file changed +19
-2
lines changed
Expand file tree Collapse file tree 1 file changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ import { MapState } from "../types";
88import { MAP_SOURCES } from "../constants/mapSources" ;
99import { useApp } from "../contexts/AppContext" ;
1010import { SourceSpecification } from "maplibre-gl" ;
11+ import { useCallback } from "react" ;
12+ import throttle from "lodash.throttle" ;
1113
1214interface 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
You can’t perform that action at this time.
0 commit comments