Skip to content

Commit

Permalink
Add AsyncUnityEventHandler.OnInvokeAsyncSuppressCancellationThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Aug 20, 2018
1 parent f8d8adc commit d335f45
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Assembly-CSharp-Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp-firstpass-vs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp-firstpass.Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp-firstpass.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp-vs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp.Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
4 changes: 4 additions & 0 deletions Assets/Plugins/UniRx/Scripts/Async/DiagnosticsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ static bool IgnoreLine(MethodBase methodInfo)
{
return true;
}
else if (declareType.StartsWith("UniRx.Async.CompilerServices"))
{
return true;
}
else if (declareType == "System.Threading.Tasks.AwaitTaskContinuation")
{
return true;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions Assets/Plugins/UniRx/Scripts/Async/UnityAsyncExtensions.uGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button butt
return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false);
}

public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler(button.onClick, cancellationToken, false);
}

public static UniTask OnClickAsync(this Button button)
{
return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
Expand All @@ -44,6 +49,11 @@ public static IAsyncValueChangedEventHandler<bool> GetAsyncValueChangedEventHand
return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy(), false);
}

public static IAsyncValueChangedEventHandler<bool> GetAsyncValueChangedEventHandler(this Toggle toggle, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, cancellationToken, false);
}

public static UniTask<bool> OnValueChangedAsync(this Toggle toggle)
{
return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
Expand All @@ -59,6 +69,11 @@ public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHan
return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy(), false);
}

public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHandler(this Scrollbar scrollbar, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, cancellationToken, false);
}

public static UniTask<float> OnValueChangedAsync(this Scrollbar scrollbar)
{
return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
Expand All @@ -74,6 +89,11 @@ public static IAsyncValueChangedEventHandler<Vector2> GetAsyncValueChangedEventH
return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy(), false);
}

public static IAsyncValueChangedEventHandler<Vector2> GetAsyncValueChangedEventHandler(this ScrollRect scrollRect, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, cancellationToken, false);
}

public static UniTask<Vector2> OnValueChangedAsync(this ScrollRect scrollRect)
{
return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
Expand All @@ -89,6 +109,11 @@ public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHan
return new AsyncUnityEventHandler<float>(slider.onValueChanged, slider.GetCancellationTokenOnDestroy(), false);
}

public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHandler(this Slider slider, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<float>(slider.onValueChanged, cancellationToken, false);
}

public static UniTask<float> OnValueChangedAsync(this Slider slider)
{
return new AsyncUnityEventHandler<float>(slider.onValueChanged, slider.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
Expand All @@ -104,6 +129,11 @@ public static IAsyncEndEditEventHandler<string> GetAsyncEndEditEventHandler(this
return new AsyncUnityEventHandler<string>(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), false);
}

public static IAsyncEndEditEventHandler<string> GetAsyncEndEditEventHandler(this InputField inputField, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<string>(inputField.onEndEdit, cancellationToken, false);
}

public static UniTask<string> OnEndEditAsync(this InputField inputField)
{
return new AsyncUnityEventHandler<string>(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
Expand All @@ -119,6 +149,11 @@ public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandl
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
}

public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, cancellationToken, false);
}

