AsyncObserver.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Threading.Tasks;
  5. namespace System.Reactive.Linq
  6. {
  7. public static partial class AsyncObserver
  8. {
  9. public static IAsyncObserver<T> Create<T>(Func<T, Task> onNextAsync)
  10. {
  11. if (onNextAsync == null)
  12. throw new ArgumentNullException(nameof(onNextAsync));
  13. return new AsyncObserver<T>(
  14. onNextAsync,
  15. ex => Task.FromException(ex),
  16. () => Task.CompletedTask
  17. );
  18. }
  19. public static IAsyncObserver<T> Create<T>(Func<T, Task> onNextAsync, Func<Exception, Task> onErrorAsync, Func<Task> onCompletedAsync)
  20. {
  21. if (onNextAsync == null)
  22. throw new ArgumentNullException(nameof(onNextAsync));
  23. if (onErrorAsync == null)
  24. throw new ArgumentNullException(nameof(onErrorAsync));
  25. if (onCompletedAsync == null)
  26. throw new ArgumentNullException(nameof(onCompletedAsync));
  27. return new AsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);
  28. }
  29. internal static IAsyncObserver<T> CreateUnsafe<T>(Func<T, Task> onNextAsync, Func<Exception, Task> onErrorAsync, Func<Task> onCompletedAsync)
  30. {
  31. if (onNextAsync == null)
  32. throw new ArgumentNullException(nameof(onNextAsync));
  33. if (onErrorAsync == null)
  34. throw new ArgumentNullException(nameof(onErrorAsync));
  35. if (onCompletedAsync == null)
  36. throw new ArgumentNullException(nameof(onCompletedAsync));
  37. return new UnsafeAsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);
  38. }
  39. }
  40. }