wabbajack/Wabbajack.Common.CSP/TakeTaskHandler.cs

47 lines
1.2 KiB
C#
Raw Normal View History

2019-11-09 06:37:05 +00:00
using System;
using System.Threading;
2019-11-09 06:37:05 +00:00
using System.Threading.Tasks;
namespace Wabbajack.Common.CSP
{
public class TakeTaskHandler<T> : Handler<Action<bool, T>>
2019-11-09 06:37:05 +00:00
{
private readonly bool _blockable;
private TaskCompletionSource<(bool, T)> _tcs;
2019-11-09 06:37:05 +00:00
public TakeTaskHandler(TaskCompletionSource<T> tcs = null, bool blockable = true)
{
_blockable = blockable;
}
public TaskCompletionSource<(bool, T)> TaskCompletionSource
{
get
{
if (_tcs == null)
{
var new_tcs = new TaskCompletionSource<(bool, T)>();
Interlocked.CompareExchange(ref _tcs, new_tcs, null);
}
return _tcs;
}
2019-11-09 06:37:05 +00:00
}
public bool IsActive => true;
public bool IsBlockable => _blockable;
public uint LockId => 0;
public Task<(bool, T)> Task => TaskCompletionSource.Task;
public Action<bool, T> Commit()
2019-11-09 06:37:05 +00:00
{
return Handle;
}
private void Handle(bool is_open, T a)
2019-11-09 06:37:05 +00:00
{
TaskCompletionSource.SetResult((is_open, a));
2019-11-09 06:37:05 +00:00
}
}
}