virtualx-engine/modules/mono/editor/GodotTools/GodotTools.IdeMessaging/Utils/NotifyAwaiter.cs
Raul Santos 79f9f59a87
Fix various C# exceptions
- Replace `IndexOutOfRangeException` with `ArgumentOutOfRangeException`
- Replace `Exception` with a more specific exception
- Add the parameter name to argument exception
- Update documentation for methods that throw exceptions
- Use `StringBuilder` to build exception messages
- Ensure exception messages end with a period
2022-08-26 16:56:00 +02:00

64 lines
1.6 KiB
C#

using System;
using System.Runtime.CompilerServices;
namespace GodotTools.IdeMessaging.Utils
{
public class NotifyAwaiter<T> : INotifyCompletion
{
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 already has a continuation.");
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;
result = default(T);
IsCompleted = false;
return this;
}
public NotifyAwaiter<T> GetAwaiter()
{
return this;
}
}
}