1
1
using Google . Protobuf . WellKnownTypes ;
2
+ using Serilog ;
2
3
using System ;
3
4
using System . Collections . Generic ;
4
5
using System . IO ;
@@ -30,11 +31,6 @@ namespace Coder.Desktop.App.Services;
30
31
/// <param name="ct"></param>
31
32
/// <returns></returns>
32
33
public Task Write ( T settings , CancellationToken ct = default ) ;
33
- /// <summary>
34
- /// Returns null if the settings are not cached or not available.
35
- /// </summary>
36
- /// <returns></returns>
37
- public T ? GetFromCache ( ) ;
38
34
}
39
35
40
36
/// <summary>
@@ -80,6 +76,12 @@ public SettingsManager(string? settingsFilePath = null)
80
76
81
77
public async Task < T > Read ( CancellationToken ct = default )
82
78
{
79
+ if ( _cachedSettings is notnull )
80
+ {
81
+ // return cached settings if available
82
+ return ( T ) _cachedSettings . Clone ( ) ;
83
+ }
84
+
83
85
// try to get the lock with short timeout
84
86
if ( ! await _gate . WaitAsync ( LockTimeout , ct ) . ConfigureAwait ( false ) )
85
87
throw new InvalidOperationException (
@@ -145,41 +147,39 @@ await File.WriteAllTextAsync(_settingsFilePath, json, ct)
145
147
_gate . Release ( ) ;
146
148
}
147
149
}
148
-
149
- public T ? GetFromCache ( )
150
- {
151
- return _cachedSettings ;
152
- }
153
150
}
154
151
155
152
public interface ISettings
156
153
{
157
154
/// <summary>
158
- ///Gets the version of the settingsschema .
155
+ ///FileName where the settingsare stored .
159
156
/// </summary>
160
- int Version { get ; }
157
+ static abstract string SettingsFileName { get ; }
161
158
162
159
/// <summary>
163
- ///FileName where the settingsare stored .
160
+ ///Gets the version of the settingsschema .
164
161
/// </summary>
165
- static abstract string SettingsFileName { get ; }
162
+ int Version { get ; }
163
+
164
+ ISettings Clone ( ) ;
166
165
}
167
166
168
167
/// <summary>
169
168
/// CoderConnect settings class that holds the settings for the CoderConnect feature.
170
169
/// </summary>
171
170
public class CoderConnectSettings : ISettings
172
171
{
172
+ public static string SettingsFileName { get ; } = "coder-connect-settings.json" ;
173
+ public int Version { get ; set ; }
174
+ public bool ConnectOnLaunch { get ; set ; }
175
+
173
176
/// <summary>
174
- /// CoderConnect settings version. Increment this when the settings schema changes.
177
+ /// CoderConnectcurrent settings version. Increment this when the settings schema changes.
175
178
/// In future iterations we will be able to handle migrations when the user has
176
179
/// an older version.
177
180
/// </summary>
178
- public int Version { get ; set ; }
179
- public bool ConnectOnLaunch { get ; set ; }
180
- public static string SettingsFileName { get ; } = "coder-connect-settings.json" ;
181
+ private const int VERSION = 1 ;
181
182
182
- private const int VERSION = 1 ; // Default version for backward compatibility
183
183
public CoderConnectSettings ( )
184
184
{
185
185
Version = VERSION ;
@@ -192,10 +192,13 @@ public CoderConnectSettings(int? version, bool connectOnLogin)
192
192
ConnectOnLaunch = connectOnLogin ;
193
193
}
194
194
195
+ ISettings ISettings . Clone ( )
196
+ {
197
+ return Clone ( ) ;
198
+ }
199
+
195
200
public CoderConnectSettings Clone ( )
196
201
{
197
202
return new CoderConnectSettings ( Version , ConnectOnLaunch ) ;
198
203
}
199
-
200
-
201
204
}