Skip to content

Commit

Permalink
Remove when.js and replace with native promises
Browse files Browse the repository at this point in the history
There were a few areas in Source where a function was assumed to be executed synchronously when a function was resolved. Native Promises however, by spec, will resolve at the end of a frame. There were also some strange handling of promise rejection in imagery providers that I cleaned up a bit in order to make them testable.

- In LabelCollection and EntityCluster, order of execution adjustment where made.
- Added TextureAtlas.addImageSync to support the order of execution fix in LabelCollection
- ArcGisMapServerImageryProvider.readyPromise will not reject if there is a failure unless the request cannot be retried.
- SingleTileImageryProvider.readyPromise will not reject if there is a failure unless the request cannot be retried.

The majority of the changes lie in the Specs, where unresolved promises weren’t being awaited before finishing executions, and where resolved promises are assumed to be synchronous all over the place and needed a good amount of fixes. Another issue which came up was calling Promise.reject in the body of a spec can cause node to halt execution when running via the command line.
  • Loading branch information
ggetz committed Mar 16, 2022
1 parent 0daea96 commit 31c154a
Show file tree
Hide file tree
Showing 264 changed files with 9,538 additions and 7,802 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Source/ThirdParty/rbush.js
Source/ThirdParty/topojson.js
Source/ThirdParty/Tween.js
Source/ThirdParty/Uri.js
Source/ThirdParty/when.js
Source/ThirdParty/zip.js
Source/ThirdParty/Workers/pako_inflate.min.js
Source/ThirdParty/Workers/pako_deflate.min.js
Expand Down
2 changes: 1 addition & 1 deletion Apps/CesiumViewer/CesiumViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function main() {
viewer.flyTo(dataSource);
}
})
.otherwise(function (error) {
.catch(function (error) {
showLoadError(source, error);
});
}
Expand Down
4 changes: 1 addition & 3 deletions Apps/Sandcastle/CesiumSandcastle.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ require({
"dojo/parser",
"dojo/promise/all",
"dojo/query",
"dojo/when",
"dojo/request/script",
"Sandcastle/LinkButton",
"ThirdParty/clipboard.min",
Expand Down Expand Up @@ -92,7 +91,6 @@ require({
parser,
all,
query,
when,
dojoscript,
LinkButton,
ClipboardJS,
Expand Down Expand Up @@ -1470,7 +1468,7 @@ require({
});
}

when(promise).then(function () {
Promise.resolve(promise).then(function () {
dom.byId("searchDemos").appendChild(galleryErrorMsg);
searchContainer = registry.byId("searchContainer");

Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Adjust Height.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
)
);
})
.otherwise(function (error) {
.catch(function (error) {
console.log(error);
});

Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles BIM.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
)
);
})
.otherwise(function (error) {
.catch(function (error) {
console.log(error);
});

Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Clipping Planes.html
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
}
return tileset;
})
.otherwise(function (error) {
.catch(function (error) {
console.log(error);
});
}
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Formats.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
});
}
})
.otherwise(function (error) {
.catch(function (error) {
throw error;
});
});
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Ambient Occlusion.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
.then(function (tileset) {
viewer.scene.primitives.add(tileset);
})
.otherwise(function (error) {
.catch(function (error) {
console.log(error);
});

Expand Down
79 changes: 38 additions & 41 deletions Apps/Sandcastle/gallery/Billboards.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,48 +160,45 @@

function offsetByDistance() {
Sandcastle.declare(offsetByDistance);
Cesium.when.all(
[
Cesium.Resource.fetchImage("../images/Cesium_Logo_overlay.png"),
Cesium.Resource.fetchImage("../images/facility.gif"),
],
function (images) {
// As viewer zooms closer to facility billboard,
// increase pixelOffset on CesiumLogo billboard to this height
const facilityHeight = images[1].height;
Promise.all([
Cesium.Resource.fetchImage("../images/Cesium_Logo_overlay.png"),
Cesium.Resource.fetchImage("../images/facility.gif"),
]).then(function (images) {
// As viewer zooms closer to facility billboard,
// increase pixelOffset on CesiumLogo billboard to this height
const facilityHeight = images[1].height;

// colocated billboards, separate as viewer gets closer
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
billboard: {
image: images[1],
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
},
});
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
billboard: {
image: images[0],
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
pixelOffset: new Cesium.Cartesian2(0.0, -facilityHeight),
pixelOffsetScaleByDistance: new Cesium.NearFarScalar(
1.0e3,
1.0,
1.5e6,
0.0
),
translucencyByDistance: new Cesium.NearFarScalar(
1.0e3,
1.0,
1.5e6,
0.1
),
},
});
}
);
// colocated billboards, separate as viewer gets closer
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
billboard: {
image: images[1],
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
},
});
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
billboard: {
image: images[0],
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
pixelOffset: new Cesium.Cartesian2(0.0, -facilityHeight),
pixelOffsetScaleByDistance: new Cesium.NearFarScalar(
1.0e3,
1.0,
1.5e6,
0.0
),
translucencyByDistance: new Cesium.NearFarScalar(
1.0e3,
1.0,
1.5e6,
0.1
),
},
});
});
}

