@trackunit/react-map-adapter-mapbox 0.0.146-alpha-7afddbbe4cc.0 → 0.0.146

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.cjs.js +49 -16
  2. package/index.esm.js +49 -16
  3. package/package.json +3 -3
package/index.cjs.js CHANGED
@@ -181,6 +181,25 @@ const toHeatmapColorExpression = (colorRange) => {
181
181
  ...stops,
182
182
  ];
183
183
  };
184
+ /**
185
+ * Runs `mutate` and returns the first error the map reports while it runs, or `undefined`.
186
+ *
187
+ * Mapbox rejects an invalid `addSource` / `addLayer` by firing a synchronous `error` event instead of throwing.
188
+ */
189
+ const captureMapError = (map, mutate) => {
190
+ let captured;
191
+ const onError = (event) => {
192
+ captured ?? (captured = event.error);
193
+ };
194
+ map.on("error", onError);
195
+ try {
196
+ mutate();
197
+ }
198
+ finally {
199
+ map.off("error", onError);
200
+ }
201
+ return captured;
202
+ };
184
203
  // ============================================================================
185
204
  // MapboxLayerPort
186
205
  // ============================================================================
@@ -985,22 +1004,36 @@ class MapboxLayerPort {
985
1004
  this.removeHeatmapInternal(config.id);
986
1005
  const sourceId = `heatmap-source-${config.id}`;
987
1006
  const layerId = `heatmap-layer-${config.id}`;
988
- this.map.addSource(sourceId, {
989
- type: "geojson",
990
- data: toMapboxGeoJson(config.features),
991
- });
992
- this.map.addLayer({
993
- id: layerId,
994
- type: "heatmap",
995
- source: sourceId,
996
- paint: {
997
- "heatmap-weight": ["coalesce", ["get", "weight"], 1],
998
- "heatmap-radius": config.style.radiusPixels,
999
- "heatmap-intensity": config.style.intensity,
1000
- "heatmap-opacity": config.style.opacity,
1001
- "heatmap-color": toHeatmapColorExpression(config.style.colorRange),
1002
- },
1003
- });
1007
+ try {
1008
+ const map = this.map;
1009
+ const sourceError = captureMapError(map, () => map.addSource(sourceId, {
1010
+ type: "geojson",
1011
+ data: toMapboxGeoJson(config.features),
1012
+ }));
1013
+ if (!hasSource(map, sourceId)) {
1014
+ throw sourceError ?? new Error(`Mapbox rejected heatmap source "${sourceId}"`);
1015
+ }
1016
+ const layerError = captureMapError(map, () => map.addLayer({
1017
+ id: layerId,
1018
+ type: "heatmap",
1019
+ source: sourceId,
1020
+ paint: {
1021
+ "heatmap-weight": ["coalesce", ["get", "weight"], 1],
1022
+ "heatmap-radius": config.style.radiusPixels,
1023
+ "heatmap-intensity": config.style.intensity,
1024
+ "heatmap-opacity": config.style.opacity,
1025
+ "heatmap-color": toHeatmapColorExpression(config.style.colorRange),
1026
+ },
1027
+ }));
1028
+ if (!hasLayer(map, layerId)) {
1029
+ throw layerError ?? new Error(`Mapbox rejected heatmap layer "${layerId}"`);
1030
+ }
1031
+ }
1032
+ catch (cause) {
1033
+ safeRemoveLayersAndSource(this.map, [layerId], sourceId);
1034
+ config.onError?.({ layerId: config.id, code: "renderer-update-failed", cause });
1035
+ }
1036
+ // Tracked even on failure: an unchanged config is skipped (no rebuild, no repeat onError) and style reload retries it.
1004
1037
  this.heatmapSources.set(config.id, { sourceId, layerId, config });
1005
1038
  }
1006
1039
  removeHeatmapInternal(id) {
package/index.esm.js CHANGED
@@ -180,6 +180,25 @@ const toHeatmapColorExpression = (colorRange) => {
180
180
  ...stops,
181
181
  ];
182
182
  };
