Skip to content

Commit

Permalink
Switch to onLayerLoaded for visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewMuehlhauserNRCan committed Dec 5, 2024
1 parent a349716 commit 31a6b10
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions packages/geoview-core/src/geo/layer/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ export class LayerApi {
.then(() => {
const originalLayerPaths = originalMapOrderedLayerInfo.map((info) => info.layerPath);

// Prepare Listeners for Removing Previously Removed Layers
// Prepare listeners for removing previously removed layers
parentPaths.forEach((parentPath) => {
function removeChildLayers(sender: LayerApi): void {
const childPaths = sender.#getAllChildPaths(parentPath);
Expand All @@ -627,37 +627,17 @@ export class LayerApi {
this.onLayerAdded(removeChildLayers);
});

// Prepare listeners for changing the visibility
MapEventProcessor.setMapOrderedLayerInfo(this.getMapId(), originalMapOrderedLayerInfo);
const changedVisibilityPaths = originalMapOrderedLayerInfo
.filter((info) => {
const config = configs.find((c) => c.layerPath === info.layerPath);
return config?.initialSettings.states?.visible !== info.visible;
})
.map((info) => info.layerPath);

// Add listener for each layer with visibility
// that needs to be changed when added to the map
changedVisibilityPaths.forEach((path) => {
function setLayerVisibility(sender: LayerApi, event: LayerAddedEvent): void {
if (path.includes(event.layer.geoviewLayerId)) {
whenThisThen(
() => {
const lyrConfig = sender.getLayerEntryConfig(path);
return lyrConfig && lyrConfig.layerStatus === 'loaded';
},
10000,
500
)
.then(() => {
const { visible } = originalMapOrderedLayerInfo.filter((info) => info.layerPath === path)[0];
sender.getGeoviewLayerHybrid(path)?.setVisible(visible, path);
sender.offLayerAdded(setLayerVisibility);
})
.catch((err) => logger.logError(err));
originalMapOrderedLayerInfo.forEach((layerInfo) => {
function setLayerVisibility(sender: LayerApi, event: LayerLoadedEvent): void {
if (layerInfo.layerPath === event.layerPath) {
const { visible } = originalMapOrderedLayerInfo.filter((info) => info.layerPath === event.layerPath)[0];
event.layer?.setVisible(visible, event.layerPath);
sender.offLayerLoaded(setLayerVisibility);
}
}
// Tried with onLayerLoaded, but didn't work as expected
this.onLayerAdded(setLayerVisibility);
this.onLayerLoaded(setLayerVisibility);
});
})
.catch((err) => logger.logError(err));
Expand Down

0 comments on commit 31a6b10

Please sign in to comment.