Select.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.ObservableImpl
  5. {
  6. class Select<TSource, TResult> : Producer<TResult>
  7. {
  8. private readonly IObservable<TSource> _source;
  9. private readonly Func<TSource, TResult> _selector;
  10. private readonly Func<TSource, int, TResult> _selectorI;
  11. public Select(IObservable<TSource> source, Func<TSource, TResult> selector)
  12. {
  13. _source = source;
  14. _selector = selector;
  15. }
  16. public Select(IObservable<TSource> source, Func<TSource, int, TResult> selector)
  17. {
  18. _source = source;
  19. _selectorI = selector;
  20. }
  21. protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
  22. {
  23. if (_selector != null)
  24. {
  25. var sink = new _(this, observer, cancel);
  26. setSink(sink);
  27. return _source.SubscribeSafe(sink);
  28. }
  29. else
  30. {
  31. var sink = new SelectImpl(this, observer, cancel);
  32. setSink(sink);
  33. return _source.SubscribeSafe(sink);
  34. }
  35. }
  36. class _ : Sink<TResult>, IObserver<TSource>
  37. {
  38. private readonly Select<TSource, TResult> _parent;
  39. public _(Select<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  40. : base(observer, cancel)
  41. {
  42. _parent = parent;
  43. }
  44. public void OnNext(TSource value)
  45. {
  46. var result = default(TResult);
  47. try
  48. {
  49. result = _parent._selector(value);
  50. }
  51. catch (Exception exception)
  52. {
  53. base._observer.OnError(exception);
  54. base.Dispose();
  55. return;
  56. }
  57. base._observer.OnNext(result);
  58. }
  59. public void OnError(Exception error)
  60. {
  61. base._observer.OnError(error);
  62. base.Dispose();
  63. }
  64. public void OnCompleted()
  65. {
  66. base._observer.OnCompleted();
  67. base.Dispose();
  68. }
  69. }
  70. class SelectImpl : Sink<TResult>, IObserver<TSource>
  71. {
  72. private readonly Select<TSource, TResult> _parent;
  73. private int _index;
  74. public SelectImpl(Select<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  75. : base(observer, cancel)
  76. {
  77. _parent = parent;
  78. _index = 0;
  79. }
  80. public void OnNext(TSource value)
  81. {
  82. var result = default(TResult);
  83. try
  84. {
  85. result = _parent._selectorI(value, checked(_index++));
  86. }
  87. catch (Exception exception)
  88. {
  89. base._observer.OnError(exception);
  90. base.Dispose();
  91. return;
  92. }
  93. base._observer.OnNext(result);
  94. }
  95. public void OnError(Exception error)
  96. {
  97. base._observer.OnError(error);
  98. base.Dispose();
  99. }
  100. public void OnCompleted()
  101. {
  102. base._observer.OnCompleted();
  103. base.Dispose();
  104. }
  105. }
  106. }
  107. }
  108. #endif