PushPullAdapter.cs 1.9 KB

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