Observable.FromAsync.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. namespace UniRx
  3. {
  4. public static partial class Observable
  5. {
  6. public static Func<IObservable<TResult>> FromAsyncPattern<TResult>(Func<AsyncCallback, object, IAsyncResult> begin, Func<IAsyncResult, TResult> end)
  7. {
  8. return () =>
  9. {
  10. var subject = new AsyncSubject<TResult>();
  11. try
  12. {
  13. begin(iar =>
  14. {
  15. TResult result;
  16. try
  17. {
  18. result = end(iar);
  19. }
  20. catch (Exception exception)
  21. {
  22. subject.OnError(exception);
  23. return;
  24. }
  25. subject.OnNext(result);
  26. subject.OnCompleted();
  27. }, null);
  28. }
  29. catch (Exception exception)
  30. {
  31. return Observable.Throw<TResult>(exception, Scheduler.DefaultSchedulers.AsyncConversions);
  32. }
  33. return subject.AsObservable();
  34. };
  35. }
  36. public static Func<T1, IObservable<TResult>> FromAsyncPattern<T1, TResult>(Func<T1, AsyncCallback, object, IAsyncResult> begin, Func<IAsyncResult, TResult> end)
  37. {
  38. return x =>
  39. {
  40. var subject = new AsyncSubject<TResult>();
  41. try
  42. {
  43. begin(x, iar =>
  44. {
  45. TResult result;
  46. try
  47. {
  48. result = end(iar);
  49. }
  50. catch (Exception exception)
  51. {
  52. subject.OnError(exception);
  53. return;
  54. }
  55. subject.OnNext(result);
  56. subject.OnCompleted();
  57. }, null);
  58. }
  59. catch (Exception exception)
  60. {
  61. return Observable.Throw<TResult>(exception, Scheduler.DefaultSchedulers.AsyncConversions);
  62. }
  63. return subject.AsObservable();
  64. };
  65. }
  66. public static Func<T1, T2, IObservable<TResult>> FromAsyncPattern<T1, T2, TResult>(Func<T1, T2, AsyncCallback, object, IAsyncResult> begin, Func<IAsyncResult, TResult> end)
  67. {
  68. return (x, y) =>
  69. {
  70. var subject = new AsyncSubject<TResult>();
  71. try
  72. {
  73. begin(x, y, iar =>
  74. {
  75. TResult result;
  76. try
  77. {
  78. result = end(iar);
  79. }
  80. catch (Exception exception)
  81. {
  82. subject.OnError(exception);
  83. return;
  84. }
  85. subject.OnNext(result);
  86. subject.OnCompleted();
  87. }, null);
  88. }
  89. catch (Exception exception)
  90. {
  91. return Observable.Throw<TResult>(exception, Scheduler.DefaultSchedulers.AsyncConversions);
  92. }
  93. return subject.AsObservable();
  94. };
  95. }
  96. public static Func<IObservable<Unit>> FromAsyncPattern(Func<AsyncCallback, object, IAsyncResult> begin, Action<IAsyncResult> end)
  97. {
  98. return FromAsyncPattern(begin, iar =>
  99. {
  100. end(iar);
  101. return Unit.Default;
  102. });
  103. }
  104. public static Func<T1, IObservable<Unit>> FromAsyncPattern<T1>(Func<T1, AsyncCallback, object, IAsyncResult> begin, Action<IAsyncResult> end)
  105. {
  106. return FromAsyncPattern(begin, iar =>
  107. {
  108. end(iar);
  109. return Unit.Default;
  110. });
  111. }
  112. public static Func<T1, T2, IObservable<Unit>> FromAsyncPattern<T1, T2>(Func<T1, T2, AsyncCallback, object, IAsyncResult> begin, Action<IAsyncResult> end)
  113. {
  114. return FromAsyncPattern(begin, iar =>
  115. {
  116. end(iar);
  117. return Unit.Default;
  118. });
  119. }
  120. }
  121. }