Throw.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.Concurrency;
  5. using System.Threading.Tasks;
  6. namespace System.Reactive.Linq
  7. {
  8. partial class AsyncObservable
  9. {
  10. public static IAsyncObservable<TSource> Throw<TSource>(Exception error)
  11. {
  12. if (error == null)
  13. throw new ArgumentNullException(nameof(error));
  14. return Create<TSource>(observer => AsyncObserver.Throw(observer, error));
  15. }
  16. public static IAsyncObservable<TSource> Throw<TSource>(Exception error, IAsyncScheduler scheduler)
  17. {
  18. if (error == null)
  19. throw new ArgumentNullException(nameof(error));
  20. if (scheduler == null)
  21. throw new ArgumentNullException(nameof(scheduler));
  22. return Create<TSource>(observer => AsyncObserver.Throw(observer, error, scheduler));
  23. }
  24. }
  25. partial class AsyncObserver
  26. {
  27. public static Task<IAsyncDisposable> Throw<TSource>(IAsyncObserver<TSource> observer, Exception error) => Throw(observer, error, ImmediateAsyncScheduler.Instance);
  28. public static Task<IAsyncDisposable> Throw<TSource>(IAsyncObserver<TSource> observer, Exception error, IAsyncScheduler scheduler)
  29. {
  30. if (observer == null)
  31. throw new ArgumentNullException(nameof(observer));
  32. if (scheduler == null)
  33. throw new ArgumentNullException(nameof(scheduler));
  34. return scheduler.ScheduleAsync(async ct =>
  35. {
  36. ct.ThrowIfCancellationRequested();
  37. await observer.OnErrorAsync(error).RendezVous(scheduler, ct);
  38. });
  39. }
  40. }
  41. }