namespace Zinnia.Data.Operation.Cache
{
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Zinnia.Extension;
///
/// Caches data and invokves an appropriate event when the cache is updated.
///
/// The type of data to cache.
/// The event to invoke.
[Obsolete("Use `Zinnia.Data.Type.Observer.ObservableProperty` instead.")]
public abstract class ValueCache : MonoBehaviour where TEvent : UnityEvent, new()
{
///
/// Emitted when the cached data is updated and has been modified to a new value.
///
public TEvent Modified = new TEvent();
///
/// Emitted when the cached data is updated but the value is unmodified.
///
public TEvent Unmodified = new TEvent();
///
/// The cache of data.
///
public TValue Data
{
get
{
return data;
}
set
{
if (!this.IsValidState())
{
return;
}
if (AreEqual(value, data))
{
Unmodified?.Invoke(value);
}
else
{
Modified?.Invoke(value);
}
data = value;
}
}
///
/// The backing field for .
///
private TValue data;
///
/// Clears the cache by setting it to the default value.
///
public virtual void ClearCache()
{
if (!this.IsValidState())
{
return;
}
Data = default;
}
///
/// Determines if the two given values are considered equal.
///
/// The first value to check.
/// The second value to check.
/// Whether the two given values are considered equal.
protected virtual bool AreEqual(TValue a, TValue b)
{
return EqualityComparer.Default.Equals(a, b);
}
}
}