function addMarkerBillboards() {
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/CZML 3D Tiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
.then(function (dataSource) {
viewer.flyTo(dataSource.entities.getById("BatchedColors"));
})
.otherwise(function (error) {
.catch(function (error) {
window.alert(error);
});
//Sandcastle_End
Expand Down
26 changes: 14 additions & 12 deletions Apps/Sandcastle/gallery/CZML Custom Properties.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
});

const dataSource = new Cesium.CzmlDataSource();
const loadedPromise = dataSource.load(czml);

function scaleProperty(property, scalingFactor) {
// returns a property that scales another property by a constant factor.
Expand All @@ -134,18 +135,20 @@
}

function setExtrudedHeight(propertyName) {
const customPropertyObject = dataSource.entities.getById(
"custom_property_object"
);
const property = customPropertyObject.properties[propertyName];
const colorado = dataSource.entities.getById("colorado");
loadedPromise.then(function () {
const customPropertyObject = dataSource.entities.getById(
"custom_property_object"
);
const property = customPropertyObject.properties[propertyName];
const colorado = dataSource.entities.getById("colorado");

// Because the population values are so large, we scale them down
// by 50 so they fit on the screen.
// If we didn't need to scale, we could directly assign the property
// to extrudedHeight.
// colorado.polygon.extrudedHeight = scaleProperty(property, 1 / 50.0);
colorado.polygon.extrudedHeight = scaleProperty(property, 1 / 50.0);
// Because the population values are so large, we scale them down
// by 50 so they fit on the screen.
// If we didn't need to scale, we could directly assign the property
// to extrudedHeight.
// colorado.polygon.extrudedHeight = scaleProperty(property, 1 / 50.0);
colorado.polygon.extrudedHeight = scaleProperty(property, 1 / 50.0);
});
}

// Custom properties can be used as the value of graphical properties:
Expand All @@ -167,7 +170,6 @@
"propertiesMenu"
);

dataSource.load(czml);
viewer.dataSources.add(dataSource);
viewer.zoomTo(dataSource); //Sandcastle_End
Sandcastle.finishedLoading();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
.then(function (dataSource) {
viewer.trackedEntity = dataSource.entities.getById("model");
})
.otherwise(function (error) {
.catch(function (error) {
window.alert(error);
}); //Sandcastle_End
Sandcastle.finishedLoading();
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/CZML Model Articulations.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
.then(function (dataSource) {
viewer.trackedEntity = dataSource.entities.getById("test model");
})
.otherwise(function (error) {
.catch(function (error) {
console.error(error);
});
//Sandcastle_End
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/CZML Model Data URL.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
.then(function (dataSource) {
viewer.trackedEntity = dataSource.entities.getById("cube");
})
.otherwise(function (error) {
.catch(function (error) {
window.alert(error);
});

Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/CZML Model.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"aircraft model"
);
})
.otherwise(function (error) {
.catch(function (error) {
window.alert(error);
});

Expand Down
79 changes: 39 additions & 40 deletions Apps/Sandcastle/gallery/Clamp to Terrain.html
Original file line number Diff line number Diff line change
Expand Up @@ -256,53 +256,52 @@
terrainSamplePositions.push(position);
}

Cesium.when(
Promise.resolve(
Cesium.sampleTerrainMostDetailed(
viewer.terrainProvider,
terrainSamplePositions
),
function (samples) {
let offset = 10.0;
for (let i = 0; i < samples.length; ++i) {
samples[i].height += offset;
}
)
).then(function (samples) {
let offset = 10.0;
for (let i = 0; i < samples.length; ++i) {
samples[i].height += offset;
}

viewer.entities.add({
polyline: {
positions: Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(
samples
),
arcType: Cesium.ArcType.NONE,
width: 5,
material: new Cesium.PolylineOutlineMaterialProperty({
color: Cesium.Color.ORANGE,
viewer.entities.add({
polyline: {
positions: Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(
samples
),
arcType: Cesium.ArcType.NONE,
width: 5,
material: new Cesium.PolylineOutlineMaterialProperty({
color: Cesium.Color.ORANGE,
outlineWidth: 2,
outlineColor: Cesium.Color.BLACK,
}),
depthFailMaterial: new Cesium.PolylineOutlineMaterialProperty(
{
color: Cesium.Color.RED,
outlineWidth: 2,
outlineColor: Cesium.Color.BLACK,
}),
depthFailMaterial: new Cesium.PolylineOutlineMaterialProperty(
{
color: Cesium.Color.RED,
outlineWidth: 2,
outlineColor: Cesium.Color.BLACK,
}
),
},
});
}
),
},
});

const target = new Cesium.Cartesian3(
300770.50872389384,
5634912.131394585,
2978152.2865545116
);
offset = new Cesium.Cartesian3(
6344.974098678562,
-793.3419798081741,
2499.9508860763162
);
viewer.camera.lookAt(target, offset);
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
}
);
const target = new Cesium.Cartesian3(
300770.50872389384,
5634912.131394585,
2978152.2865545116
);
offset = new Cesium.Cartesian3(
6344.974098678562,
-793.3419798081741,
2499.9508860763162
);
viewer.camera.lookAt(target, offset);
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
});
},
},
{
Expand Down
6 changes: 3 additions & 3 deletions Apps/Sandcastle/gallery/Custom DataSource.html
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,20 @@
this._changed.raiseEvent(this);
}

//Use 'when' to load the URL into a json object
//Load the URL into a json object
//and then process is with the `load` function.
const that = this;
return Cesium.Resource.fetchJson(url)
.then(function (json) {
return that.load(json, url);
})
.otherwise(function (error) {
.catch(function (error) {
//Otherwise will catch any errors or exceptions that occur
//during the promise processing. When this happens,
//we raise the error event and reject the promise.
this._setLoading(false);
that._error.raiseEvent(that, error);
return Cesium.when.reject(error);
return Promise.reject(error);
});
};

Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/DataSource Ordering.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
viewer.dataSources.add(promise2);

Sandcastle.addToolbarButton("Swap", function () {
Cesium.when.all([promise1, promise2]).then(function (results) {
Promise.all([promise1, promise2]).then(function (results) {
const ds1 = results[0];
const ds2 = results[1];
if (viewer.dataSources.indexOf(ds1) === 0) {
Expand Down
Loading

0 comments on commit 31c154a

Please sign in to comment.