AsObservable.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. class AsObservable<TSource> : Producer<TSource>, IEvaluatableObservable<TSource>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. public AsObservable(IObservable<TSource> source)
  12. {
  13. _source = source;
  14. }
  15. public IObservable<TSource> Omega()
  16. {
  17. return this;
  18. }
  19. public IObservable<TSource> Eval()
  20. {
  21. return _source;
  22. }
  23. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  24. {
  25. var sink = new _(observer, cancel);
  26. setSink(sink);
  27. return _source.SubscribeSafe(sink);
  28. }
  29. class _ : Sink<TSource>, IObserver<TSource>
  30. {
  31. public _(IObserver<TSource> observer, IDisposable cancel)
  32. : base(observer, cancel)
  33. {
  34. }
  35. public void OnNext(TSource value)
  36. {
  37. base._observer.OnNext(value);
  38. }
  39. public void OnError(Exception error)
  40. {
  41. base._observer.OnError(error);
  42. base.Dispose();
  43. }
  44. public void OnCompleted()
  45. {
  46. base._observer.OnCompleted();
  47. base.Dispose();
  48. }
  49. }
  50. }
  51. }
  52. #endif