AsyncObservableExtensions.cs 1.1 KB

1234567891011121314151617181920212223242526
  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;
  5. using System.Threading.Tasks;
  6. namespace System
  7. {
  8. public static class AsyncObservableExtensions
  9. {
  10. public static Task<IAsyncDisposable> SubscribeAsync<T>(this IAsyncObservable<T> source, Func<T, Task> onNextAsync, Func<Exception, Task> onErrorAsync, Func<Task> onCompletedAsync)
  11. {
  12. if (source == null)
  13. throw new ArgumentNullException(nameof(source));
  14. if (onNextAsync == null)
  15. throw new ArgumentNullException(nameof(onNextAsync));
  16. if (onErrorAsync == null)
  17. throw new ArgumentNullException(nameof(onErrorAsync));
  18. if (onCompletedAsync == null)
  19. throw new ArgumentNullException(nameof(onCompletedAsync));
  20. return source.SubscribeAsync(new AsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync));
  21. }
  22. }
  23. }