Skip to content

Commit

Permalink
Merge pull request #11059 from CesiumGS/no-ready-promises
Browse files Browse the repository at this point in the history
Deprecate ready promises
  • Loading branch information
jjhembd authored Mar 29, 2023
2 parents 07b1082 + 72a4215 commit 2f40741
Show file tree
Hide file tree
Showing 475 changed files with 25,585 additions and 21,260 deletions.
63 changes: 32 additions & 31 deletions Apps/CesiumViewer/CesiumViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ window.CESIUM_BASE_URL = window.CESIUM_BASE_URL

import {
Cartesian3,
createWorldTerrain,
defined,
formatError,
Math as CesiumMath,
objectToQuery,
queryToObject,
CzmlDataSource,
GeoJsonDataSource,
ImageryLayer,
KmlDataSource,
GpxDataSource,
Terrain,
TileMapServiceImageryProvider,
Viewer,
viewerCesiumInspectorMixin,
viewerDragDropMixin,
} from "../../Build/CesiumUnminified/index.js";

function main() {
async function main() {
/*
Options parsed from query string:
source=url The URL of a CZML/GeoJSON/KML data source to load at startup.
Expand All @@ -43,32 +44,34 @@ function main() {
*/
const endUserOptions = queryToObject(window.location.search.substring(1));

let imageryProvider;
let baseLayer;
if (defined(endUserOptions.tmsImageryUrl)) {
imageryProvider = new TileMapServiceImageryProvider({
url: endUserOptions.tmsImageryUrl,
});
baseLayer = ImageryLayer.fromProviderAsync(
TileMapServiceImageryProvider.fromUrl(endUserOptions.tmsImageryUrl)
);
}

const loadingIndicator = document.getElementById("loadingIndicator");
const hasBaseLayerPicker = !defined(baseLayer);

const terrain = Terrain.fromWorldTerrain({
requestWaterMask: true,
requestVertexNormals: true,
});

let viewer;
try {
const hasBaseLayerPicker = !defined(imageryProvider);
viewer = new Viewer("cesiumContainer", {
imageryProvider: imageryProvider,
baseLayer: baseLayer,
baseLayerPicker: hasBaseLayerPicker,
scene3DOnly: endUserOptions.scene3DOnly,
requestRenderMode: true,
terrain: terrain,
});

if (hasBaseLayerPicker) {
const viewModel = viewer.baseLayerPicker.viewModel;
viewModel.selectedTerrain = viewModel.terrainProviderViewModels[1];
} else {
viewer.terrainProvider = createWorldTerrain({
requestWaterMask: true,
requestVertexNormals: true,
});
}
} catch (exception) {
loadingIndicator.style.display = "none";
Expand Down Expand Up @@ -145,25 +148,23 @@ function main() {
}

if (defined(loadPromise)) {
viewer.dataSources
.add(loadPromise)
.then(function (dataSource) {
const lookAt = endUserOptions.lookAt;
if (defined(lookAt)) {
const entity = dataSource.entities.getById(lookAt);
if (defined(entity)) {
viewer.trackedEntity = entity;
} else {
const error = `No entity with id "${lookAt}" exists in the provided data source.`;
showLoadError(source, error);
}
} else if (!defined(view) && endUserOptions.flyTo !== "false") {
viewer.flyTo(dataSource);
try {
const dataSource = await viewer.dataSources.add(loadPromise);
const lookAt = endUserOptions.lookAt;
if (defined(lookAt)) {
const entity = dataSource.entities.getById(lookAt);
if (defined(entity)) {
viewer.trackedEntity = entity;
} else {
const error = `No entity with id "${lookAt}" exists in the provided data source.`;
showLoadError(source, error);
}
})
.catch(function (error) {
showLoadError(source, error);
});
} else if (!defined(view) && endUserOptions.flyTo !== "false") {
viewer.flyTo(dataSource);
}
} catch (error) {
showLoadError(source, error);
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions Apps/Sandcastle/Sandcastle-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
window.embedInSandcastleTemplate = function (code, addExtraLine) {
return (
`${
"window.startup = function (Cesium) {\n" +
"window.startup = async function (Cesium) {\n" +
" 'use strict';\n" +
"//Sandcastle_Begin\n"
}${addExtraLine ? "\n" : ""}${code}//Sandcastle_End\n` +
` Sandcastle.finishedLoading();\n` +
`};\n` +
`if (typeof Cesium !== 'undefined') {\n` +
` window.startupCalled = true;\n` +
` window.startup(Cesium);\n` +
` window.startup(Cesium).catch((error) => {\n` +
` "use strict";\n` +
` console.error(error);\n` +
` });\n` +
`}\n`
);
};
Expand Down
9 changes: 6 additions & 3 deletions Apps/Sandcastle/gallery/3D Models Coloring.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
</table>
</div>
<script id="cesium_sandcastle_script">
window.startup = function (Cesium) {
window.startup = async function (Cesium) {
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
Expand Down Expand Up @@ -322,11 +322,14 @@
});

//Sandcastle_End
Sandcastle.finishedLoading();
};
if (typeof Cesium !== "undefined") {
window.startupCalled = true;
window.startup(Cesium);
window.startup(Cesium).catch((error) => {
"use strict";
console.error(error);
});
Sandcastle.finishedLoading();
}
</script>
</body>
Expand Down
9 changes: 6 additions & 3 deletions Apps/Sandcastle/gallery/3D Models.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar"></div>
<script id="cesium_sandcastle_script">
window.startup = function (Cesium) {
window.startup = async function (Cesium) {
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
Expand Down Expand Up @@ -169,11 +169,14 @@

Sandcastle.addToolbarMenu(options);
//Sandcastle_End
Sandcastle.finishedLoading();
};
if (typeof Cesium !== "undefined") {
window.startupCalled = true;
window.startup(Cesium);
window.startup(Cesium).catch((error) => {
"use strict";
console.error(error);
});
Sandcastle.finishedLoading();
}
</script>
</body>
Expand Down
50 changes: 25 additions & 25 deletions Apps/Sandcastle/gallery/3D Tiles Adjust Height.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@
<input type="text" size="5" data-bind="value: height" />
</div>
<script id="cesium_sandcastle_script">
window.startup = function (Cesium) {
window.startup = async function (Cesium) {
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
shadows: true,
});

viewer.scene.globe.depthTestAgainstTerrain = true;

const viewModel = {
height: 0,
};
Expand All @@ -71,31 +69,30 @@
const toolbar = document.getElementById("toolbar");
Cesium.knockout.applyBindings(viewModel, toolbar);

const tileset = new Cesium.Cesium3DTileset({
url: "../../SampleData/Cesium3DTiles/Tilesets/Tileset/tileset.json",
});

tileset.readyPromise
.then(function (tileset) {
viewer.scene.primitives.add(tileset);
viewer.zoomTo(
tileset,
new Cesium.HeadingPitchRange(
0.0,
-0.5,
tileset.boundingSphere.radius * 2.0
)
);
})
.catch(function (error) {
console.log(error);
});
let tileset;
try {
tileset = await Cesium.Cesium3DTileset.fromUrl(
"../../SampleData/Cesium3DTiles/Tilesets/Tileset/tileset.json"
);
viewer.scene.primitives.add(tileset);
viewer.scene.globe.depthTestAgainstTerrain = true;
viewer.zoomTo(
tileset,
new Cesium.HeadingPitchRange(
0.0,
-0.5,
tileset.boundingSphere.radius * 2.0
)
);
} catch (error) {
console.log(`Error loading tileset: ${error}`);
}

Cesium.knockout
.getObservable(viewModel, "height")
.subscribe(function (height) {
height = Number(height);
if (isNaN(height)) {
if (isNaN(height) || !Cesium.defined(tileset)) {
return;
}

Expand All @@ -121,11 +118,14 @@
});

//Sandcastle_End
Sandcastle.finishedLoading();
};
if (typeof Cesium !== "undefined") {
window.startupCalled = true;
window.startup(Cesium);
window.startup(Cesium).catch((error) => {
"use strict";
console.error(error);
});
Sandcastle.finishedLoading();
}
</script>
</body>
Expand Down
62 changes: 29 additions & 33 deletions Apps/Sandcastle/gallery/3D Tiles BIM.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar"></div>
<script id="cesium_sandcastle_script">
window.startup = function (Cesium) {
window.startup = async function (Cesium) {
"use strict";
//Sandcastle_Begin
// Power Plant design model provided by Bentley Systems
Expand All @@ -43,29 +43,6 @@
"2022-08-01T00:00:00Z"
);

const tileset = scene.primitives.add(
new Cesium.Cesium3DTileset({
url: Cesium.IonResource.fromAssetId(1240402),
})
);

tileset.readyPromise
.then(function (tileset) {
viewer.zoomTo(
tileset,
new Cesium.HeadingPitchRange(
0.5,
-0.2,
tileset.boundingSphere.radius * 4.0
)
);
})
.catch(function (error) {
console.log(error);
});

tileset.colorBlendMode = Cesium.Cesium3DTileColorBlendMode.REPLACE;

let selectedFeature;
let picking = false;

Expand Down Expand Up @@ -209,19 +186,38 @@
}
}

tileset.tileLoad.addEventListener(function (tile) {
processTileFeatures(tile, loadFeature);
});
try {
const tileset = await Cesium.Cesium3DTileset.fromIonAssetId(1240402);
scene.primitives.add(tileset);

viewer.zoomTo(
tileset,
new Cesium.HeadingPitchRange(
0.5,
-0.2,
tileset.boundingSphere.radius * 4.0
)
);

tileset.colorBlendMode = Cesium.Cesium3DTileColorBlendMode.REPLACE;
tileset.tileLoad.addEventListener(function (tile) {
processTileFeatures(tile, loadFeature);
});

tileset.tileUnload.addEventListener(function (tile) {
processTileFeatures(tile, unloadFeature);
});
//Sandcastle_End
Sandcastle.finishedLoading();
tileset.tileUnload.addEventListener(function (tile) {
processTileFeatures(tile, unloadFeature);
});
} catch (error) {
console.log(`Error loading tileset: ${error}`);
} //Sandcastle_End
};
if (typeof Cesium !== "undefined") {
window.startupCalled = true;
window.startup(Cesium);
window.startup(Cesium).catch((error) => {
"use strict";
console.error(error);
});
Sandcastle.finishedLoading();
}
</script>
</body>
Expand Down
Loading

0 comments on commit 2f40741

Please sign in to comment.