2
0

ObservableExtensions.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. namespace Jint.Tests.Runtime.ExtensionMethods
  2. {
  3. internal class Subscribe<T> : IObserver<T>
  4. {
  5. private readonly Action<T> onNext;
  6. private readonly Action<Exception> onError;
  7. private readonly Action onCompleted;
  8. readonly int isStopped = 0;
  9. public Subscribe(Action<T> onNext, Action<Exception> onError, Action onCompleted)
  10. {
  11. this.onNext = onNext;
  12. this.onError = onError;
  13. this.onCompleted = onCompleted;
  14. }
  15. public void OnNext(T value)
  16. {
  17. if (isStopped == 0)
  18. {
  19. onNext(value);
  20. }
  21. }
  22. public void OnError(Exception error)
  23. {
  24. onError(error);
  25. }
  26. public void OnCompleted()
  27. {
  28. onCompleted();
  29. }
  30. }
  31. public static partial class ObservableExtensions
  32. {
  33. public static void Subscribe<T>(this IObservable<T> source, Action<T> onNext)
  34. {
  35. var subs = new Subscribe<T>(onNext, null, null);
  36. source.Subscribe(subs);
  37. }
  38. public static TResult Select<T, TResult>(this IObservable<T> source, TResult result)
  39. {
  40. return result;
  41. }
  42. public static IObservable<T> Where<T>(this IObservable<T> source, Func<T, bool> predicate)
  43. {
  44. T t = default;
  45. predicate(t);
  46. return source;
  47. }
  48. public static IObservable<T> Where<T>(this IObservable<T> source, Func<T, int, bool> predicate)
  49. {
  50. T t = default;
  51. bool result = predicate(t, 42);
  52. return source;
  53. }
  54. }
  55. public class BaseObservable<T> : IObservable<T>
  56. {
  57. private readonly List<IObserver<T>> observers = new List<IObserver<T>>();
  58. public T Last { get; private set; }
  59. public IDisposable Subscribe(IObserver<T> observer)
  60. {
  61. if (!observers.Contains(observer))
  62. observers.Add(observer);
  63. return new Unsubscriber(observers, observer);
  64. }
  65. private class Unsubscriber : IDisposable
  66. {
  67. private readonly List<IObserver<T>> _observers;
  68. private readonly IObserver<T> _observer;
  69. public Unsubscriber(List<IObserver<T>> observers, IObserver<T> observer)
  70. {
  71. this._observers = observers;
  72. this._observer = observer;
  73. }
  74. public void Dispose()
  75. {
  76. if (_observer != null && _observers.Contains(_observer))
  77. _observers.Remove(_observer);
  78. }
  79. }
  80. protected void BroadcastUpdate(T t)
  81. {
  82. foreach (var observer in observers)
  83. {
  84. observer.OnNext(t);
  85. }
  86. }
  87. public void Update(T t)
  88. {
  89. Last = t;
  90. BroadcastUpdate(t);
  91. }
  92. public void BroadcastCompleted()
  93. {
  94. foreach (var observer in observers.ToArray())
  95. {
  96. observer.OnCompleted();
  97. }
  98. observers.Clear();
  99. }
  100. }
  101. public class ObservableFactory
  102. {
  103. public static BaseObservable<bool> GetBoolBaseObservable()
  104. {
  105. return new BaseObservable<bool>();
  106. }
  107. }
  108. public class NameObservable : BaseObservable<string>
  109. {
  110. public void UpdateName(string name)
  111. {
  112. Update(name);
  113. }
  114. public void CommitName()
  115. {
  116. BroadcastCompleted();
  117. }
  118. }
  119. }