GetAwaiter.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Subjects;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObservable
  8. {
  9. public static AsyncAsyncSubject<TSource> GetAwaiter<TSource>(this IAsyncObservable<TSource> source)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. var subject = new SequentialAsyncAsyncSubject<TSource>();
  14. var subscribeTask = source.SubscribeSafeAsync(subject);
  15. subscribeTask.ContinueWith(t =>
  16. {
  17. if (t.Exception != null)
  18. {
  19. subject.OnErrorAsync(t.Exception); // NB: Should not occur due to use of SubscribeSafeAsync.
  20. }
  21. });
  22. return subject;
  23. }
  24. public static AsyncAsyncSubject<TSource> GetAwaiter<TSource>(this IConnectableAsyncObservable<TSource> source)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. var subject = new SequentialAsyncAsyncSubject<TSource>();
  29. var subscribeTask = source.SubscribeSafeAsync(subject);
  30. subscribeTask.ContinueWith(t =>
  31. {
  32. if (t.Exception != null)
  33. {
  34. subject.OnErrorAsync(t.Exception); // NB: Should not occur due to use of SubscribeSafeAsync.
  35. }
  36. else
  37. {
  38. source.ConnectAsync();
  39. }
  40. });
  41. return subject;
  42. }
  43. }
  44. }