PushToPullAdapter.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. #nullable disable
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Reactive.Disposables;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. internal abstract class PushToPullAdapter<TSource, TResult> : IEnumerable<TResult>
  11. {
  12. private readonly IObservable<TSource> _source;
  13. protected PushToPullAdapter(IObservable<TSource> source)
  14. {
  15. _source = source;
  16. }
  17. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  18. public IEnumerator<TResult> GetEnumerator()
  19. {
  20. var res = Run();
  21. res.SetUpstream(_source.SubscribeSafe(res));
  22. return res;
  23. }
  24. protected abstract PushToPullSink<TSource, TResult> Run();
  25. }
  26. internal abstract class PushToPullSink<TSource, TResult> : IObserver<TSource>, IEnumerator<TResult>
  27. {
  28. private IDisposable _upstream;
  29. public abstract void OnNext(TSource value);
  30. public abstract void OnError(Exception error);
  31. public abstract void OnCompleted();
  32. public abstract bool TryMoveNext(out TResult current);
  33. private bool _done;
  34. public bool MoveNext()
  35. {
  36. if (!_done)
  37. {
  38. if (TryMoveNext(out var current))
  39. {
  40. Current = current;
  41. return true;
  42. }
  43. _done = true;
  44. Dispose();
  45. }
  46. return false;
  47. }
  48. public TResult Current
  49. {
  50. get;
  51. private set;
  52. }
  53. object IEnumerator.Current => Current;
  54. public void Reset()
  55. {
  56. throw new NotSupportedException();
  57. }
  58. public void Dispose()
  59. {
  60. Disposable.Dispose(ref _upstream);
  61. }
  62. public void SetUpstream(IDisposable d)
  63. {
  64. Disposable.SetSingle(ref _upstream, d);
  65. }
  66. }
  67. }