Skip to content

Commit

Permalink
Prevent multiple loads, add last loaded on start, update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jul 4, 2024
1 parent e3ab911 commit 5bcf624
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
4 changes: 3 additions & 1 deletion samples/Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
</TableSection>

<TableSection>
<!-- <TextCell Text="Last Loaded" /> -->
<TextCell Text="Last Loaded"
Detail="{Binding LastLoad}" />

<TextCell Text="Reload"
Command="{Binding RefreshCommand}" />
</TableSection>
Expand Down
6 changes: 6 additions & 0 deletions samples/Sample/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ IOptionsMonitor<MyConfig> cfg
[ObservableProperty] string configurationUri;
[ObservableProperty] string? theValue;
[ObservableProperty] string valueFrom;
[ObservableProperty] string lastLoad;

void Update(MyConfig config)
{
this.AccessToken = config.AccessToken;
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";
}


Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5bcf624

Please sign in to comment.