183
+ /**
184
+ * Runs `mutate` and returns the first error the map reports while it runs, or `undefined`.
185
+ *
186
+ * Mapbox rejects an invalid `addSource` / `addLayer` by firing a synchronous `error` event instead of throwing.
187
+ */
188
+ const captureMapError = (map, mutate) => {
189
+ let captured;
190
+ const onError = (event) => {
191
+ captured ?? (captured = event.error);
192
+ };
193
+ map.on("error", onError);
194
+ try {
195
+ mutate();
196
+ }
197
+ finally {
198
+ map.off("error", onError);
199
+ }
200
+ return captured;
201
+ };
183
202
  // ============================================================================
184
203
  // MapboxLayerPort
185
204
  // ============================================================================
@@ -984,22 +1003,36 @@ class MapboxLayerPort {
984
1003
  this.removeHeatmapInternal(config.id);
985
1004
  const sourceId = `heatmap-source-${config.id}`;
986
1005
  const layerId = `heatmap-layer-${config.id}`;
987
- this.map.addSource(sourceId, {
988
- type: "geojson",
989
- data: toMapboxGeoJson(config.features),
990
- });
991
- this.map.addLayer({
992
- id: layerId,
993
- type: "heatmap",
994
- source: sourceId,
995
- paint: {
996
- "heatmap-weight": ["coalesce", ["get", "weight"], 1],
997
- "heatmap-radius": config.style.radiusPixels,
998
- "heatmap-intensity": config.style.intensity,
999
- "heatmap-opacity": config.style.opacity,
1000
- "heatmap-color": toHeatmapColorExpression(config.style.colorRange),
1001
- },
1002
- });
1006
+ try {
1007
+ const map = this.map;
1008
+ const sourceError = captureMapError(map, () => map.addSource(sourceId, {
1009
+ type: "geojson",
1010
+ data: toMapboxGeoJson(config.features),
1011
+ }));
1012
+ if (!hasSource(map, sourceId)) {
1013
+ throw sourceError ?? new Error(`Mapbox rejected heatmap source "${sourceId}"`);
1014
+ }
1015
+ const layerError = captureMapError(map, () => map.addLayer({
1016
+ id: layerId,
1017
+ type: "heatmap",
1018
+ source: sourceId,
1019
+ paint: {
1020
+ "heatmap-weight": ["coalesce", ["get", "weight"], 1],
1021
+ "heatmap-radius": config.style.radiusPixels,
1022
+ "heatmap-intensity": config.style.intensity,
1023
+ "heatmap-opacity": config.style.opacity,
1024
+ "heatmap-color": toHeatmapColorExpression(config.style.colorRange),
1025
+ },
1026
+ }));
1027
+ if (!hasLayer(map, layerId)) {
1028
+ throw layerError ?? new Error(`Mapbox rejected heatmap layer "${layerId}"`);
1029
+ }
1030
+ }
1031
+ catch (cause) {
1032
+ safeRemoveLayersAndSource(this.map, [layerId], sourceId);
1033
+ config.onError?.({ layerId: config.id, code: "renderer-update-failed", cause });
1034
+ }
1035
+ // Tracked even on failure: an unchanged config is skipped (no rebuild, no repeat onError) and style reload retries it.
1003
1036
  this.heatmapSources.set(config.id, { sourceId, layerId, config });
1004
1037
  }
1005
1038
  removeHeatmapInternal(id) {
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@trackunit/react-map-adapter-mapbox",
3
- "version": "0.0.146-alpha-7afddbbe4cc.0",
3
+ "version": "0.0.146",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
7
7
  "node": ">=24.x"
8
8
  },
9
9
  "dependencies": {
10
- "@trackunit/react-map-adapter-shared": "0.0.158-alpha-7afddbbe4cc.0",
11
- "@trackunit/geo-json-utils": "1.18.7-alpha-7afddbbe4cc.0",
10
+ "@trackunit/react-map-adapter-shared": "0.0.158",
11
+ "@trackunit/geo-json-utils": "1.18.6",
12
12
  "mapbox-gl": "^3.18.1",
13
13
  "supercluster": "^8.0.1",
14
14
  "es-toolkit": "^1.52.0",