From 5bcf624e4b344150961e75a11c8b53482473022e Mon Sep 17 00:00:00 2001 From: Allan Ritchie Date: Thu, 4 Jul 2024 16:30:57 -0400 Subject: [PATCH] Prevent multiple loads, add last loaded on start, update sample --- samples/Sample/MainPage.xaml | 4 +++- samples/Sample/MainViewModel.cs | 6 ++++++ .../Infrastructure/DefaultRequestInvoker.cs | 7 ------- .../Infrastructure/IRequestInvoker.cs | 6 ------ .../Infrastructure/RemoteConfigurationProvider.cs | 15 ++++++++++++++- 5 files changed, 23 insertions(+), 15 deletions(-) delete mode 100644 src/Shiny.Extensions.Configuration.Remote/Infrastructure/DefaultRequestInvoker.cs delete mode 100644 src/Shiny.Extensions.Configuration.Remote/Infrastructure/IRequestInvoker.cs diff --git a/samples/Sample/MainPage.xaml b/samples/Sample/MainPage.xaml index f85d4e0..8145afc 100644 --- a/samples/Sample/MainPage.xaml +++ b/samples/Sample/MainPage.xaml @@ -18,7 +18,9 @@ - + + diff --git a/samples/Sample/MainViewModel.cs b/samples/Sample/MainViewModel.cs index 205eb77..35fe1ef 100644 --- a/samples/Sample/MainViewModel.cs +++ b/samples/Sample/MainViewModel.cs @@ -25,6 +25,7 @@ IOptionsMonitor cfg [ObservableProperty] string configurationUri; [ObservableProperty] string? theValue; [ObservableProperty] string valueFrom; + [ObservableProperty] string lastLoad; void Update(MyConfig config) { @@ -32,6 +33,11 @@ void Update(MyConfig config) this.ConfigurationUri = config.ConfigurationUri; this.ValueFrom = config.ValueFrom; this.TheValue = config.TheValue; + + this.LastLoad = this.remoteProvider + .LastLoaded? + .ToLocalTime() + .ToString("yyyy MMMM dd - h:mm:ss tt") ?? "Never"; } diff --git a/src/Shiny.Extensions.Configuration.Remote/Infrastructure/DefaultRequestInvoker.cs b/src/Shiny.Extensions.Configuration.Remote/Infrastructure/DefaultRequestInvoker.cs deleted file mode 100644 index 9baa7cb..0000000 --- a/src/Shiny.Extensions.Configuration.Remote/Infrastructure/DefaultRequestInvoker.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Shiny.Extensions.Configuration.Remote.Infrastructure; - - -public class DefaultRequestInvoker : IRequestInvoker -{ - -} \ No newline at end of file diff --git a/src/Shiny.Extensions.Configuration.Remote/Infrastructure/IRequestInvoker.cs b/src/Shiny.Extensions.Configuration.Remote/Infrastructure/IRequestInvoker.cs deleted file mode 100644 index bf86611..0000000 --- a/src/Shiny.Extensions.Configuration.Remote/Infrastructure/IRequestInvoker.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Shiny.Extensions.Configuration.Remote.Infrastructure; - -public interface IRequestInvoker -{ - -} \ No newline at end of file diff --git a/src/Shiny.Extensions.Configuration.Remote/Infrastructure/RemoteConfigurationProvider.cs b/src/Shiny.Extensions.Configuration.Remote/Infrastructure/RemoteConfigurationProvider.cs index fa9c52a..0af0e06 100644 --- a/src/Shiny.Extensions.Configuration.Remote/Infrastructure/RemoteConfigurationProvider.cs +++ b/src/Shiny.Extensions.Configuration.Remote/Infrastructure/RemoteConfigurationProvider.cs @@ -7,6 +7,9 @@ public class RemoteConfigurationProvider(RemoteConfig config) : ConfigurationPro public override void Load() { base.Load(); + if (File.Exists(config.ConfigurationFilePath)) + this.LastLoaded = File.GetLastWriteTime(config.ConfigurationFilePath); + if (config.WaitForLoadOnStartup) { this.LoadAsync().GetAwaiter().GetResult(); @@ -40,13 +43,23 @@ public async Task LoadAsync(CancellationToken cancellationToken = default) { try { + // var wasWaiting = this.semaphore.CurrentCount == 0; // there can only be one! await this.semaphore.WaitAsync(cancellationToken); + if (this.LastLoaded != null) + { + var ts = DateTimeOffset.UtcNow.Subtract(this.LastLoaded.Value); + if (ts.TotalSeconds < 30) + return; + } + // // if I was waiting, just return, the config will have been called and loaded + // if (wasWaiting) + // return; + var httpClient = new HttpClient(); var content = await httpClient.GetStringAsync(config.Uri, cancellationToken); - // TODO: this is not triggering changes from the json provider - could be the options monitor too? await File .WriteAllTextAsync(config.ConfigurationFilePath, content, cancellationToken) .ConfigureAwait(false);