From e699b305c370e62c251399c1dc7beda32eed4597 Mon Sep 17 00:00:00 2001 From: AliyanH Date: Wed, 9 Aug 2023 13:08:32 -0400 Subject: [PATCH] add whenready for map-extent --- src/map-extent.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/map-extent.js b/src/map-extent.js index 9b81dc2bb..4c569c798 100644 --- a/src/map-extent.js +++ b/src/map-extent.js @@ -78,7 +78,38 @@ export class MapExtent extends HTMLElement { this.parentNode.nodeName.toUpperCase() === 'LAYER-' ? this.parentNode : this.parentNode.host; - this._layer = parentLayer._layer; + parentLayer + .whenReady() + .then(() => { + this._layer = parentLayer._layer; + }) + .catch(() => { + throw new Error('Map never became ready'); + }); } disconnectedCallback() {} + whenReady() { + return new Promise((resolve, reject) => { + let interval, failureTimer; + if (this._layer) { + resolve(); + } else { + let extentElement = this; + interval = setInterval(testForExtent, 300, extentElement); + failureTimer = setTimeout(extentNotDefined, 10000); + } + function testForExtent(extentElement) { + if (extentElement._layer) { + clearInterval(interval); + clearTimeout(failureTimer); + resolve(); + } + } + function extentNotDefined() { + clearInterval(interval); + clearTimeout(failureTimer); + reject('Timeout reached waiting for extent to be ready'); + } + }); + } }