Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overload in Progress.Create, Progress.CreateOnlyValueChanged #625

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/UniTask/Assets/Plugins/UniTask/Runtime/Progress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public static IProgress<T> Create<T>(Action<T> handler)
return new AnonymousProgress<T>(handler);
}

public static IProgress<T> Create<T, TState>(TState state, Action<T, TState> handler)
{
if (handler == null) return NullProgress<T>.Instance;
return new AnonymousProgress<T, TState>(handler, state);
}

public static IProgress<T> CreateOnlyValueChanged<T>(Action<T> handler, IEqualityComparer<T> comparer = null)
{
if (handler == null) return NullProgress<T>.Instance;
Expand All @@ -25,6 +31,16 @@ public static IProgress<T> CreateOnlyValueChanged<T>(Action<T> handler, IEqualit
#endif
}

public static IProgress<T> CreateOnlyValueChanged<T, TState>(TState state, Action<T, TState> handler, IEqualityComparer<T> comparer = null)
{
if (handler == null) return NullProgress<T>.Instance;
#if UNITY_2018_3_OR_NEWER
return new OnlyValueChangedProgress<T, TState>(handler, state, comparer ?? UnityEqualityComparer.GetDefault<T>());
#else
return new OnlyValueChangedProgress<T, TState>(handler, state, comparer ?? EqualityComparer<T>.Default);
#endif
}

sealed class NullProgress<T> : IProgress<T>
{
public static readonly IProgress<T> Instance = new NullProgress<T>();
Expand Down Expand Up @@ -54,6 +70,23 @@ public void Report(T value)
}
}

sealed class AnonymousProgress<T, TState> : IProgress<T>
{
readonly Action<T, TState> action;
readonly TState state;

public AnonymousProgress(Action<T, TState> action, TState state)
{
this.action = action;
this.state = state;
}

public void Report(T value)
{
action(value, state);
}
}

sealed class OnlyValueChangedProgress<T> : IProgress<T>
{
readonly Action<T> action;
Expand Down Expand Up @@ -83,5 +116,37 @@ public void Report(T value)
action(value);
}
}

sealed class OnlyValueChangedProgress<T, TState> : IProgress<T>
{
readonly Action<T, TState> action;
readonly TState state;
readonly IEqualityComparer<T> comparer;
bool isFirstCall;
T latestValue;

public OnlyValueChangedProgress(Action<T, TState> action, TState state, IEqualityComparer<T> comparer)
{
this.action = action;
this.state = state;
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, state);
}
}
}
}