mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-17 00:59:29 -07:00
hourly and travel forecast silent reload
This commit is contained in:
@@ -23,7 +23,7 @@ class CurrentWeather extends WeatherDisplay {
|
||||
|
||||
async getData(_weatherParameters, refresh) {
|
||||
// always load the data for use in the lower scroll
|
||||
const superResult = super.getData(_weatherParameters);
|
||||
const superResult = super.getData(_weatherParameters, refresh);
|
||||
const weatherParameters = _weatherParameters ?? this.weatherParameters;
|
||||
|
||||
// filter for 4-letter observation stations, only those contain sky conditions and thus an icon
|
||||
|
||||
@@ -19,7 +19,7 @@ class ExtendedForecast extends WeatherDisplay {
|
||||
}
|
||||
|
||||
async getData(_weatherParameters, refresh) {
|
||||
if (!super.getData(_weatherParameters)) return;
|
||||
if (!super.getData(_weatherParameters, refresh)) return;
|
||||
const weatherParameters = _weatherParameters ?? this.weatherParameters;
|
||||
|
||||
// request us or si units
|
||||
|
||||
@@ -28,7 +28,7 @@ class Hazards extends WeatherDisplay {
|
||||
|
||||
async getData(weatherParameters, refresh) {
|
||||
// super checks for enabled
|
||||
const superResult = super.getData(weatherParameters);
|
||||
const superResult = super.getData(weatherParameters, refresh);
|
||||
|
||||
const alert = this.checkbox.querySelector('.alert');
|
||||
alert.classList.remove('show');
|
||||
|
||||
@@ -24,7 +24,7 @@ class HourlyGraph extends WeatherDisplay {
|
||||
}
|
||||
|
||||
async getData(_weatherParameters, refresh) {
|
||||
if (!super.getData()) return;
|
||||
if (!super.getData(undefined, refresh)) return;
|
||||
|
||||
const data = await getHourlyData(() => this.stillWaiting());
|
||||
if (data === undefined) {
|
||||
|
||||
@@ -29,21 +29,28 @@ class Hourly extends WeatherDisplay {
|
||||
|
||||
async getData(weatherParameters, refresh) {
|
||||
// super checks for enabled
|
||||
const superResponse = super.getData(weatherParameters);
|
||||
const superResponse = super.getData(weatherParameters, refresh);
|
||||
let forecast;
|
||||
try {
|
||||
// get the forecast
|
||||
forecast = await json(weatherParameters.forecastGridData, { retryCount: 3, stillWaiting: () => this.stillWaiting() });
|
||||
forecast = await json(this.weatherParameters.forecastGridData, { retryCount: 3, stillWaiting: () => this.stillWaiting() });
|
||||
// parse the forecast
|
||||
this.data = await parseForecast(forecast.properties);
|
||||
} catch (error) {
|
||||
console.error('Get hourly forecast failed');
|
||||
console.error(error.status, error.responseJSON);
|
||||
if (this.isEnabled) this.setStatus(STATUS.failed);
|
||||
// return undefined to other subscribers
|
||||
this.getDataCallback(undefined);
|
||||
return;
|
||||
// use old data if available
|
||||
if (this.data) {
|
||||
console.log('Using previous hourly forecast');
|
||||
// don't return, this.data is usable from the previous update
|
||||
} else {
|
||||
if (this.isEnabled) this.setStatus(STATUS.failed);
|
||||
// return undefined to other subscribers
|
||||
this.getDataCallback(undefined);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.data = await parseForecast(forecast.properties);
|
||||
this.getDataCallback();
|
||||
if (!superResponse) return;
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class LatestObservations extends WeatherDisplay {
|
||||
}
|
||||
|
||||
async getData(_weatherParameters, refresh) {
|
||||
if (!super.getData(_weatherParameters)) return;
|
||||
if (!super.getData(_weatherParameters, refresh)) return;
|
||||
const weatherParameters = _weatherParameters ?? this.weatherParameters;
|
||||
|
||||
// calculate distance to each station
|
||||
|
||||
@@ -15,7 +15,7 @@ class LocalForecast extends WeatherDisplay {
|
||||
}
|
||||
|
||||
async getData(_weatherParameters, refresh) {
|
||||
if (!super.getData(_weatherParameters)) return;
|
||||
if (!super.getData(_weatherParameters, refresh)) return;
|
||||
const weatherParameters = _weatherParameters ?? this.weatherParameters;
|
||||
|
||||
// get raw data
|
||||
|
||||
@@ -43,7 +43,7 @@ class Radar extends WeatherDisplay {
|
||||
}
|
||||
|
||||
async getData(_weatherParameters, refresh) {
|
||||
if (!super.getData(_weatherParameters)) return;
|
||||
if (!super.getData(_weatherParameters, refresh)) return;
|
||||
const weatherParameters = _weatherParameters ?? this.weatherParameters;
|
||||
|
||||
// ALASKA AND HAWAII AREN'T SUPPORTED!
|
||||
|
||||
@@ -22,7 +22,7 @@ class RegionalForecast extends WeatherDisplay {
|
||||
}
|
||||
|
||||
async getData(_weatherParameters, refresh) {
|
||||
if (!super.getData(_weatherParameters)) return;
|
||||
if (!super.getData(_weatherParameters, refresh)) return;
|
||||
const weatherParameters = _weatherParameters ?? this.weatherParameters;
|
||||
|
||||
// pre-load the base map
|
||||
|
||||
@@ -26,20 +26,42 @@ class TravelForecast extends WeatherDisplay {
|
||||
if (extra !== 0) this.timing.delay.push(Math.round(this.extra * this.cityHeight));
|
||||
// add the final 3 second delay
|
||||
this.timing.delay.push(150);
|
||||
|
||||
// add previous data cache
|
||||
this.previousData = [];
|
||||
}
|
||||
|
||||
async getData(weatherParameters, refresh) {
|
||||
// super checks for enabled
|
||||
if (!super.getData(this.weatherParameters)) return;
|
||||
const forecastPromises = TravelCities.map(async (city) => {
|
||||
if (!super.getData(weatherParameters, refresh)) return;
|
||||
|
||||
// clear stored data if not refresh
|
||||
if (!refresh) {
|
||||
this.previousData = [];
|
||||
}
|
||||
|
||||
const forecastPromises = TravelCities.map(async (city, index) => {
|
||||
try {
|
||||
// get point then forecast
|
||||
if (!city.point) throw new Error('No pre-loaded point');
|
||||
const forecast = await json(`https://api.weather.gov/gridpoints/${city.point.wfo}/${city.point.x},${city.point.y}/forecast`, {
|
||||
data: {
|
||||
units: settings.units.value,
|
||||
},
|
||||
});
|
||||
let forecast;
|
||||
try {
|
||||
forecast = await json(`https://api.weather.gov/gridpoints/${city.point.wfo}/${city.point.x},${city.point.y}/forecast`, {
|
||||
data: {
|
||||
units: settings.units.value,
|
||||
},
|
||||
});
|
||||
// store for the next run
|
||||
this.previousData[index] = forecast;
|
||||
} catch (e) {
|
||||
// if there's previous data use it
|
||||
if (this.previousData?.[index]) {
|
||||
forecast = this.previousData?.[index];
|
||||
} else {
|
||||
// otherwise re-throw for the standard error handling
|
||||
throw (e);
|
||||
}
|
||||
}
|
||||
// determine today or tomorrow (shift periods by 1 if tomorrow)
|
||||
const todayShift = forecast.properties.periods[0].isDaytime ? 0 : 1;
|
||||
// return a pared-down forecast
|
||||
|
||||
Reference in New Issue
Block a user