DelayFrameSubscription.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. #if UniRxLibrary
  3. using UnityObservable = UniRx.ObservableUnity;
  4. #else
  5. using UnityObservable = UniRx.Observable;
  6. #endif
  7. namespace UniRx.Operators
  8. {
  9. internal class DelayFrameSubscriptionObservable<T> : OperatorObservableBase<T>
  10. {
  11. readonly IObservable<T> source;
  12. readonly int frameCount;
  13. readonly FrameCountType frameCountType;
  14. public DelayFrameSubscriptionObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType)
  15. : base(source.IsRequiredSubscribeOnCurrentThread())
  16. {
  17. this.source = source;
  18. this.frameCount = frameCount;
  19. this.frameCountType = frameCountType;
  20. }
  21. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  22. {
  23. var d = new MultipleAssignmentDisposable();
  24. d.Disposable = UnityObservable.TimerFrame(frameCount, frameCountType)
  25. .SubscribeWithState3(observer, d, source, (_, o, disp, s) =>
  26. {
  27. disp.Disposable = s.Subscribe(o);
  28. });
  29. return d;
  30. }
  31. }
  32. }