Browse Source

Add methods for scheduling `AcediaConfig` saving

core_refactor
Anton Tarasenko 2 years ago
parent
commit
36c8f7f65a
  1. 49
      sources/Config/AcediaConfig.uc

49
sources/Config/AcediaConfig.uc

@ -67,6 +67,9 @@ class AcediaConfig extends AcediaObject
// was detected in config, but not yet loaded. // was detected in config, but not yet loaded.
// Only its default value is ever used. // Only its default value is ever used.
var private HashTable existingConfigs; var private HashTable existingConfigs;
// TODO: comment and add static cleanup
var private array<AcediaConfig> clearQueue;
var private bool syncScheduled;
// Stores name of the config where settings are to be stored. // Stores name of the config where settings are to be stored.
// Must correspond to value in `config(...)` modifier in class definition. // 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; new(none, NameToStorageVersion(name.ToString())) default.class;
newConfig._ = __(); newConfig._ = __();
newConfig.DefaultIt(); newConfig.DefaultIt();
newConfig.SaveConfig(); newConfig.SyncSave();
default.existingConfigs.SetItem(name, newConfig); default.existingConfigs.SetItem(name, newConfig);
name.FreeSelf(); name.FreeSelf();
return true; return true;
@ -212,14 +215,18 @@ public final static function bool Exists(BaseText name)
*/ */
public final static function DeleteConfig(BaseText name) public final static function DeleteConfig(BaseText name)
{ {
local AcediaObject value; local AcediaConfig value;
if (name == none) return; if (name == none) return;
if (default.existingConfigs == none) return; if (default.existingConfigs == none) return;
name = name.LowerCopy(); name = name.LowerCopy();
value = default.existingConfigs.TakeItem(name); value = AcediaConfig(default.existingConfigs.TakeItem(name));
if (value != none) { if (value != none)
value.ClearConfig(); {
__().scheduler.RequestDiskAccess(default.existingConfigs).connect =
HandleClearQueue;
default.clearQueue[default.clearQueue.length] = value;
} }
__().memory.Free(name); __().memory.Free(name);
} }
@ -317,8 +324,38 @@ public final static function SaveData(BaseText name, HashTable data)
if (requiredConfig != none) if (requiredConfig != none)
{ {
requiredConfig.FromData(data); 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 defaultproperties

Loading…
Cancel
Save