Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Add awaiter for UnityEvent and generic version
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Aug 31, 2022
1 parent f1a87d3 commit 1a6351b
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 4 deletions.
2 changes: 1 addition & 1 deletion BetterExtensions.asmdef
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "BetterExtensions",
"rootNamespace": "BetterExtensions",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Extension/ActionExtensions.meta

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

34 changes: 34 additions & 0 deletions Runtime/Extension/ActionExtensions/ActionAwaiter.cs
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();
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Extension/ActionExtensions/ActionAwaiter.cs.meta

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

19 changes: 19 additions & 0 deletions Runtime/Extension/ActionExtensions/BaseActionWrapper.cs
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();
}
}
3 changes: 3 additions & 0 deletions Runtime/Extension/ActionExtensions/BaseActionWrapper.cs.meta

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

63 changes: 63 additions & 0 deletions Runtime/Extension/ActionExtensions/UnityEventWrapper.cs
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();
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Extension/ActionExtensions/UnityEventWrapper.cs.meta

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

6 changes: 3 additions & 3 deletions Samples~/TestSamples/Applause_short1.wav
Git LFS file not shown

0 comments on commit 1a6351b

Please sign in to comment.