fix data sharing race condition

This commit is contained in:
Matt Walsh
2020-10-20 20:04:51 -05:00
parent c4e8ef6a14
commit 98e01688ae
8 changed files with 285 additions and 8 deletions

View File

@@ -55,6 +55,8 @@ class CurrentWeather extends WeatherDisplay {
// we only get here if there was no error above
this.data = Object.assign({}, observations, {station: station});
this.setStatus(STATUS.loaded);
this.getDataCallback();
}
// format the data for use outside this function
@@ -201,9 +203,14 @@ class CurrentWeather extends WeatherDisplay {
this.finishDraw();
}
// return the latest gathered information if available
getCurrentWeather() {
return this.parseData();
// make data available outside this class
// promise allows for data to be requested before it is available
async getCurrentWeather() {
return new Promise((resolve) => {
if (this.data) resolve(this.parseData());
// data not available, put it into the data callback queue
this.getDataCallbacks.push(() => resolve(this.parseData()));
});
}
shortConditions(condition) {