Allow custom fallbacks from failed ArchiCacheable requests

This commit is contained in:
JustArchi
2019-01-21 22:41:03 +01:00
parent 4026e9dbfd
commit a0f4a8ae40

View File

@@ -22,6 +22,7 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using ArchiSteamFarm.Localization;
using JetBrains.Annotations; using JetBrains.Annotations;
namespace ArchiSteamFarm.Helpers { namespace ArchiSteamFarm.Helpers {
@@ -55,7 +56,13 @@ namespace ArchiSteamFarm.Helpers {
} }
[PublicAPI] [PublicAPI]
public async Task<(bool Success, T Result)> GetValue() { public async Task<(bool Success, T Result)> GetValue(EFallback fallback = EFallback.DefaultForType) {
if (!Enum.IsDefined(typeof(EFallback), fallback)) {
ASF.ArchiLogger.LogNullError(nameof(fallback));
return (false, default);
}
if (IsInitialized && IsRecent) { if (IsInitialized && IsRecent) {
return (true, InitializedValue); return (true, InitializedValue);
} }
@@ -70,7 +77,20 @@ namespace ArchiSteamFarm.Helpers {
(bool success, T result) = await ResolveFunction().ConfigureAwait(false); (bool success, T result) = await ResolveFunction().ConfigureAwait(false);
if (!success) { if (!success) {
return (false, InitializedValue); switch (fallback) {
case EFallback.DefaultForType:
return (false, default);
case EFallback.FailedNow:
return (false, result);
case EFallback.SuccessPreviously:
return (false, InitializedValue);
default:
ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(fallback), fallback));
goto case EFallback.DefaultForType;
}
} }
InitializedValue = result; InitializedValue = result;
@@ -95,7 +115,8 @@ namespace ArchiSteamFarm.Helpers {
} }
} }
internal async Task Reset() { [PublicAPI]
public async Task Reset() {
if (!IsInitialized) { if (!IsInitialized) {
return; return;
} }
@@ -113,9 +134,12 @@ namespace ArchiSteamFarm.Helpers {
} }
} }
private void HardReset() { private void HardReset(bool withValue = true) {
InitializedAt = DateTime.MinValue; InitializedAt = DateTime.MinValue;
InitializedValue = default;
if (withValue) {
InitializedValue = default;
}
if (MaintenanceTimer != null) { if (MaintenanceTimer != null) {
MaintenanceTimer.Dispose(); MaintenanceTimer.Dispose();
@@ -135,10 +159,16 @@ namespace ArchiSteamFarm.Helpers {
return; return;
} }
HardReset(); HardReset(false);
} finally { } finally {
InitSemaphore.Release(); InitSemaphore.Release();
} }
} }
public enum EFallback : byte {
DefaultForType,
FailedNow,
SuccessPreviously
}
} }
} }