Skip to content

Commit 89e4a45

Browse files
committed
add buffering test example
1 parent 1480eba commit 89e4a45

4 files changed

Lines changed: 449 additions & 2 deletions

File tree

dist/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 })

examples/vite-example/src/App.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'react-leaflet-cluster/dist/assets/MarkerCluster.Default.css'
77
import SimpleExample from './components/SimpleExample'
88
import TenThousandMarker from './components/TenThousandMarker'
99
import CustomMarkerCluster from './components/CustomMarkerCluster'
10+
import BufferingTest from './components/BufferingTest'
1011
import L from 'leaflet'
1112

1213
// configure the default icon
@@ -16,7 +17,7 @@ L.Icon.Default.mergeOptions({
1617
iconUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon.png',
1718
shadowUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-shadow.png',
1819
})
19-
type ExampleType = 'simple' | 'ten-thousand' | 'custom'
20+
type ExampleType = 'simple' | 'ten-thousand' | 'custom' | 'buffering-test'
2021

2122
export default function App() {
2223
const [currentExample, setCurrentExample] = useState<ExampleType>('simple')
@@ -29,6 +30,8 @@ export default function App() {
2930
return <TenThousandMarker />
3031
case 'custom':
3132
return <CustomMarkerCluster />
33+
case 'buffering-test':
34+
return <BufferingTest />
3235
default:
3336
return <SimpleExample />
3437
}
@@ -70,6 +73,7 @@ export default function App() {
7073
<button
7174
onClick={() => setCurrentExample('custom')}
7275
style={{
76+
marginRight: '10px',
7377
padding: '8px 16px',
7478
backgroundColor: currentExample === 'custom' ? '#007bff' : '#f8f9fa',
7579
color: currentExample === 'custom' ? 'white' : 'black',
@@ -80,6 +84,19 @@ export default function App() {
8084
>
8185
Custom Cluster
8286
</button>
87+
<button
88+
onClick={() => setCurrentExample('buffering-test')}
89+
style={{
90+
padding: '8px 16px',
91+
backgroundColor: currentExample === 'buffering-test' ? '#007bff' : '#f8f9fa',
92+
color: currentExample === 'buffering-test' ? 'white' : 'black',
93+
border: '1px solid #dee2e6',
94+
borderRadius: '4px',
95+
cursor: 'pointer',
96+
}}
97+
>
98+
Buffering Test
99+
</button>
83100
</nav>
84101

85102
{renderExample()}

0 commit comments

Comments
 (0)