#if NET_4_6 || NET_STANDARD_2_0 || CSHARP_7_OR_LATER
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Collections.Generic;
using UniRx.Async.Internal;
namespace UniRx.Async
{
///
/// Lightweight IProgress[T] factory.
///
public static class Progress
{
public static IProgress Create(Action handler)
{
if (handler == null) return NullProgress.Instance;
return new AnonymousProgress(handler);
}
public static IProgress CreateOnlyValueChanged(Action handler, IEqualityComparer comparer = null)
{
if (handler == null) return NullProgress.Instance;
return new OnlyValueChangedProgress(handler, comparer ?? UnityEqualityComparer.GetDefault());
}
sealed class NullProgress : IProgress
{
public static readonly IProgress Instance = new NullProgress();
NullProgress()
{
}
public void Report(T value)
{
}
}
sealed class AnonymousProgress : IProgress
{
readonly Action action;
public AnonymousProgress(Action action)
{
this.action = action;
}
public void Report(T value)
{
action(value);
}
}
sealed class OnlyValueChangedProgress : IProgress
{
readonly Action action;
readonly IEqualityComparer comparer;
bool isFirstCall;
T latestValue;
public OnlyValueChangedProgress(Action action, IEqualityComparer comparer)
{
this.action = action;
this.comparer = comparer;
this.isFirstCall = true;
}
public void Report(T value)
{
if (isFirstCall)
{
isFirstCall = false;
}
else if (comparer.Equals(value, latestValue))
{
return;
}
latestValue = value;
action(value);
}
}
}
}
#endif