satvis icon indicating copy to clipboard operation
satvis copied to clipboard

make removing last satellite more robust

Open ot-goegelem opened this issue 3 weeks ago • 1 comments

When removing the last satellite the following error pops up

image

This MR fixes it by handling this a bit more gracefully

ot-goegelem avatar Nov 29 '25 22:11 ot-goegelem

this question may from cesium's ‘Loop Rendering’ : This code attempts to access Cesium's internal private properties at a very deep level (_primitives) in order to clean up memory. When you click "Deselect," the DataSource has already been removed from Cesium.

This causes the array under viewer.scene.primitives to become empty or its hierarchical structure to change. The original code assumes there is always an element at index 0 ([0]). Once the array is empty, accessing [0] will return undefined. Subsequently, trying to read its properties will throw an error: "Cannot read properties of undefined (reading '0')".

Solution You need to use the optional chaining operator (?.) to protect each array index access. If the array is empty or does not exist, it will directly return undefined instead of causing a crash. you can try to replace the code in satvis-next\src\modules\util\CesiumCleanupHelper.js with code below:

import { CesiumCallbackHelper } from "./CesiumCallbackHelper";

export class CesiumCleanupHelper { // Cleanup leftover Cesium internal data after removing satellites // https://github.com/CesiumGS/cesium/issues/7184 static cleanup(viewer) { const onTickEventRemovalCallback = CesiumCallbackHelper.createPeriodicTickCallback(viewer, 1, () => { console.info("Removing leftover Cesium internal data"); onTickEventRemovalCallback();

  /* eslint-disable no-underscore-dangle */
  
  const labelCollection = viewer.scene.primitives
    ?._primitives?.[0]
    ?._primitives?.[0]
    ?._primitives?.[0]
    ?._labelCollection;

  if (labelCollection) {
    if (labelCollection._spareBillboards) {
         labelCollection._spareBillboards.forEach((billboard) => {
           labelCollection._billboardCollection.remove(billboard);
         });
         labelCollection._spareBillboards.length = 0;
    }
  }
  /* eslint-enable no-underscore-dangle */
});

} }

hope useful :>

zhangzhiming212 avatar Dec 02 '25 06:12 zhangzhiming212