Revert "Add support for fallback WeatherProviders"

This reverts commit 0e23e56817.
This commit is contained in:
Matthew Oslan
2019-05-23 14:36:34 -04:00
parent 4ffeed5999
commit dec9f31dec
2 changed files with 1 additions and 50 deletions

View File

@@ -1,45 +0,0 @@
import { GeoCoordinates, WateringData, WeatherData, WeatherProvider } from "../../types";
/**
* A WeatherProvider calls other WeatherProviders until one successfully returns a value.
* This is a special utility WeatherProvider, and should NOT be selected with the WEATHER_PROVIDER environment variable.
*/
export default class CompositeWeatherProvider implements WeatherProvider {
private readonly weatherProviders: WeatherProvider[];
public constructor( weatherProviders: WeatherProvider[] ) {
this.weatherProviders = weatherProviders;
}
public async getWateringData( coordinates : GeoCoordinates ): Promise< WateringData > {
return await this.callMethod( "getWateringData", coordinates ) as WateringData;
}
public async getWeatherData( coordinates : GeoCoordinates ): Promise< WeatherData > {
return await this.callMethod( "getWeatherData", coordinates ) as WeatherData;
}
/**
* Calls a specified function in each WeatherProvider until one returns a non-undefined value. If the function is
* not defined for a WeatherProvider, it will be skipped.
* @param func The name of the function to call.
* @param args The arguments to pass to the function.
* @return A promise that will be resolved with the first non-undefined value returned by a WeatherProvider, or
* resolved with undefined if none of the WeatherProviders returned a value.
*/
private async callMethod( func: "getWateringData" | "getWeatherData", ...args: any ): Promise< unknown > {
for ( const weatherProvider of this.weatherProviders ) {
if ( !weatherProvider[ func ] ) {
continue;
}
// @ts-ignore
const result = await weatherProvider[ func ]( ...args );
if ( result ) {
return result;
}
}
return undefined;
}
}