public static UniTask<int> OnValueChanged(this Dropdown dropdown)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
Expand All @@ -133,16 +168,19 @@ public static UniTask<int> OnValueChanged(this Dropdown dropdown, CancellationTo
public interface IAsyncClickEventHandler : IDisposable
{
UniTask OnClickAsync();
UniTask<bool> OnClickAsyncSuppressCancellationThrow();
}

public interface IAsyncValueChangedEventHandler<T> : IDisposable
{
UniTask<T> OnValueChangedAsync();
UniTask<(bool IsCanceled, T Result)> OnValueChangedAsyncSuppressCancellationThrow();
}

public interface IAsyncEndEditEventHandler<T> : IDisposable
{
UniTask<T> OnEndEditAsync();
UniTask<(bool IsCanceled, T Result)> OnEndEditAsyncSuppressCancellationThrow();
}

// event handler is reusable when callOnce = false.
Expand All @@ -156,6 +194,7 @@ public class AsyncUnityEventHandler : IAwaiter, IDisposable, IAsyncClickEventHan
CancellationTokenRegistration registration;
bool isDisposed;
bool callOnce;
UniTask<bool>? suppressCancellationThrowTask;

public AsyncUnityEventHandler(UnityEvent unityEvent, CancellationToken cancellationToken, bool callOnce)
{
Expand Down Expand Up @@ -185,6 +224,15 @@ public UniTask OnInvokeAsync()
return new UniTask(this);
}

public UniTask<bool> OnInvokeAsyncSuppressCancellationThrow()
{
if (suppressCancellationThrowTask == null)
{
suppressCancellationThrowTask = OnInvokeAsync().SuppressCancellationThrow();
}
return suppressCancellationThrowTask.Value;
}

void Invoke()
{
var c = continuation;
Expand Down Expand Up @@ -241,6 +289,11 @@ UniTask IAsyncClickEventHandler.OnClickAsync()
{
return OnInvokeAsync();
}

UniTask<bool> IAsyncClickEventHandler.OnClickAsyncSuppressCancellationThrow()
{
return OnInvokeAsyncSuppressCancellationThrow();
}
}

// event handler is reusable when callOnce = false.
Expand All @@ -255,6 +308,7 @@ public class AsyncUnityEventHandler<T> : IAwaiter<T>, IDisposable, IAsyncValueCh
bool isDisposed;
T eventValue;
bool callOnce;
UniTask<(bool, T)>? suppressCancellationThrowTask;

public AsyncUnityEventHandler(UnityEvent<T> unityEvent, CancellationToken cancellationToken, bool callOnce)
{
Expand Down Expand Up @@ -284,6 +338,15 @@ public UniTask<T> OnInvokeAsync()
return new UniTask<T>(this);
}

public UniTask<(bool IsCanceled, T Result)> OnInvokeAsyncSuppressCancellationThrow()
{
if (suppressCancellationThrowTask == null)
{
suppressCancellationThrowTask = OnInvokeAsync().SuppressCancellationThrow();
}
return suppressCancellationThrowTask.Value;
}

void Invoke(T value)
{
this.eventValue = value;
Expand Down Expand Up @@ -351,10 +414,20 @@ UniTask<T> IAsyncValueChangedEventHandler<T>.OnValueChangedAsync()
return OnInvokeAsync();
}

UniTask<(bool IsCanceled, T Result)> IAsyncValueChangedEventHandler<T>.OnValueChangedAsyncSuppressCancellationThrow()
{
return OnInvokeAsyncSuppressCancellationThrow();
}

UniTask<T> IAsyncEndEditEventHandler<T>.OnEndEditAsync()
{
return OnInvokeAsync();
}

UniTask<(bool IsCanceled, T Result)> IAsyncEndEditEventHandler<T>.OnEndEditAsyncSuppressCancellationThrow()
{
return OnInvokeAsyncSuppressCancellationThrow();
}
}
}

Expand Down
47 changes: 36 additions & 11 deletions Assets/Scripts/Sandbox/SandboxScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,49 @@ public class SandboxScene : MonoBehaviour

void Start()
{
//for (int i = 0; i < 3; i++)
{
DelayForever().Forget();
HandleEventA().Forget();
HandleEventB().Forget();
DoAsync().Forget();
}
//var promise = new UniTaskCompletionSource();
UniTaskScheduler.PropagateOperationCanceledException = true;

var cts = new CancellationTokenSource();
SingleClick(cts.Token).Forget();
MultiClick(cts.Token).Forget();
DestroyA(cts).Forget();
}

async UniTaskVoid DoAsync()
async UniTask SingleClick(CancellationToken ct)
{
while (true)
var click = buttonA.OnClickAsync(ct);
await click;

Debug.Log("After Single Clicked");
}

async UniTask MultiClick(CancellationToken ct)
{
try
{
await rp.WaitUntilValueChangedAsync(this.GetCancellationTokenOnDestroy());
using (var handler = buttonA.GetAsyncClickEventHandler(CancellationToken.None))
{
while (true)
{
var supress = await handler.OnClickAsyncSuppressCancellationThrow();
if (supress) return;
Debug.Log("MultilLicked");
}
}
}
finally
{
Debug.Log("Finished");
}
}

async UniTask DestroyA(CancellationTokenSource cts)
{
await buttonB.OnClickAsync();
cts.Cancel();
cts.Dispose();
Destroy(buttonA.gameObject);
}


async UniTask DelayForever()
Expand Down
2 changes: 1 addition & 1 deletion EditorTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion NewAssembly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion UniRx.Async.Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion UniRx.Async.Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
5 changes: 2 additions & 3 deletions UniRx.Async.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.20506</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<RootNamespace>
</RootNamespace>
<RootNamespace></RootNamespace>
<ProjectGuid>{089999BB-1900-31E8-673F-067C92198C6A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
Expand Down Expand Up @@ -759,7 +758,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>6365</DocumentationFile>
<DocumentationFile>62044</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
Loading

0 comments on commit d335f45

Please sign in to comment.