PushPullAdapter.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #if NO_PERF
  5. using System.Collections.Generic;
  6. namespace System.Reactive
  7. {
  8. sealed class PushPullAdapter<T, R> : IObserver<T>, IEnumerator<R>
  9. {
  10. Action<Notification<T>> yield;
  11. Action dispose;
  12. Func<Notification<R>> moveNext;
  13. Notification<R> current;
  14. bool done = false;
  15. bool disposed;
  16. public PushPullAdapter(Action<Notification<T>> yield, Func<Notification<R>> moveNext, Action dispose)
  17. {
  18. this.yield = yield;
  19. this.moveNext = moveNext;
  20. this.dispose = dispose;
  21. }
  22. public void OnNext(T value)
  23. {
  24. yield(Notification.CreateOnNext<T>(value));
  25. }
  26. public void OnError(Exception exception)
  27. {
  28. yield(Notification.CreateOnError<T>(exception));
  29. dispose();
  30. }
  31. public void OnCompleted()
  32. {
  33. yield(Notification.CreateOnCompleted<T>());
  34. dispose();
  35. }
  36. public R Current
  37. {
  38. get { return current.Value; }
  39. }
  40. public void Dispose()
  41. {
  42. disposed = true;
  43. dispose();
  44. }
  45. object System.Collections.IEnumerator.Current
  46. {
  47. get { return this.Current; }
  48. }
  49. public bool MoveNext()
  50. {
  51. if (disposed)
  52. throw new ObjectDisposedException("");
  53. if (!done)
  54. {
  55. current = moveNext();
  56. done = current.Kind != NotificationKind.OnNext;
  57. }
  58. current.Exception.ThrowIfNotNull();
  59. return current.HasValue;
  60. }
  61. public void Reset()
  62. {
  63. throw new NotSupportedException();
  64. }
  65. }
  66. }
  67. #endif