SubscribeOnMainThread.cs 966 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. namespace UniRx.Operators
  3. {
  4. internal class SubscribeOnMainThreadObservable<T> : OperatorObservableBase<T>
  5. {
  6. readonly IObservable<T> source;
  7. readonly IObservable<long> subscribeTrigger;
  8. public SubscribeOnMainThreadObservable(IObservable<T> source, IObservable<long> subscribeTrigger)
  9. : base(source.IsRequiredSubscribeOnCurrentThread())
  10. {
  11. this.source = source;
  12. this.subscribeTrigger = subscribeTrigger;
  13. }
  14. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  15. {
  16. var m = new SingleAssignmentDisposable();
  17. var d = new SerialDisposable();
  18. d.Disposable = m;
  19. m.Disposable = subscribeTrigger.SubscribeWithState3(observer, d, source, (_, o, disp, s) =>
  20. {
  21. disp.Disposable = s.Subscribe(o);
  22. });
  23. return d;
  24. }
  25. }
  26. }