52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using Godot;
|
|
using Nocturnis.DataStructures.Data;
|
|
using Nocturnis.DataStructures.DataTypes;
|
|
using Nocturnis.GlobalManagement.Constants;
|
|
using Nocturnis.GlobalManagement.Providers;
|
|
using Skeleton.DataStructure;
|
|
|
|
namespace Nocturnis.DataStructures;
|
|
|
|
public class DataCache : CacheItem<DataVariable>
|
|
{
|
|
public new static DataCache Null => new (x => (null, DataTypeConstant.BaseDataTypes.Null));
|
|
|
|
public DataCache(Func<CacheItem, DataVariable> rec)
|
|
{
|
|
Value = new DataVariable();
|
|
ProxyCalculator = c => (rec(c).Data, rec(c).Type);
|
|
}
|
|
|
|
public DataCache(Func<CacheItem, (object, DataType)> rec)
|
|
{
|
|
Value = new DataVariable();
|
|
ProxyCalculator = rec;
|
|
}
|
|
|
|
private new Func<CacheItem, (object, DataType)> ProxyCalculator { get; set; }
|
|
|
|
public override DataVariable Get
|
|
{
|
|
get
|
|
{
|
|
if (Expired)
|
|
{
|
|
(object val, DataType type) = ProxyCalculator(this);
|
|
Value!.Type.Assign(type);
|
|
Value.Data = val;
|
|
}
|
|
|
|
return Value;
|
|
}
|
|
}
|
|
|
|
public void UpdateCalculation(Func<CacheItem, (object, DataType)> rec)
|
|
{
|
|
Expire();
|
|
foreach (CacheItem item in Dependencies)
|
|
item.References.Remove(this);
|
|
Dependencies = new HashSet<CacheItem>();
|
|
ProxyCalculator = rec;
|
|
}
|
|
|
|
} |