This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add awaiter for UnityEvent and generic version
- Loading branch information
uurha
committed
Aug 31, 2022
1 parent
f1a87d3
commit 1a6351b
Showing
9 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using UnityEngine.Events; | ||
|
||
namespace BetterExtensions.Runtime.Extension.ActionExtensions | ||
{ | ||
public static class ActionAwaiter | ||
{ | ||
public static Task Await(this UnityEvent action, CancellationToken cancellationToken = default) | ||
{ | ||
var taskCompletionSource = new TaskCompletionSource<bool>(); | ||
var buffer = new UnityEventWrapper(taskCompletionSource, ref action); | ||
if (!cancellationToken.Equals(default)) | ||
{ | ||
cancellationToken.Register(() => buffer.Cancel()); | ||
} | ||
|
||
return buffer.Initialize(); | ||
} | ||
|
||
public static Task Await<T>(this UnityEvent<T> action, CancellationToken cancellationToken = default) | ||
{ | ||
var taskCompletionSource = new TaskCompletionSource<T>(); | ||
var buffer = new UnityEventWrapper<T>(taskCompletionSource, ref action); | ||
if (!cancellationToken.Equals(default)) | ||
{ | ||
cancellationToken.Register(() => buffer.Cancel()); | ||
} | ||
|
||
return buffer.Initialize(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace BetterExtensions.Runtime.Extension.ActionExtensions | ||
{ | ||
internal abstract class BaseActionWrapper<T> | ||
{ | ||
private protected TaskCompletionSource<T> _taskCompletionSource; | ||
|
||
public BaseActionWrapper(TaskCompletionSource<T> taskCompletionSource) | ||
{ | ||
_taskCompletionSource = taskCompletionSource; | ||
} | ||
|
||
public abstract Task<T> Initialize(); | ||
|
||
|
||
public abstract void Cancel(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Threading.Tasks; | ||
using UnityEngine.Events; | ||
|
||
namespace BetterExtensions.Runtime.Extension.ActionExtensions | ||
{ | ||
internal class UnityEventWrapper<T> : BaseActionWrapper<T> | ||
{ | ||
private UnityEvent<T> _action; | ||
|
||
public UnityEventWrapper(TaskCompletionSource<T> taskCompletionSource, ref UnityEvent<T> action) : base( | ||
taskCompletionSource) | ||
{ | ||
_action = action; | ||
} | ||
|
||
public override Task<T> Initialize() | ||
{ | ||
_action.AddListener(SetResult); | ||
return _taskCompletionSource.Task; | ||
} | ||
|
||
public override void Cancel() | ||
{ | ||
_action.RemoveListener(SetResult); | ||
_taskCompletionSource.TrySetCanceled(); | ||
} | ||
|
||
private void SetResult(T result) | ||
{ | ||
_action.RemoveListener(SetResult); | ||
_taskCompletionSource.TrySetResult(result); | ||
} | ||
} | ||
|
||
internal class UnityEventWrapper : BaseActionWrapper<bool> | ||
{ | ||
private UnityEvent _action; | ||
|
||
public UnityEventWrapper(TaskCompletionSource<bool> taskCompletionSource, ref UnityEvent action) : base( | ||
taskCompletionSource) | ||
{ | ||
_action = action; | ||
} | ||
|
||
public override Task<bool> Initialize() | ||
{ | ||
_action.AddListener(SetResult); | ||
return _taskCompletionSource.Task; | ||
} | ||
|
||
private void SetResult() | ||
{ | ||
_action.RemoveListener(SetResult); | ||
_taskCompletionSource.TrySetResult(true); | ||
} | ||
|
||
public override void Cancel() | ||
{ | ||
_action.RemoveListener(SetResult); | ||
_taskCompletionSource.TrySetCanceled(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Git LFS file not shown