Materialize.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. namespace System.Reactive.Linq
  5. {
  6. partial class AsyncObservable
  7. {
  8. public static IAsyncObservable<Notification<TSource>> Materialize<TSource>(this IAsyncObservable<TSource> source)
  9. {
  10. if (source == null)
  11. throw new ArgumentNullException(nameof(source));
  12. return Create<Notification<TSource>>(observer => source.SubscribeSafeAsync(AsyncObserver.Materialize(observer)));
  13. }
  14. }
  15. partial class AsyncObserver
  16. {
  17. public static IAsyncObserver<TSource> Materialize<TSource>(IAsyncObserver<Notification<TSource>> observer)
  18. {
  19. if (observer == null)
  20. throw new ArgumentNullException(nameof(observer));
  21. return Create<TSource>(
  22. x => observer.OnNextAsync(Notification.CreateOnNext(x)),
  23. async ex =>
  24. {
  25. await observer.OnNextAsync(Notification.CreateOnError<TSource>(ex)).ConfigureAwait(false);
  26. await observer.OnCompletedAsync().ConfigureAwait(false);
  27. },
  28. async () =>
  29. {
  30. await observer.OnNextAsync(Notification.CreateOnCompleted<TSource>()).ConfigureAwait(false);
  31. await observer.OnCompletedAsync().ConfigureAwait(false);
  32. }
  33. );
  34. }
  35. }
  36. }