Skip to content

Commit 5bcf624

Browse files
committed
Prevent multiple loads, add last loaded on start, update sample
1 parent e3ab911 commit 5bcf624

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

samples/Sample/MainPage.xaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
</TableSection>
1919

2020
<TableSection>
21-
<!-- <TextCell Text="Last Loaded" /> -->
21+
<TextCell Text="Last Loaded"
22+
Detail="{Binding LastLoad}" />
23+
2224
<TextCell Text="Reload"
2325
Command="{Binding RefreshCommand}" />
2426
</TableSection>

samples/Sample/MainViewModel.cs

+6
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ IOptionsMonitor<MyConfig> cfg
2525
[ObservableProperty] string configurationUri;
2626
[ObservableProperty] string? theValue;
2727
[ObservableProperty] string valueFrom;
28+
[ObservableProperty] string lastLoad;
2829

2930
void Update(MyConfig config)
3031
{
3132
this.AccessToken = config.AccessToken;
3233
this.ConfigurationUri = config.ConfigurationUri;
3334
this.ValueFrom = config.ValueFrom;
3435
this.TheValue = config.TheValue;
36+
37+
this.LastLoad = this.remoteProvider
38+
.LastLoaded?
39+
.ToLocalTime()
40+
.ToString("yyyy MMMM dd - h:mm:ss tt") ?? "Never";
3541
}
3642

3743

src/Shiny.Extensions.Configuration.Remote/Infrastructure/DefaultRequestInvoker.cs

-7
This file was deleted.

src/Shiny.Extensions.Configuration.Remote/Infrastructure/IRequestInvoker.cs

-6
This file was deleted.

src/Shiny.Extensions.Configuration.Remote/Infrastructure/RemoteConfigurationProvider.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class RemoteConfigurationProvider(RemoteConfig config) : ConfigurationPro
77
public override void Load()
88
{
99
base.Load();
10+
if (File.Exists(config.ConfigurationFilePath))
11+
this.LastLoaded = File.GetLastWriteTime(config.ConfigurationFilePath);
12+
1013
if (config.WaitForLoadOnStartup)
1114
{
1215
this.LoadAsync().GetAwaiter().GetResult();
@@ -40,13 +43,23 @@ public async Task LoadAsync(CancellationToken cancellationToken = default)
4043
{
4144
try
4245
{
46+
// var wasWaiting = this.semaphore.CurrentCount == 0;
4347
// there can only be one!
4448
await this.semaphore.WaitAsync(cancellationToken);
4549

50+
if (this.LastLoaded != null)
51+
{
52+
var ts = DateTimeOffset.UtcNow.Subtract(this.LastLoaded.Value);
53+
if (ts.TotalSeconds < 30)
54+
return;
55+
}
56+
// // if I was waiting, just return, the config will have been called and loaded
57+
// if (wasWaiting)
58+
// return;
59+
4660
var httpClient = new HttpClient();
4761
var content = await httpClient.GetStringAsync(config.Uri, cancellationToken);
4862

49-
// TODO: this is not triggering changes from the json provider - could be the options monitor too?
5063
await File
5164
.WriteAllTextAsync(config.ConfigurationFilePath, content, cancellationToken)
5265
.ConfigureAwait(false);

0 commit comments

Comments
 (0)