Skip to content

Commit

Permalink
[sigma,layer-leaflet/maplibre] using killLayer to properly remove m…
Browse files Browse the repository at this point in the history
…ap container

Change the `killLayer` to allow removing none canvas/webgl layers which
can be created by `createLayer`.

Using it in map layer in the clean function.
  • Loading branch information
sim51 committed Nov 19, 2024
1 parent 7e0453a commit d3263c9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/layer-leaflet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export default function bindLeafletLayer(
const prevSigmaSettings = sigma.getSettings();

// Creating map container
const mapContainer = sigma.createLayer("layer-leaflet", "div", {
const mapLayerName = "layer-leaflet";
const mapContainer = sigma.createLayer(mapLayerName, "div", {
style: { position: "absolute", inset: "0", zIndex: "0" },
// 'edges' is the first sigma layer
beforeLayer: "edges",
Expand Down Expand Up @@ -118,7 +119,8 @@ export default function bindLeafletLayer(
isKilled = true;

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

sigma.killLayer(mapLayerName);

sigma.off("afterRender", fnSyncMapWithSigma);
sigma.off("resize", fnOnResize);
Expand Down
6 changes: 4 additions & 2 deletions packages/layer-maplibre/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default function bindMaplibreLayer(
const prevSigmaSettings = sigma.getSettings();

// Creating map container
const mapContainer = sigma.createLayer("layer-leaflet", "div", {
const mapLayerName = "layer-maplibre";
const mapContainer = sigma.createLayer(mapLayerName, "div", {
style: { position: "absolute", inset: "0" },
// 'edges' is the first sigma layer
beforeLayer: "edges",
Expand Down Expand Up @@ -100,7 +101,8 @@ export default function bindMaplibreLayer(

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

sigma.killLayer(mapLayerName);

sigma.off("afterRender", fnSyncMapWithSigma);
sigma.off("resize", fnOnResize);
Expand Down
12 changes: 6 additions & 6 deletions packages/sigma/src/sigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1645,26 +1645,26 @@ export default class Sigma<
}

/**
* Function used to properly kill a canvas layer.
* Function used to properly kill a layer.
*
* @param {string} id - Layer id.
* @return {Sigma}
*/
killLayer(id: string): this {
const canvas = this.elements[id];
const element = this.elements[id];

if (!canvas) throw new Error(`Sigma: cannot kill layer ${id}, which does not exist`);
if (!element) throw new Error(`Sigma: cannot kill layer ${id}, which does not exist`);

if (this.webGLContexts[id]) {
const gl = this.webGLContexts[id];
gl.getExtension("WEBGL_lose_context")?.loseContext();
delete this.webGLContexts[id];
} else {
} else if (this.canvasContexts[id]) {
delete this.canvasContexts[id];
}

// Delete canvas:
canvas.remove();
// Delete layer element
element.remove();
delete this.elements[id];

return this;
Expand Down

0 comments on commit d3263c9

Please sign in to comment.