From 0903deeaa27da6b596e37377548974c285351cee Mon Sep 17 00:00:00 2001 From: Jens Lody Date: Fri, 11 Sep 2015 06:47:20 +0200 Subject: [PATCH] Add reentrency-guards to parseWeatherxxxx-functions. --- src/forecast_io.js | 22 ++++++++++++++++++++++ src/openweathermap_org.js | 10 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/forecast_io.js b/src/forecast_io.js index 6bfecd1..6dbba88 100644 --- a/src/forecast_io.js +++ b/src/forecast_io.js @@ -93,10 +93,21 @@ function getWeatherIcon(icon) { function parseWeatherCurrent() { if (this.currentWeatherCache === undefined) { + // this is a reentrency guard, in this times set for both caches, + // because they get updated with one call to forecast.io + this.currentWeatherCache = "in refresh"; + // but do it only if the cache has been cleared, otherwise we would + // overwrite possibly valid data, that can be kept if the update fails + // for some reason + if (this.forecastWeatherCache === undefined) + this.forecastWeatherCache = "in refresh"; this.refreshWeatherCurrent(); return; } + if (this.currentWeatherCache == "in refresh") + return; + this.checkPositionInPanel(); let json = this.currentWeatherCache; @@ -199,10 +210,21 @@ function refreshWeatherCurrent() { function parseWeatherForecast() { if (this.forecastWeatherCache === undefined) { + // this is a reentrency guard, in this times set for both caches, + // because they get updated with one call to forecast.io + this.forecastWeatherCache = "in refresh"; + // but do it only if the cache has been cleared, otherwise we would + // overwrite possibly valid data, that can be kept if the update fails + // for some reason + if (this.currentWeatherCache === undefined) + this.currentWeatherCache = "in refresh"; this.refreshWeatherCurrent(); return; } + if (this.forecastWeatherCache == "in refresh") + return; + let forecast = this.forecastWeatherCache; let beginOfDay = new Date(new Date().setHours(0, 0, 0, 0)); let cnt = Math.min(this._days_forecast, forecast.length); diff --git a/src/openweathermap_org.js b/src/openweathermap_org.js index ca9fe4d..551fd01 100644 --- a/src/openweathermap_org.js +++ b/src/openweathermap_org.js @@ -290,10 +290,15 @@ function getWeatherCondition(code) { function parseWeatherCurrent() { if (this.currentWeatherCache === undefined) { + // this is a reentrency guard + this.currentWeatherCache = "in refresh"; this.refreshWeatherCurrent(); return; } + if (this.currentWeatherCache == "in refresh") + return; + this.checkPositionInPanel(); let json = this.currentWeatherCache; @@ -406,10 +411,15 @@ function refreshWeatherCurrent() { function parseWeatherForecast() { if (this.forecastWeatherCache === undefined) { + // this is a reentrency guard + this.forecastWeatherCache = "in refresh"; this.refreshWeatherForecast(); return; } + if (this.forecastWeatherCache == "in refresh") + return; + let forecast = this.forecastWeatherCache; let beginOfDay = new Date(new Date().setHours(0, 0, 0, 0));