OperatorObserverBase.cs 909 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Threading;
  3. namespace UniRx.Operators
  4. {
  5. public abstract class OperatorObserverBase<TSource, TResult> : IDisposable, IObserver<TSource>
  6. {
  7. protected internal volatile IObserver<TResult> observer;
  8. IDisposable cancel;
  9. public OperatorObserverBase(IObserver<TResult> observer, IDisposable cancel)
  10. {
  11. this.observer = observer;
  12. this.cancel = cancel;
  13. }
  14. public abstract void OnNext(TSource value);
  15. public abstract void OnError(Exception error);
  16. public abstract void OnCompleted();
  17. public void Dispose()
  18. {
  19. observer = UniRx.InternalUtil.EmptyObserver<TResult>.Instance;
  20. var target = System.Threading.Interlocked.Exchange(ref cancel, null);
  21. if (target != null)
  22. {
  23. target.Dispose();
  24. }
  25. }
  26. }
  27. }