AsyncObserver.cs 1.3 KB

12345678910111213141516171819202122232425262728
  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 AsyncObserver<T> : AsyncObserverBase<T>
  8. {
  9. private readonly Func<T, ValueTask> _onNextAsync;
  10. private readonly Func<Exception, ValueTask> _onErrorAsync;
  11. private readonly Func<ValueTask> _onCompletedAsync;
  12. public AsyncObserver(Func<T, ValueTask> onNextAsync, Func<Exception, ValueTask> onErrorAsync, Func<ValueTask> onCompletedAsync)
  13. {
  14. _onNextAsync = onNextAsync ?? throw new ArgumentNullException(nameof(onNextAsync));
  15. _onErrorAsync = onErrorAsync ?? throw new ArgumentNullException(nameof(onErrorAsync));
  16. _onCompletedAsync = onCompletedAsync ?? throw new ArgumentNullException(nameof(onCompletedAsync));
  17. }
  18. protected override ValueTask OnCompletedAsyncCore() => _onCompletedAsync();
  19. protected override ValueTask OnErrorAsyncCore(Exception error) => _onErrorAsync(error ?? throw new ArgumentNullException(nameof(error)));
  20. protected override ValueTask OnNextAsyncCore(T value) => _onNextAsync(value);
  21. }
  22. }