2019-07-18 02:08:24 +00:00
|
|
|
using System;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
2020-05-09 18:45:43 +00:00
|
|
|
namespace GodotTools.IdeMessaging.Utils
|
2019-07-18 02:08:24 +00:00
|
|
|
{
|
2020-05-09 18:45:43 +00:00
|
|
|
public class NotifyAwaiter<T> : INotifyCompletion
|
2019-07-18 02:08:24 +00:00
|
|
|
{
|
|
|
|
private Action continuation;
|
|
|
|
private Exception exception;
|
|
|
|
private T result;
|
|
|
|
|
|
|
|
public bool IsCompleted { get; private set; }
|
|
|
|
|
|
|
|
public T GetResult()
|
|
|
|
{
|
|
|
|
if (exception != null)
|
|
|
|
throw exception;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnCompleted(Action continuation)
|
|
|
|
{
|
|
|
|
if (this.continuation != null)
|
2022-08-24 11:54:47 +00:00
|
|
|
throw new InvalidOperationException("This awaiter already has a continuation.");
|
2019-07-18 02:08:24 +00:00
|
|
|
this.continuation = continuation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetResult(T result)
|
|
|
|
{
|
|
|
|
if (IsCompleted)
|
2022-08-24 11:54:47 +00:00
|
|
|
throw new InvalidOperationException("This awaiter is already completed.");
|
2019-07-18 02:08:24 +00:00
|
|
|
|
|
|
|
IsCompleted = true;
|
|
|
|
this.result = result;
|
|
|
|
|
|
|
|
continuation?.Invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetException(Exception exception)
|
|
|
|
{
|
|
|
|
if (IsCompleted)
|
2022-08-24 11:54:47 +00:00
|
|
|
throw new InvalidOperationException("This awaiter is already completed.");
|
2019-07-18 02:08:24 +00:00
|
|
|
|
|
|
|
IsCompleted = true;
|
|
|
|
this.exception = exception;
|
|
|
|
|
|
|
|
continuation?.Invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
public NotifyAwaiter<T> Reset()
|
|
|
|
{
|
|
|
|
continuation = null;
|
|
|
|
exception = null;
|
2019-08-25 11:48:37 +00:00
|
|
|
result = default(T);
|
2019-07-18 02:08:24 +00:00
|
|
|
IsCompleted = false;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public NotifyAwaiter<T> GetAwaiter()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|