From aba782027b86693037ddf140f23f9d784f234f1d Mon Sep 17 00:00:00 2001 From: Craig Kochis Date: Wed, 14 Aug 2024 16:20:11 -0400 Subject: [PATCH] use map data event instead of interval when waiting for map to be ready --- src/ui/RadarLineFeature.ts | 15 ++++----------- src/ui/RadarPolygonFeature.ts | 15 ++++----------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/ui/RadarLineFeature.ts b/src/ui/RadarLineFeature.ts index 4aea91ac..ebf27f4a 100644 --- a/src/ui/RadarLineFeature.ts +++ b/src/ui/RadarLineFeature.ts @@ -77,17 +77,10 @@ class RadarLineFeature extends RadarMapFeature { // ensure map is ready before modifying source and layers if (map.loaded()) { addFeatureToMap(); - } else { // wait for map to load up to 3 seconds, or abort - let count = 0; - let interval = setInterval(() => { - if (map.loaded()) { - clearInterval(interval); - addFeatureToMap(); - } else if (++count >= 30 /* 3 seconds */) { - clearInterval(interval); - Logger.warn(`could not add line feature ${feature.id}`); - } - }, 100); + } else { + map.once('data',() => { // wait for map to be ready + addFeatureToMap(); + }); } return this; diff --git a/src/ui/RadarPolygonFeature.ts b/src/ui/RadarPolygonFeature.ts index 24d9bb0c..83a03b6e 100644 --- a/src/ui/RadarPolygonFeature.ts +++ b/src/ui/RadarPolygonFeature.ts @@ -65,17 +65,10 @@ class RadarPolygonFeature extends RadarMapFeature { // ensure map is ready before modifying source and layers if (map.loaded()) { addFeatureToMap(); - } else { // wait for map to load up to 3 seconds, or abort - let count = 0; - let interval = setInterval(() => { - if (map.loaded()) { - clearInterval(interval); - addFeatureToMap(); - } else if (++count >= 30 /* 3 seconds */) { - clearInterval(interval); - Logger.warn(`could not add polygon feature ${feature.id}`); - } - }, 100); + } else { + map.once('data',() => { // wait for map to be ready + addFeatureToMap(); + }); } return this;