Throw.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #if !NO_PERF
  5. using System;
  6. using System.Reactive.Concurrency;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. class Throw<TResult> : Producer<TResult>
  10. {
  11. private readonly Exception _exception;
  12. private readonly IScheduler _scheduler;
  13. public Throw(Exception exception, IScheduler scheduler)
  14. {
  15. _exception = exception;
  16. _scheduler = scheduler;
  17. }
  18. protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
  19. {
  20. var sink = new _(this, observer, cancel);
  21. setSink(sink);
  22. return sink.Run();
  23. }
  24. class _ : Sink<TResult>
  25. {
  26. private readonly Throw<TResult> _parent;
  27. public _(Throw<TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  28. : base(observer, cancel)
  29. {
  30. _parent = parent;
  31. }
  32. public IDisposable Run()
  33. {
  34. return _parent._scheduler.Schedule(Invoke);
  35. }
  36. private void Invoke()
  37. {
  38. base._observer.OnError(_parent._exception);
  39. base.Dispose();
  40. }
  41. }
  42. }
  43. }
  44. #endif