UnsafeAsyncObserver.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  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
  6. {
  7. public class UnsafeAsyncObserver<T> : IAsyncObserver<T>
  8. {
  9. private readonly Func<T, Task> _onNextAsync;
  10. private readonly Func<Exception, Task> _onErrorAsync;
  11. private readonly Func<Task> _onCompletedAsync;
  12. public UnsafeAsyncObserver(Func<T, Task> onNextAsync, Func<Exception, Task> onErrorAsync, Func<Task> onCompletedAsync)
  13. {
  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. _onNextAsync = onNextAsync;
  21. _onErrorAsync = onErrorAsync;
  22. _onCompletedAsync = onCompletedAsync;
  23. }
  24. public Task OnCompletedAsync() => _onCompletedAsync();
  25. public Task OnErrorAsync(Exception error) => _onErrorAsync(error ?? throw new ArgumentNullException(nameof(error)));
  26. public Task OnNextAsync(T value) => _onNextAsync(value);
  27. }
  28. }