summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/StateTracking/FieldWatchers
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-11-24 13:49:30 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-11-24 13:49:30 -0500
commita3f21685049cabf2d824c8060dc0b1de47e9449e (patch)
treead9add30e9da2a50e0ea0245f1546b7378f0d282 /src/SMAPI/Framework/StateTracking/FieldWatchers
parent6521df7b131924835eb797251c1e956fae0d6e13 (diff)
parent277bf082675b98b95bf6184fe3c7a45b969c7ac2 (diff)
downloadSMAPI-a3f21685049cabf2d824c8060dc0b1de47e9449e.tar.gz
SMAPI-a3f21685049cabf2d824c8060dc0b1de47e9449e.tar.bz2
SMAPI-a3f21685049cabf2d824c8060dc0b1de47e9449e.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers')
-rw-r--r--src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs2
-rw-r--r--src/SMAPI/Framework/StateTracking/FieldWatchers/ImmutableCollectionWatcher.cs37
-rw-r--r--src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs35
3 files changed, 62 insertions, 12 deletions
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs
index 6550f950..32ec8c7e 100644
--- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs
+++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs
@@ -53,7 +53,7 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
{
this.AssertNotDisposed();
- // optimise for zero items
+ // optimize for zero items
if (this.CurrentValues.Count == 0)
{
if (this.LastValues.Count > 0)
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ImmutableCollectionWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ImmutableCollectionWatcher.cs
new file mode 100644
index 00000000..30e6274f
--- /dev/null
+++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ImmutableCollectionWatcher.cs
@@ -0,0 +1,37 @@
+using System.Collections.Generic;
+
+namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
+{
+ /// <summary>A collection watcher which never changes.</summary>
+ /// <typeparam name="TValue">The value type within the collection.</typeparam>
+ internal class ImmutableCollectionWatcher<TValue> : BaseDisposableWatcher, ICollectionWatcher<TValue>
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>A singleton collection watcher instance.</summary>
+ public static ImmutableCollectionWatcher<TValue> Instance { get; } = new ImmutableCollectionWatcher<TValue>();
+
+ /// <summary>Whether the collection changed since the last reset.</summary>
+ public bool IsChanged { get; } = false;
+
+ /// <summary>The values added since the last reset.</summary>
+ public IEnumerable<TValue> Added { get; } = new TValue[0];
+
+ /// <summary>The values removed since the last reset.</summary>
+ public IEnumerable<TValue> Removed { get; } = new TValue[0];
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Update the current value if needed.</summary>
+ public void Update() { }
+
+ /// <summary>Set the current value as the baseline.</summary>
+ public void Reset() { }
+
+ /// <summary>Stop watching the field and release all references.</summary>
+ public override void Dispose() { }
+ }
+}
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs
index 8301351e..314ff7f5 100644
--- a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs
+++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs
@@ -12,10 +12,13 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
/*********
** Public methods
*********/
+ /****
+ ** Values
+ ****/
/// <summary>Get a watcher which compares values using their <see cref="object.Equals(object)"/> method. This method should only be used when <see cref="ForEquatable{T}"/> won't work, since this doesn't validate whether they're comparable.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="getValue">Get the current value.</param>
- public static ComparableWatcher<T> ForGenericEquality<T>(Func<T> getValue) where T : struct
+ public static IValueWatcher<T> ForGenericEquality<T>(Func<T> getValue) where T : struct
{
return new ComparableWatcher<T>(getValue, new GenericEqualsComparer<T>());
}
@@ -23,7 +26,7 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
/// <summary>Get a watcher for an <see cref="IEquatable{T}"/> value.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="getValue">Get the current value.</param>
- public static ComparableWatcher<T> ForEquatable<T>(Func<T> getValue) where T : IEquatable<T>
+ public static IValueWatcher<T> ForEquatable<T>(Func<T> getValue) where T : IEquatable<T>
{
return new ComparableWatcher<T>(getValue, new EquatableComparer<T>());
}
@@ -31,15 +34,27 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
/// <summary>Get a watcher which detects when an object reference changes.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="getValue">Get the current value.</param>
- public static ComparableWatcher<T> ForReference<T>(Func<T> getValue)
+ public static IValueWatcher<T> ForReference<T>(Func<T> getValue)
{
return new ComparableWatcher<T>(getValue, new ObjectReferenceComparer<T>());
}
+ /// <summary>Get a watcher for a net collection.</summary>
+ /// <typeparam name="T">The value type.</typeparam>
+ /// <typeparam name="TSelf">The net field instance type.</typeparam>
+ /// <param name="field">The net collection.</param>
+ public static IValueWatcher<T> ForNetValue<T, TSelf>(NetFieldBase<T, TSelf> field) where TSelf : NetFieldBase<T, TSelf>
+ {
+ return new NetValueWatcher<T, TSelf>(field);
+ }
+
+ /****
+ ** Collections
+ ****/
/// <summary>Get a watcher which detects when an object reference in a collection changes.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="collection">The observable collection.</param>
- public static ComparableListWatcher<T> ForReferenceList<T>(ICollection<T> collection)
+ public static ICollectionWatcher<T> ForReferenceList<T>(ICollection<T> collection)
{
return new ComparableListWatcher<T>(collection, new ObjectReferenceComparer<T>());
}
@@ -47,24 +62,22 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
/// <summary>Get a watcher for an observable collection.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="collection">The observable collection.</param>
- public static ObservableCollectionWatcher<T> ForObservableCollection<T>(ObservableCollection<T> collection)
+ public static ICollectionWatcher<T> ForObservableCollection<T>(ObservableCollection<T> collection)
{
return new ObservableCollectionWatcher<T>(collection);
}
- /// <summary>Get a watcher for a net collection.</summary>
+ /// <summary>Get a watcher for a collection that never changes.</summary>
/// <typeparam name="T">The value type.</typeparam>
- /// <typeparam name="TSelf">The net field instance type.</typeparam>
- /// <param name="field">The net collection.</param>
- public static NetValueWatcher<T, TSelf> ForNetValue<T, TSelf>(NetFieldBase<T, TSelf> field) where TSelf : NetFieldBase<T, TSelf>
+ public static ICollectionWatcher<T> ForImmutableCollection<T>()
{
- return new NetValueWatcher<T, TSelf>(field);
+ return ImmutableCollectionWatcher<T>.Instance;
}
/// <summary>Get a watcher for a net collection.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="collection">The net collection.</param>
- public static NetCollectionWatcher<T> ForNetCollection<T>(NetCollection<T> collection) where T : class, INetObject<INetSerializable>
+ public static ICollectionWatcher<T> ForNetCollection<T>(NetCollection<T> collection) where T : class, INetObject<INetSerializable>
{
return new NetCollectionWatcher<T>(collection);
}