wabbajack/Wabbajack.Common/CSP/TakeTaskHandler.cs

46 lines
1.1 KiB
C#
Raw Normal View History

2019-11-09 06:37:05 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Wabbajack.Common.CSP
{
public class TakeTaskHandler<T> : Handler<Action<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)
_tcs = new TaskCompletionSource<(bool, T)>();
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<T> Commit()
2019-11-09 06:37:05 +00:00
{
return Handle;
}
private void Handle(T a)
2019-11-09 06:37:05 +00:00
{
TaskCompletionSource.SetResult((true, a));
2019-11-09 06:37:05 +00:00
}
}
}