using System;
using System.Collections.Generic;
namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
{
/// A watcher which detects changes to a value using a specified instance.
/// The comparable value type.
internal class ComparableWatcher : IValueWatcher
{
/*********
** Fields
*********/
/// Get the current value.
private readonly Func GetValue;
/// The equality comparer.
private readonly IEqualityComparer Comparer;
/*********
** Accessors
*********/
///
public string Name { get; }
///
public TValue PreviousValue { get; private set; }
///
public TValue CurrentValue { get; private set; }
///
public bool IsChanged { get; private set; }
/*********
** Public methods
*********/
/// Construct an instance.
/// A name which identifies what the watcher is watching, used for troubleshooting.
/// Get the current value.
/// The equality comparer which indicates whether two values are the same.
public ComparableWatcher(string name, Func getValue, IEqualityComparer comparer)
{
this.Name = name;
this.GetValue = getValue;
this.Comparer = comparer;
this.CurrentValue = getValue();
this.PreviousValue = this.CurrentValue;
}
///
public void Update()
{
this.CurrentValue = this.GetValue();
this.IsChanged = !this.Comparer.Equals(this.PreviousValue, this.CurrentValue);
}
///
public void Reset()
{
this.PreviousValue = this.CurrentValue;
this.IsChanged = false;
}
///
public void Dispose() { }
}
}