From 29eba352c8ca19aaa85eb0d2c326237828e1726f Mon Sep 17 00:00:00 2001 From: Luca Date: Tue, 23 Jul 2024 10:26:02 +0200 Subject: [PATCH] Debounce config saving --- ShockOsc/Config/ConfigManager.cs | 33 +++++++++++-------- ShockOsc/Services/AuthService.cs | 2 +- ShockOsc/Services/UnderscoreConfig.cs | 20 +++++------ .../Pages/Authentication/Authenticate.razor | 2 +- .../Ui/Pages/Authentication/LoginPart.razor | 2 +- .../Pages/Dash/Components/UpdateDialog.razor | 4 +-- .../Ui/Pages/Dash/Tabs/AppSettingsTab.razor | 6 ++-- ShockOsc/Ui/Pages/Dash/Tabs/ChatboxTab.razor | 6 ++-- ShockOsc/Ui/Pages/Dash/Tabs/ConfigTab.razor | 3 +- ShockOsc/Ui/Pages/Dash/Tabs/GroupsTab.razor | 28 ++++++++-------- ShockOsc/Ui/Pages/Dash/Tabs/ShockersTab.razor | 4 +-- 11 files changed, 59 insertions(+), 51 deletions(-) diff --git a/ShockOsc/Config/ConfigManager.cs b/ShockOsc/Config/ConfigManager.cs index 302a83c..e069ac8 100644 --- a/ShockOsc/Config/ConfigManager.cs +++ b/ShockOsc/Config/ConfigManager.cs @@ -8,18 +8,23 @@ namespace OpenShock.ShockOsc.Config; public sealed class ConfigManager { - private static readonly string Path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\OpenShock\ShockOSC\config.json"; - + private static readonly string Path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + + @"\OpenShock\ShockOSC\config.json"; + private readonly ILogger _logger; public ShockOscConfig Config { get; } + private readonly Timer _saveTimer; + public ConfigManager(ILogger logger) { _logger = logger; + _saveTimer = new Timer(_ => { OsTask.Run(SaveInternally); }); + // Load config ShockOscConfig? config = null; - + _logger.LogInformation("Config file found, trying to load config from {Path}", Path); if (File.Exists(Path)) { @@ -47,9 +52,11 @@ public ConfigManager(ILogger logger) _logger.LogInformation("Successfully loaded config"); return; } - _logger.LogInformation("No config file found (does not exist or empty or invalid), generating new one at {Path}", Path); + + _logger.LogInformation( + "No config file found (does not exist or empty or invalid), generating new one at {Path}", Path); Config = new ShockOscConfig(); - SaveAsync().Wait(); + SaveInternally().Wait(); _logger.LogInformation("New configuration file generated!"); } @@ -58,10 +65,10 @@ public ConfigManager(ILogger logger) WriteIndented = true, Converters = { new JsonStringEnumConverter(), new SemVersionJsonConverter() } }; - + private readonly SemaphoreSlim _saveLock = new(1, 1); - public async Task SaveAsync() + private async Task SaveInternally() { await _saveLock.WaitAsync().ConfigureAwait(false); try @@ -70,14 +77,14 @@ public async Task SaveAsync() var directory = System.IO.Path.GetDirectoryName(Path); if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) Directory.CreateDirectory(directory); - + await File.WriteAllTextAsync(Path, JsonSerializer.Serialize(Config, Options)).ConfigureAwait(false); _logger.LogInformation("Config saved"); } catch (Exception e) { _logger.LogError(e, "Error occurred while saving new config file"); - } + } finally { _saveLock.Release(); @@ -86,9 +93,9 @@ public async Task SaveAsync() public void Save() { - SaveAsync().Wait(); + lock (_saveTimer) + { + _saveTimer.Change(TimeSpan.FromSeconds(1), Timeout.InfiniteTimeSpan); + } } - - public void SaveFnf() => OsTask.Run(SaveAsync); - } \ No newline at end of file diff --git a/ShockOsc/Services/AuthService.cs b/ShockOsc/Services/AuthService.cs index 48acc89..b2033d6 100644 --- a/ShockOsc/Services/AuthService.cs +++ b/ShockOsc/Services/AuthService.cs @@ -66,7 +66,7 @@ public async Task Logout() Authenticated = false; _configManager.Config.OpenShock.Token = string.Empty; - await _configManager.SaveAsync(); + _configManager.Save(); _logger.LogInformation("Logging out"); await _hubClient.StopAsync(); diff --git a/ShockOsc/Services/UnderscoreConfig.cs b/ShockOsc/Services/UnderscoreConfig.cs index 41a3466..4b41a71 100644 --- a/ShockOsc/Services/UnderscoreConfig.cs +++ b/ShockOsc/Services/UnderscoreConfig.cs @@ -85,7 +85,7 @@ private void HandleGlobalConfigCommand(string action, object? value) { if (_configManager.Config.Behaviour.RandomIntensity == modeIntensity) return; _configManager.Config.Behaviour.RandomIntensity = modeIntensity; - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } break; @@ -95,7 +95,7 @@ private void HandleGlobalConfigCommand(string action, object? value) { if(_configManager.Config.Behaviour.RandomDuration == modeDuration) return; _configManager.Config.Behaviour.RandomDuration = modeDuration; - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } break; @@ -111,7 +111,7 @@ private void HandleGlobalConfigCommand(string action, object? value) _configManager.Config.Behaviour.FixedIntensity = Math.Clamp((byte)Math.Round(intensityFloat * 100), (byte)0, (byte)100); ValidateSettings(); - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } @@ -128,7 +128,7 @@ private void HandleGlobalConfigCommand(string action, object? value) _configManager.Config.Behaviour.IntensityRange.Min = MathUtils.ClampUint((uint)Math.Round(minIntensityFloat * 100), 0, 100); ValidateSettings(); - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } @@ -145,7 +145,7 @@ private void HandleGlobalConfigCommand(string action, object? value) _configManager.Config.Behaviour.IntensityRange.Max = MathUtils.ClampUint((uint)Math.Round(maxIntensityFloat * 100), 0, 100); ValidateSettings(); - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } @@ -161,7 +161,7 @@ private void HandleGlobalConfigCommand(string action, object? value) _configManager.Config.Behaviour.DurationRange.Min = MathUtils.ClampUint((uint)Math.Round(minDurationFloat * 10_000), 300, 30_000); ValidateSettings(); - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } @@ -177,7 +177,7 @@ private void HandleGlobalConfigCommand(string action, object? value) _configManager.Config.Behaviour.DurationRange.Max = MathUtils.ClampUint((uint)Math.Round(maxDurationFloat * 10_000), 300, 30_000); ValidateSettings(); - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } @@ -193,7 +193,7 @@ private void HandleGlobalConfigCommand(string action, object? value) _configManager.Config.Behaviour.FixedDuration = MathUtils.ClampUint((uint)Math.Round(durationFloat * 10_000), 300, 10_000); ValidateSettings(); - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } @@ -210,7 +210,7 @@ private void HandleGlobalConfigCommand(string action, object? value) _configManager.Config.Behaviour.CooldownTime = MathUtils.ClampUint((uint)Math.Round(cooldownTimeFloat * 100000), 0, 100000); ValidateSettings(); - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } @@ -226,7 +226,7 @@ private void HandleGlobalConfigCommand(string action, object? value) _configManager.Config.Behaviour.HoldTime = MathUtils.ClampUint((uint)Math.Round(holdTimeFloat * 1000), 0, 1000); ValidateSettings(); - _configManager.SaveFnf(); + _configManager.Save(); OnConfigUpdate?.Invoke(); // update Ui } diff --git a/ShockOsc/Ui/Pages/Authentication/Authenticate.razor b/ShockOsc/Ui/Pages/Authentication/Authenticate.razor index daf5ac5..6df63ba 100644 --- a/ShockOsc/Ui/Pages/Authentication/Authenticate.razor +++ b/ShockOsc/Ui/Pages/Authentication/Authenticate.razor @@ -77,7 +77,7 @@ { if (string.IsNullOrEmpty(PipeService.Token) || _currentState != State.Login) return false; ConfigManager.Config.OpenShock.Token = PipeService.Token; - await ConfigManager.SaveAsync(); + ConfigManager.Save(); PipeService.Token = null; await ProceedAuthenticated(); return true; diff --git a/ShockOsc/Ui/Pages/Authentication/LoginPart.razor b/ShockOsc/Ui/Pages/Authentication/LoginPart.razor index 100b3c2..cb950ae 100644 --- a/ShockOsc/Ui/Pages/Authentication/LoginPart.razor +++ b/ShockOsc/Ui/Pages/Authentication/LoginPart.razor @@ -87,7 +87,7 @@ public async Task Login() { - await ConfigManager.SaveAsync(); + ConfigManager.Save(); await ProceedAuthenticated(); } diff --git a/ShockOsc/Ui/Pages/Dash/Components/UpdateDialog.razor b/ShockOsc/Ui/Pages/Dash/Components/UpdateDialog.razor index 6764c2c..49f7e44 100644 --- a/ShockOsc/Ui/Pages/Dash/Components/UpdateDialog.razor +++ b/ShockOsc/Ui/Pages/Dash/Components/UpdateDialog.razor @@ -21,10 +21,10 @@ InvokeAsync(StateHasChanged); } - private async Task Skip() + private void Skip() { ConfigManager.Config.App.LastIgnoredVersion = Updater.LatestVersion; - await ConfigManager.SaveAsync(); + ConfigManager.Save(); MudDialog.Close(DialogResult.Ok(true)); } diff --git a/ShockOsc/Ui/Pages/Dash/Tabs/AppSettingsTab.razor b/ShockOsc/Ui/Pages/Dash/Tabs/AppSettingsTab.razor index 55ed8df..ecf5c10 100644 --- a/ShockOsc/Ui/Pages/Dash/Tabs/AppSettingsTab.razor +++ b/ShockOsc/Ui/Pages/Dash/Tabs/AppSettingsTab.razor @@ -58,14 +58,14 @@ InvokeAsync(StateHasChanged); } - private async Task OnSettingsValueChange() + private void OnSettingsValueChange() { - await ConfigManager.SaveAsync(); + ConfigManager.Save(); } private async Task UpdateChannelChanged() { - await OnSettingsValueChange(); + OnSettingsValueChange(); await Updater.CheckUpdate(); } diff --git a/ShockOsc/Ui/Pages/Dash/Tabs/ChatboxTab.razor b/ShockOsc/Ui/Pages/Dash/Tabs/ChatboxTab.razor index f3e024f..fd72786 100644 --- a/ShockOsc/Ui/Pages/Dash/Tabs/ChatboxTab.razor +++ b/ShockOsc/Ui/Pages/Dash/Tabs/ChatboxTab.razor @@ -14,7 +14,7 @@

- + Timeout: @MathF.Round(ConfigManager.Config.Chatbox.Timeout / 1000f, 1).ToString(CultureInfo.InvariantCulture)s @@ -61,9 +61,9 @@ @code { - private async Task OnSettingsValueChange() + private void OnSettingsValueChange() { - await ConfigManager.SaveAsync(); + ConfigManager.Save(); } } \ No newline at end of file diff --git a/ShockOsc/Ui/Pages/Dash/Tabs/ConfigTab.razor b/ShockOsc/Ui/Pages/Dash/Tabs/ConfigTab.razor index 86f094e..571133d 100644 --- a/ShockOsc/Ui/Pages/Dash/Tabs/ConfigTab.razor +++ b/ShockOsc/Ui/Pages/Dash/Tabs/ConfigTab.razor @@ -122,7 +122,8 @@ private Task OnSettingsValueChangeAsync() { - return Task.WhenAll(ConfigManager.SaveAsync(), underscoreConfig.SendUpdateForAll()); + ConfigManager.Save(); + return underscoreConfig.SendUpdateForAll(); } protected override void OnInitialized() diff --git a/ShockOsc/Ui/Pages/Dash/Tabs/GroupsTab.razor b/ShockOsc/Ui/Pages/Dash/Tabs/GroupsTab.razor index 597bd5f..57bf15d 100644 --- a/ShockOsc/Ui/Pages/Dash/Tabs/GroupsTab.razor +++ b/ShockOsc/Ui/Pages/Dash/Tabs/GroupsTab.razor @@ -30,7 +30,7 @@ Group = groupId; await InvokeAsync(StateHasChanged); - await ConfigManager.SaveAsync(); + ConfigManager.Save(); await ShockOsc.RaiseOnGroupsChanged(); } @@ -41,17 +41,17 @@ ConfigManager.Config.Groups.Remove(Group.Value); Group = null; await InvokeAsync(StateHasChanged); - await ConfigManager.SaveAsync(); + ConfigManager.Save(); await ShockOsc.RaiseOnGroupsChanged(); } private async Task OnSettingsValueChange() { - await ConfigManager.SaveAsync(); + ConfigManager.Save(); await ShockOsc.RaiseOnGroupsChanged(); } - private Task OnGroupSettingsValueChange() => ConfigManager.SaveAsync(); + private void OnGroupSettingsValueChange() => ConfigManager.Save(); private void OnGroupSelect() { @@ -60,12 +60,12 @@ InvokeAsync(StateHasChanged); } - private async Task OnSelectedShockersUpdate() + private void OnSelectedShockersUpdate() { if (CurrentGroup != null) { CurrentGroup.Shockers = _selectedShockers.ToList(); - await ConfigManager.SaveAsync(); + ConfigManager.Save(); } } @@ -112,7 +112,7 @@ CurrentGroup.RandomIntensity = ConfigManager.Config.Behaviour.RandomIntensity; await InvokeAsync(StateHasChanged); - await ConfigManager.SaveAsync(); + ConfigManager.Save(); } private async Task ResetDuration() @@ -123,7 +123,7 @@ CurrentGroup.RandomDuration = ConfigManager.Config.Behaviour.RandomDuration; await InvokeAsync(StateHasChanged); - await ConfigManager.SaveAsync(); + ConfigManager.Save(); } } @@ -167,7 +167,7 @@ @if (!CurrentGroup.RandomIntensity) { + OnSaveAction="_ => ConfigManager.Save()"> Intensity: @CurrentGroup.FixedIntensity% } @@ -175,13 +175,13 @@ { + OnSaveAction="_ => ConfigManager.Save()"> Min Intensity: @CurrentGroup.IntensityRange.Min% + OnSaveAction="_ => ConfigManager.Save()"> Max Intensity: @CurrentGroup.IntensityRange.Max% } @@ -208,7 +208,7 @@ @if (!CurrentGroup.RandomDuration) { Duration: @MathF.Round(CurrentGroup.FixedDuration / 1000f, 1).ToString(CultureInfo.InvariantCulture)s @@ -217,14 +217,14 @@ { Min Duration: @MathF.Round(CurrentGroup.DurationRange.Min / 1000f, 1).ToString(CultureInfo.InvariantCulture)s Max Duration: @MathF.Round(CurrentGroup.DurationRange.Max / 1000f, 1).ToString(CultureInfo.InvariantCulture)s diff --git a/ShockOsc/Ui/Pages/Dash/Tabs/ShockersTab.razor b/ShockOsc/Ui/Pages/Dash/Tabs/ShockersTab.razor index e507c38..b0ffec5 100644 --- a/ShockOsc/Ui/Pages/Dash/Tabs/ShockersTab.razor +++ b/ShockOsc/Ui/Pages/Dash/Tabs/ShockersTab.razor @@ -31,9 +31,9 @@ @code { - private Task OnShockerConfigUpdate() + private void OnShockerConfigUpdate() { - return ConfigManager.SaveAsync(); + ConfigManager.Save(); } } \ No newline at end of file