SignalAwaiter.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Godot
  4. {
  5. public class SignalAwaiter : IAwaiter<object[]>, IAwaitable<object[]>
  6. {
  7. private bool completed;
  8. private object[] result;
  9. private Action action;
  10. public SignalAwaiter(Object source, string signal, Object target)
  11. {
  12. godot_icall_SignalAwaiter_connect(Object.GetPtr(source), signal, Object.GetPtr(target), this);
  13. }
  14. [MethodImpl(MethodImplOptions.InternalCall)]
  15. internal extern static Error godot_icall_SignalAwaiter_connect(IntPtr source, string signal, IntPtr target, SignalAwaiter awaiter);
  16. public bool IsCompleted
  17. {
  18. get
  19. {
  20. return completed;
  21. }
  22. }
  23. public void OnCompleted(Action action)
  24. {
  25. this.action = action;
  26. }
  27. public object[] GetResult()
  28. {
  29. return result;
  30. }
  31. public IAwaiter<object[]> GetAwaiter()
  32. {
  33. return this;
  34. }
  35. internal void SignalCallback(object[] args)
  36. {
  37. completed = true;
  38. result = args;
  39. if (action != null)
  40. {
  41. action();
  42. }
  43. }
  44. internal void FailureCallback()
  45. {
  46. action = null;
  47. completed = true;
  48. }
  49. }
  50. }