From 36c8f7f65a06204ea2e7e67e4d941dd2b45c288e Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 6 Mar 2023 03:32:50 +0700 Subject: [PATCH] Add methods for scheduling `AcediaConfig` saving --- sources/Config/AcediaConfig.uc | 49 +++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/sources/Config/AcediaConfig.uc b/sources/Config/AcediaConfig.uc index 69a3b89..0a25beb 100644 --- a/sources/Config/AcediaConfig.uc +++ b/sources/Config/AcediaConfig.uc @@ -67,6 +67,9 @@ class AcediaConfig extends AcediaObject // was detected in config, but not yet loaded. // Only its default value is ever used. var private HashTable existingConfigs; +// TODO: comment and add static cleanup +var private array clearQueue; +var private bool syncScheduled; // Stores name of the config where settings are to be stored. // Must correspond to value in `config(...)` modifier in class definition. @@ -174,7 +177,7 @@ public final static function bool NewConfig(BaseText name) new(none, NameToStorageVersion(name.ToString())) default.class; newConfig._ = __(); newConfig.DefaultIt(); - newConfig.SaveConfig(); + newConfig.SyncSave(); default.existingConfigs.SetItem(name, newConfig); name.FreeSelf(); return true; @@ -212,14 +215,18 @@ public final static function bool Exists(BaseText name) */ public final static function DeleteConfig(BaseText name) { - local AcediaObject value; + local AcediaConfig value; + if (name == none) return; if (default.existingConfigs == none) return; name = name.LowerCopy(); - value = default.existingConfigs.TakeItem(name); - if (value != none) { - value.ClearConfig(); + value = AcediaConfig(default.existingConfigs.TakeItem(name)); + if (value != none) + { + __().scheduler.RequestDiskAccess(default.existingConfigs).connect = + HandleClearQueue; + default.clearQueue[default.clearQueue.length] = value; } __().memory.Free(name); } @@ -317,8 +324,38 @@ public final static function SaveData(BaseText name, HashTable data) if (requiredConfig != none) { requiredConfig.FromData(data); - requiredConfig.SaveConfig(); + requiredConfig.SyncSave(); + } +} + +/** + * Synchronizes current in-memory data by saving it onto the disk (into + * the config file). Can be performed asynchronously (actual saving can be + * postponed for performance reasons). + */ +public final function SyncSave() +{ + if (syncScheduled) { + return; + } + syncScheduled = true; + __().scheduler.RequestDiskAccess(default.existingConfigs).connect = DoSync; +} + +// Does actual saving +private final function DoSync() +{ + syncScheduled = false; + SaveConfig(); +} + +private final static function HandleClearQueue() +{ + if (default.clearQueue.length <= 0) { + return; } + default.clearQueue[0].ClearConfig(); + default.clearQueue.Remove(0, 1); } defaultproperties