2019-07-18 04:08:24 +02:00
|
|
|
using System;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
2020-05-09 20:45:43 +02:00
|
|
|
namespace GodotTools.IdeMessaging.Utils
|
2019-07-18 04:08:24 +02:00
|
|
|
{
|
2020-05-09 20:45:43 +02:00
|
|
|
public class NotifyAwaiter<T> : INotifyCompletion
|
2019-07-18 04:08:24 +02: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)
|
|
|
|
throw new InvalidOperationException("This awaiter has already been listened");
|
|
|
|
this.continuation = continuation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetResult(T result)
|
|
|
|
{
|
|
|
|
if (IsCompleted)
|
|
|
|
throw new InvalidOperationException("This awaiter is already completed");
|
|
|
|
|
|
|
|
IsCompleted = true;
|
|
|
|
this.result = result;
|
|
|
|
|
|
|
|
continuation?.Invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetException(Exception exception)
|
|
|
|
{
|
|
|
|
if (IsCompleted)
|
|
|
|
throw new InvalidOperationException("This awaiter is already completed");
|
|
|
|
|
|
|
|
IsCompleted = true;
|
|
|
|
this.exception = exception;
|
|
|
|
|
|
|
|
continuation?.Invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
public NotifyAwaiter<T> Reset()
|
|
|
|
{
|
|
|
|
continuation = null;
|
|
|
|
exception = null;
|
2019-08-25 13:48:37 +02:00
|
|
|
result = default(T);
|
2019-07-18 04:08:24 +02:00
|
|
|
IsCompleted = false;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public NotifyAwaiter<T> GetAwaiter()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|