AsObservable.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System;
  4. namespace System.Reactive.Linq.Observαble
  5. {
  6. class AsObservable<TSource> : Producer<TSource>, IEvaluatableObservable<TSource>
  7. {
  8. private readonly IObservable<TSource> _source;
  9. public AsObservable(IObservable<TSource> source)
  10. {
  11. _source = source;
  12. }
  13. public IObservable<TSource> Ω()
  14. {
  15. return this;
  16. }
  17. public IObservable<TSource> Eval()
  18. {
  19. return _source;
  20. }
  21. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  22. {
  23. var sink = new _(observer, cancel);
  24. setSink(sink);
  25. return _source.SubscribeSafe(sink);
  26. }
  27. class _ : Sink<TSource>, IObserver<TSource>
  28. {
  29. public _(IObserver<TSource> observer, IDisposable cancel)
  30. : base(observer, cancel)
  31. {
  32. }
  33. public void OnNext(TSource value)
  34. {
  35. base._observer.OnNext(value);
  36. }
  37. public void OnError(Exception error)
  38. {
  39. base._observer.OnError(error);
  40. base.Dispose();
  41. }
  42. public void OnCompleted()
  43. {
  44. base._observer.OnCompleted();
  45. base.Dispose();
  46. }
  47. }
  48. }
  49. }
  50. #endif