ObservableExtensions.cs 3.5 KB

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