| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
using System; |
| 5 |
using System.Runtime.CompilerServices; |
| 6 |
|
| 7 |
namespace GodotTools.IdeMessaging.Utils |
| 8 |
{ |
| 9 |
public class NotifyAwaiter<T> : INotifyCompletion |
| 10 |
{ |
| 11 |
private Action? continuation; |
| 12 |
private Exception? exception; |
| 13 |
private T? result; |
| 14 |
|
| 15 |
public bool IsCompleted { get; private set; } |
| 16 |
|
| 17 |
public T GetResult() |
| 18 |
{ |
| 19 |
if (exception != null) |
| 20 |
throw exception; |
| 21 |
return result!; |
| 22 |
} |
| 23 |
|
| 24 |
public void OnCompleted(Action continuation) |
| 25 |
{ |
| 26 |
if (this.continuation != null) |
| 27 |
throw new InvalidOperationException("This awaiter already has a continuation."); |
| 28 |
this.continuation = continuation; |
| 29 |
} |
| 30 |
|
| 31 |
public void SetResult(T result) |
| 32 |
{ |
| 33 |
if (IsCompleted) |
| 34 |
throw new InvalidOperationException("This awaiter is already completed."); |
| 35 |
|
| 36 |
IsCompleted = true; |
| 37 |
this.result = result; |
| 38 |
|
| 39 |
continuation?.Invoke(); |
| 40 |
} |
| 41 |
|
| 42 |
public void SetException(Exception exception) |
| 43 |
{ |
| 44 |
if (IsCompleted) |
| 45 |
throw new InvalidOperationException("This awaiter is already completed."); |
| 46 |
|
| 47 |
IsCompleted = true; |
| 48 |
this.exception = exception; |
| 49 |
|
| 50 |
continuation?.Invoke(); |
| 51 |
} |
| 52 |
|
| 53 |
public NotifyAwaiter<T> Reset() |
| 54 |
{ |
| 55 |
continuation = null; |
| 56 |
exception = null; |
| 57 |
result = default(T); |
| 58 |
IsCompleted = false; |
| 59 |
return this; |
| 60 |
} |
| 61 |
|
| 62 |
public NotifyAwaiter<T> GetAwaiter() |
| 63 |
{ |
| 64 |
return this; |
| 65 |
} |
| 66 |
} |
| 67 |
} |