You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
869 B
34 lines
869 B
1 year ago
|
using AsyncAwaitBestPractices;
|
||
|
using StabilityMatrix.Core.Services;
|
||
1 year ago
|
|
||
1 year ago
|
namespace StabilityMatrix.Core.Models.Settings;
|
||
1 year ago
|
|
||
|
/// <summary>
|
||
|
/// Transaction object which saves settings manager changes when disposed.
|
||
|
/// </summary>
|
||
|
public class SettingsTransaction : IDisposable, IAsyncDisposable
|
||
|
{
|
||
|
private readonly ISettingsManager settingsManager;
|
||
|
private readonly Func<Task> onCommit;
|
||
|
|
||
|
public Settings Settings => settingsManager.Settings;
|
||
|
|
||
|
public SettingsTransaction(ISettingsManager settingsManager, Func<Task> onCommit)
|
||
|
{
|
||
|
this.settingsManager = settingsManager;
|
||
|
this.onCommit = onCommit;
|
||
|
}
|
||
|
|
||
|
public void Dispose()
|
||
|
{
|
||
|
onCommit().SafeFireAndForget();
|
||
|
GC.SuppressFinalize(this);
|
||
|
}
|
||
|
|
||
|
public async ValueTask DisposeAsync()
|
||
|
{
|
||
|
await onCommit();
|
||
|
GC.SuppressFinalize(this);
|
||
|
}
|
||
|
}
|