Skip to content

Commit 36d35ca

Browse files
author
Les Moffat
committed
RD-1112 Fix map breaking when empty object is passed to space / halo
1 parent 87b2f9b commit 36d35ca

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/Map.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ export class Map extends maplibregl.Map {
304304
[0, "transparent"],
305305
[1, "transparent"],
306306
],
307-
scale: 0,
307+
scale: 1,
308308
});
309309
return;
310310
}
@@ -1015,18 +1015,16 @@ export class Map extends maplibregl.Map {
10151015
if (styleInfo.isFallback) {
10161016
if (this.getStyle()) {
10171017
console.warn(
1018-
"Invalid style. A style must be a valid URL to a style.json, a JSON string representing a valid StyleSpecification or a valid StyleSpecification object. Keeping the curent style instead.",
1018+
"[Map.setStyle]: Invalid style. A style must be a valid URL to a style.json, a JSON string representing a valid StyleSpecification or a valid StyleSpecification object. Keeping the curent style instead.",
10191019
);
10201020
return this;
10211021
}
10221022

10231023
console.warn(
1024-
"Invalid style. A style must be a valid URL to a style.json, a JSON string representing a valid StyleSpecification or a valid StyleSpecification object. Fallback to default MapTiler style.",
1024+
"[Map.setStyle]: Invalid style. A style must be a valid URL to a style.json, a JSON string representing a valid StyleSpecification or a valid StyleSpecification object. Fallback to default MapTiler style.",
10251025
);
10261026
}
10271027

1028-
this.styleInProcess = true;
1029-
10301028
// because the style must be finished loading and parsed before we can add custom layers
10311029
// we need to check if the terrain has changed, because if it has, we also need to wait
10321030
// for the terrain to load...
@@ -1035,6 +1033,7 @@ export class Map extends maplibregl.Map {
10351033

10361034
try {
10371035
super.setStyle(styleInfo.style, options);
1036+
this.styleInProcess = true;
10381037
} catch (e) {
10391038
this.styleInProcess = false;
10401039
console.error("[Map.setStyle]: Error while setting style:", e);
@@ -1047,7 +1046,8 @@ export class Map extends maplibregl.Map {
10471046

10481047
const setSpaceAndHaloFromStyle = () => {
10491048
const styleSpec = styleInfo.style as StyleSpecificationWithMetaData;
1050-
if (styleSpec.projection?.type === "mercator") {
1049+
if (!styleSpec.projection || styleSpec.projection.type === "mercator") {
1050+
console.warn("[Map.setStyle]: Neither space nor halo is supported for mercator projection. Ignoring...");
10511051
return;
10521052
}
10531053

@@ -1056,15 +1056,8 @@ export class Map extends maplibregl.Map {
10561056
this.setHaloFromStyle({ style: styleInfo.style as StyleSpecificationWithMetaData });
10571057
};
10581058

1059-
const handleStyleLoad = () => {
1060-
const styleSpec = styleInfo.style as StyleSpecificationWithMetaData;
1061-
1062-
if (styleSpec.projection?.type === "mercator") {
1063-
if (this.space) {
1064-
console.warn("Neither space nor halo is supported for mercator projection. Ignoring...");
1065-
}
1066-
return;
1067-
}
1059+
const handleStyleLoad = (e?: maplibregl.MapStyleDataEvent) => {
1060+
const styleSpec = (e?.target.getStyle() ?? styleInfo.style) as StyleSpecificationWithMetaData;
10681061

10691062
const targetBeforeLayer = this.getLayersOrder()[0];
10701063
if (this.space) {
@@ -1080,11 +1073,23 @@ export class Map extends maplibregl.Map {
10801073
}
10811074
};
10821075

1083-
if (this.styleInProcess && !this.spaceboxLoadingState.styleLoadCallbackSet) {
1076+
if (this.styleInProcess) {
10841077
// this handles setting space and halo from style on load
1085-
void this.once("style.load", handleStyleLoad);
1086-
this.spaceboxLoadingState.styleLoadCallbackSet = true;
1087-
return this;
1078+
// void this.once("idle", handleStyleLoad);
1079+
// void this.once("style.load", handleStyleLoad);
1080+
if (!this.spaceboxLoadingState.styleLoadCallbackSet) {
1081+
// unfortunatly, the style.load event is not always fired correctly when
1082+
// the style is set from am object (generally when projection changes or when metadata changes)
1083+
// so, in this instance, we have to double tap with both style events.
1084+
// an issue has been raised on the maplibre github
1085+
void this.once("style.load", handleStyleLoad);
1086+
void this.once("styledata", handleStyleLoad);
1087+
1088+
this.spaceboxLoadingState.styleLoadCallbackSet = true;
1089+
return this;
1090+
}
1091+
1092+
// if these load event doesn't fire after 10 seconds, we need to reset the flag
10881093
}
10891094

10901095
// the type returned from getStyle is incorrect, it can be null
@@ -1096,22 +1101,16 @@ export class Map extends maplibregl.Map {
10961101
return this;
10971102
}
10981103

1099-
const projectionFieldDeleted = !newStyle?.projection?.type;
1100-
if (projectionFieldDeleted) {
1101-
this.styleInProcess = true;
1102-
return this;
1103-
}
1104-
1105-
if (!this.spaceboxLoadingState.styleLoadCallbackSet) {
1104+
try {
11061105
handleStyleLoad();
1107-
this.spaceboxLoadingState.styleLoadCallbackSet = false;
1108-
}
1106+
} catch {}
11091107

11101108
return this;
11111109
}
11121110

11131111
private spaceboxLoadingState = {
11141112
styleLoadCallbackSet: false,
1113+
hasLoadedOnce: false,
11151114
};
11161115

11171116
/**

src/custom-layers/RadialGradientLayer/RadialGradientLayer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,9 @@ export class RadialGradientLayer implements CustomLayerInterface {
334334
];
335335
return;
336336
}
337+
this.gradient.scale = gradient.scale ?? defaultConstructorOptions.scale;
338+
this.gradient.stops = gradient.stops ?? defaultConstructorOptions.stops;
337339

338-
this.gradient = gradient;
339340
await this.animateIn();
340341
}
341342

0 commit comments

Comments
 (0)