Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[layer-maplibre, layer-leaflet] fix issues on clean-up #1483

Merged
merged 1 commit into from
Oct 29, 2024
Merged
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
26 changes: 19 additions & 7 deletions packages/layer-leaflet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Graph from "graphology";
import { Attributes } from "graphology-types";
import L, { MapOptions } from "leaflet";
import { Sigma } from "sigma";
import { DEFAULT_SETTINGS } from "sigma/settings";

import { graphToLatlng, latlngToGraph, setSigmaRatioBounds, syncMapWithSigma, syncSigmaWithMap } from "./utils";

Expand All @@ -22,6 +21,10 @@ export default function bindLeafletLayer(
getNodeLatLng?: (nodeAttributes: Attributes) => { lat: number; lng: number };
},
) {
// Keeping data for the cleanup
let isKilled = false;
const prevSigmaSettings = sigma.getSettings();

// Creating map container
const mapContainer = document.createElement("div");
const mapContainerId = `${sigma.getContainer().id}-map`;
Expand Down Expand Up @@ -109,12 +112,21 @@ export default function bindLeafletLayer(

// Clean up function to remove everything
function clean() {
map.remove();
mapContainer.remove();
sigma.off("afterRender", fnSyncMapWithSigma);
sigma.off("resize", fnOnResize);
sigma.setSetting("stagePadding", DEFAULT_SETTINGS.stagePadding);
sigma.setSetting("enableCameraRotation", true);
if (!isKilled) {
isKilled = true;

map.remove();
mapContainer.remove();

sigma.off("afterRender", fnSyncMapWithSigma);
sigma.off("resize", fnOnResize);

// Reset settings
sigma.setSetting("stagePadding", prevSigmaSettings.stagePadding);
sigma.setSetting("enableCameraRotation", prevSigmaSettings.enableCameraRotation);
sigma.setSetting("minCameraRatio", prevSigmaSettings.minCameraRatio);
sigma.setSetting("maxCameraRatio", prevSigmaSettings.maxCameraRatio);
}
}

// When the map is ready
Expand Down
25 changes: 18 additions & 7 deletions packages/layer-maplibre/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Attributes } from "graphology-types";
import { LngLat, Map, MapOptions } from "maplibre-gl";
import { Sigma } from "sigma";
import { DEFAULT_SETTINGS } from "sigma/settings";

Check warning on line 5 in packages/layer-maplibre/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

'DEFAULT_SETTINGS' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 5 in packages/layer-maplibre/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

'DEFAULT_SETTINGS' is defined but never used. Allowed unused vars must match /^_/u

import { graphToLatlng, latlngToGraph, syncMapWithSigma, syncSigmaWithMap } from "./utils";

Expand All @@ -20,6 +20,10 @@
getNodeLatLng?: (nodeAttributes: Attributes) => { lat: number; lng: number };
},
) {
// Keeping data for the cleanup
let isKilled = false;
const prevSigmaSettings = sigma.getSettings();

// Creating map container
const mapContainer = document.createElement("div");
const mapContainerId = `${sigma.getContainer().id}-map`;
Expand Down Expand Up @@ -91,13 +95,20 @@

// Clean up function to remove everything
function clean() {
map.off("moveend", fnSyncSigmaWithMap);
map.remove();
mapContainer.remove();
sigma.off("afterRender", fnSyncMapWithSigma);
sigma.off("resize", fnOnResize);
sigma.setSetting("stagePadding", DEFAULT_SETTINGS.stagePadding);
sigma.setSetting("enableCameraRotation", true);
if (!isKilled) {
isKilled = true;

map.off("moveend", fnSyncSigmaWithMap);
map.remove();
mapContainer.remove();

sigma.off("afterRender", fnSyncMapWithSigma);
sigma.off("resize", fnOnResize);

// Reset settings
sigma.setSetting("stagePadding", prevSigmaSettings.stagePadding);
sigma.setSetting("enableCameraRotation", prevSigmaSettings.enableCameraRotation);
}
}

// Update the sigma graph for geospatial coords
Expand Down
52 changes: 52 additions & 0 deletions packages/test/unit/layers/leaflet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import bindMapLayer from "@sigma/layer-leaflet";
import Graph from "graphology";
import { SerializedGraph } from "graphology-types";
import Sigma from "sigma";
import { createElement } from "sigma/utils";
import { afterEach, beforeEach, describe, expect, test } from "vitest";

const GRAPH: Pick<SerializedGraph, "nodes" | "edges"> = {
nodes: [
{ key: "nantes", attributes: { x: 0, y: 0, lat: 47.2308, lng: 1.5566, size: 10, label: "Nantes" } },
{ key: "paris", attributes: { x: 0, y: 0, lat: 48.8567, lng: 2.351, size: 10, label: "Paris" } },
],
edges: [{ source: "nantes", target: "paris" }],
};

interface SigmaTestContext {
sigma: Sigma;
}

beforeEach<SigmaTestContext>(async (context) => {
const graph = new Graph();
graph.import(GRAPH);
const container = createElement("div", { width: "100px", height: "100px" });
document.body.append(container);
context.sigma = new Sigma(graph, container);
});

afterEach<SigmaTestContext>(async ({ sigma }) => {
sigma.kill();
sigma.getContainer().remove();
});

describe("@sigma/layer-leaflet", () => {
test<SigmaTestContext>("calling clean function multiple times should work", ({ sigma }) => {
const { clean } = bindMapLayer(sigma);
expect(() => {
clean();
clean();
}).not.to.throw();
});
test<SigmaTestContext>("clean the map should reset sigma'settings to its previous value", ({ sigma }) => {
const prevSettings = sigma.getSettings();

const { clean } = bindMapLayer(sigma);
const settings = sigma.getSettings();

clean();

expect(prevSettings).not.toEqual(settings);
expect(sigma.getSettings()).toEqual(prevSettings);
});
});
52 changes: 52 additions & 0 deletions packages/test/unit/layers/maplibre.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import bindMapLayer from "@sigma/layer-maplibre";
import Graph from "graphology";
import { SerializedGraph } from "graphology-types";
import Sigma from "sigma";
import { createElement } from "sigma/utils";
import { afterEach, beforeEach, describe, expect, test } from "vitest";

const GRAPH: Pick<SerializedGraph, "nodes" | "edges"> = {
nodes: [
{ key: "nantes", attributes: { x: 0, y: 0, lat: 47.2308, lng: 1.5566, size: 10, label: "Nantes" } },
{ key: "paris", attributes: { x: 0, y: 0, lat: 48.8567, lng: 2.351, size: 10, label: "Paris" } },
],
edges: [{ source: "nantes", target: "paris" }],
};

interface SigmaTestContext {
sigma: Sigma;
}

beforeEach<SigmaTestContext>(async (context) => {
const graph = new Graph();
graph.import(GRAPH);
const container = createElement("div", { width: "100px", height: "100px" });
document.body.append(container);
context.sigma = new Sigma(graph, container);
});

afterEach<SigmaTestContext>(async ({ sigma }) => {
sigma.kill();
sigma.getContainer().remove();
});

describe("@sigma/layer-maplibre", () => {
test<SigmaTestContext>("calling clean function multiple times should work", ({ sigma }) => {
const { clean } = bindMapLayer(sigma);
expect(() => {
clean();
clean();
}).not.to.throw();
});
test<SigmaTestContext>("clean the map should reset sigma'settings to its previous value", ({ sigma }) => {
const prevSettings = sigma.getSettings();

const { clean } = bindMapLayer(sigma);
const settings = sigma.getSettings();

clean();

expect(prevSettings).not.toEqual(settings);
expect(sigma.getSettings()).toEqual(prevSettings);
});
});
Loading