1
+ using System . Text . Json ;
2
+
1
3
namespace Shiny . Extensions . Configuration . Remote . Infrastructure ;
2
4
3
5
4
- public class RemoteConfigurationProvider ( RemoteConfig config ) : ConfigurationProvider , IRemoteConfigurationProvider
6
+ public class RemoteConfigurationProvider ( RemoteConfig config , Func < CancellationToken , Task < object > > ? getData ) : ConfigurationProvider , IRemoteConfigurationProvider
5
7
{
6
8
const string LAST_LOAD_KEY = "__LastLoaded" ;
9
+
7
10
public override void Load ( )
8
11
{
9
12
base . Load ( ) ;
@@ -38,6 +41,7 @@ public DateTimeOffset? LastLoaded
38
41
}
39
42
40
43
44
+ HttpClient ? httpClient ;
41
45
readonly SemaphoreSlim semaphore = new ( 1 ) ;
42
46
public async Task LoadAsync ( CancellationToken cancellationToken = default )
43
47
{
@@ -57,18 +61,34 @@ public async Task LoadAsync(CancellationToken cancellationToken = default)
57
61
// if (wasWaiting)
58
62
// return;
59
63
60
- var httpClient = new HttpClient ( ) ;
61
- var content = await httpClient . GetStringAsync ( config . Uri , cancellationToken ) ;
62
-
63
- await File
64
- . WriteAllTextAsync ( config . ConfigurationFilePath , content , cancellationToken )
65
- . ConfigureAwait ( false ) ;
64
+ if ( getData == null )
65
+ {
66
+ this . httpClient ??= new ( ) ;
67
+ var content = await this . httpClient
68
+ . GetStringAsync ( config . Uri , cancellationToken )
69
+ . ConfigureAwait ( false ) ;
70
+ }
71
+ else
72
+ {
73
+ var obj = await getData . Invoke ( cancellationToken ) . ConfigureAwait ( false ) ;
74
+ var json = JsonSerializer . Serialize ( obj ) ;
75
+ await this . WriteJson ( json , cancellationToken ) ;
76
+ }
66
77
67
- this . LastLoaded = DateTimeOffset . UtcNow ;
68
78
}
69
79
finally
70
80
{
71
81
this . semaphore . Release ( ) ;
72
82
}
73
83
}
84
+
85
+
86
+ async Task WriteJson ( string json , CancellationToken cancellationToken )
87
+ {
88
+ await File
89
+ . WriteAllTextAsync ( config . ConfigurationFilePath , json , cancellationToken )
90
+ . ConfigureAwait ( false ) ;
91
+
92
+ this . LastLoaded = DateTimeOffset . UtcNow ;
93
+ }
74
94
}
0 commit comments