Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
"!src/**/__tests__/**",
"!src/shims-*.ts",
"!src/themes/**",
"!src/components/DebouncedMarkerClusterer.ts",
],
coverageThreshold: {
global: {
Expand Down
3 changes: 3 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tools]
node = "20"
pnpm = "10"
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"@googlemaps/js-api-loader": "^1.16.2",
"@googlemaps/markerclusterer": "^2.4.0",
"debounce": "^2.2.0",
"fast-deep-equal": "^3.1.3"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions src/components/DebouncedMarkerClusterer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { MarkerClusterer, MarkerClustererOptions } from "@googlemaps/markerclusterer";
import debounce from "debounce";

export class DebouncedMarkerClusterer extends MarkerClusterer {
private readonly debouncedRender: (() => void) & { clear(): void };

constructor(options: MarkerClustererOptions, debounceDelay = 10) {
super(options);

this.debouncedRender = debounce(() => {
super.render();
}, debounceDelay);
}

addMarker(marker: google.maps.Marker | google.maps.marker.AdvancedMarkerElement, noDraw?: boolean): void {
super.addMarker(marker, true);
if (!noDraw) {
this.debouncedRender();
}
}

removeMarker(marker: google.maps.Marker | google.maps.marker.AdvancedMarkerElement, noDraw?: boolean): boolean {
const result = super.removeMarker(marker, true);
if (!noDraw) {
this.debouncedRender();
}
return result;
}

addMarkers(markers: (google.maps.Marker | google.maps.marker.AdvancedMarkerElement)[], noDraw?: boolean): void {
super.addMarkers(markers, true);
if (!noDraw) {
this.debouncedRender();
}
}

removeMarkers(markers: (google.maps.Marker | google.maps.marker.AdvancedMarkerElement)[], noDraw?: boolean): boolean {
const result = super.removeMarkers(markers, true);
if (!noDraw) {
this.debouncedRender();
}
return result;
}

clearMarkers(noDraw?: boolean): void {
super.clearMarkers(true);
if (!noDraw) {
this.debouncedRender();
}
}

render(): void {
this.debouncedRender.clear();
super.render();
}

destroy(): void {
this.debouncedRender.clear();
}
}
13 changes: 11 additions & 2 deletions src/components/MarkerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
SuperClusterViewportAlgorithm,
} from "@googlemaps/markerclusterer";
import { mapSymbol, apiSymbol, markerClusterSymbol } from "../shared/index";
import { DebouncedMarkerClusterer } from "./DebouncedMarkerClusterer";

export interface IMarkerClusterExposed {
markerCluster: Ref<MarkerClusterer | undefined>;
Expand All @@ -20,6 +21,10 @@
type: Object as PropType<MarkerClustererOptions>,
default: () => ({}),
},
renderDebounceDelay: {
type: Number,
default: 10,
},
},
emits: markerClusterEvents,
setup(props, { emit, expose, slots }) {
Expand All @@ -34,13 +39,13 @@
() => {
if (map.value) {
markerCluster.value = markRaw(
new MarkerClusterer({
new DebouncedMarkerClusterer({

Check warning on line 42 in src/components/MarkerCluster.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

Insert `⏎··············`
map: map.value,

Check warning on line 43 in src/components/MarkerCluster.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

Insert `··`
// Better perf than the default `SuperClusterAlgorithm`. See:

Check warning on line 44 in src/components/MarkerCluster.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

Insert `··`
// https://github.com/googlemaps/js-markerclusterer/pull/640

Check warning on line 45 in src/components/MarkerCluster.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

Insert `··`
algorithm: new SuperClusterViewportAlgorithm(props.options.algorithmOptions ?? {}),

Check warning on line 46 in src/components/MarkerCluster.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

Insert `··`
...props.options,

Check warning on line 47 in src/components/MarkerCluster.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

Insert `··`
})
}, props.renderDebounceDelay)

Check warning on line 48 in src/components/MarkerCluster.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

Replace `············},·props.renderDebounceDelay` with `··············},⏎··············props.renderDebounceDelay⏎············`
);

markerClusterEvents.forEach((event) => {
Expand All @@ -58,6 +63,10 @@
api.value?.event.clearInstanceListeners(markerCluster.value);
markerCluster.value.clearMarkers();
markerCluster.value.setMap(null);

if (markerCluster.value instanceof DebouncedMarkerClusterer) {
markerCluster.value.destroy();
}
}
});

Expand Down
29 changes: 29 additions & 0 deletions src/components/__tests__/MarkerCluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ jest.mock("@googlemaps/markerclusterer", () => {
};
});

let debouncedMarkerClustererConstructorSpy: jest.Mock | undefined;

jest.mock("../DebouncedMarkerClusterer", () => {
return {
DebouncedMarkerClusterer: class {
addListener = jest.fn();
clearMarkers = jest.fn();
setMap = jest.fn();
private debouncedRender: any;

constructor(options: MarkerClustererOptions) {
if (debouncedMarkerClustererConstructorSpy) {
debouncedMarkerClustererConstructorSpy(options);
}
Object.assign(this, options);
this.debouncedRender = {
clear: jest.fn(),
};
mockMarkerClustererInstances.push(this);
}

destroy() {
this.debouncedRender.clear();
}
},
};
});

describe("MarkerCluster Component", () => {
let mockMap: google.maps.Map;
let mockApi: typeof google.maps;
Expand All @@ -50,6 +78,7 @@ describe("MarkerCluster Component", () => {
mockMap = new Map(null);

createMarkerClustererSpy = jest.fn();
debouncedMarkerClustererConstructorSpy = createMarkerClustererSpy;
createSuperClusterViewportAlgorithmSpy = jest.fn();

(MarkerClusterer as any) = class extends MarkerClusterer {
Expand Down
Loading