@@ -26,6 +26,57 @@ function createMarkerClusterGroup(props, context) {
2626 const clusterEvent = `cluster${ eventAsProp . substring ( 2 ) . toLowerCase ( ) } ` ;
2727 markerClusterGroup . on ( clusterEvent , callback ) ;
2828 } ) ;
29+ const proto = L . MarkerClusterGroup . prototype ;
30+ let addBuffer = [ ] ;
31+ let removeBuffer = [ ] ;
32+ let flushScheduled = false ;
33+ function flush ( ) {
34+ flushScheduled = false ;
35+ const netOps = /* @__PURE__ */ new Map ( ) ;
36+ for ( const l of addBuffer ) netOps . set ( l , ( netOps . get ( l ) ?? 0 ) + 1 ) ;
37+ for ( const l of removeBuffer ) netOps . set ( l , ( netOps . get ( l ) ?? 0 ) - 1 ) ;
38+ const toAdd = [ ] ;
39+ const toRemove = [ ] ;
40+ for ( const [ layer , count ] of netOps ) {
41+ if ( count > 0 ) toAdd . push ( layer ) ;
42+ else if ( count < 0 ) toRemove . push ( layer ) ;
43+ }
44+ removeBuffer = [ ] ;
45+ addBuffer = [ ] ;
46+ if ( toRemove . length > 0 ) markerClusterGroup . removeLayers ( toRemove ) ;
47+ if ( toAdd . length > 0 ) markerClusterGroup . addLayers ( toAdd ) ;
48+ }
49+ function scheduleFlush ( ) {
50+ if ( flushScheduled ) return ;
51+ flushScheduled = true ;
52+ queueMicrotask ( flush ) ;
53+ }
54+ markerClusterGroup . addLayer = function ( layer ) {
55+ if ( ! this . _map ) return proto . addLayer . call ( this , layer ) ;
56+ addBuffer . push ( layer ) ;
57+ scheduleFlush ( ) ;
58+ return this ;
59+ } ;
60+ markerClusterGroup . removeLayer = function ( layer ) {
61+ if ( ! this . _map ) return proto . removeLayer . call ( this , layer ) ;
62+ removeBuffer . push ( layer ) ;
63+ scheduleFlush ( ) ;
64+ return this ;
65+ } ;
66+ const originalClearLayers = markerClusterGroup . clearLayers ;
67+ markerClusterGroup . clearLayers = function ( ) {
68+ addBuffer = [ ] ;
69+ removeBuffer = [ ] ;
70+ flushScheduled = false ;
71+ return originalClearLayers . call ( this ) ;
72+ } ;
73+ markerClusterGroup . _moveChild = function ( layer , from , to ) {
74+ ;
75+ layer . _latlng = from ;
76+ proto . removeLayer . call ( this , layer ) ;
77+ layer . _latlng = to ;
78+ proto . addLayer . call ( this , layer ) ;
79+ } ;
2980 return createElementObject (
3081 markerClusterGroup ,
3182 extendContext ( context , { layerContainer : markerClusterGroup } )
0 commit comments