Select.cs 3.7 KB

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