MySubject.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Reactive.Disposables;
  7. using System.Reactive.Subjects;
  8. namespace ReactiveTests.Tests
  9. {
  10. class MySubject : ISubject<int>
  11. {
  12. private Dictionary<int, IDisposable> _disposeOn = new Dictionary<int, IDisposable>();
  13. public void DisposeOn(int value, IDisposable disposable)
  14. {
  15. _disposeOn[value] = disposable;
  16. }
  17. private IObserver<int> _observer;
  18. public void OnNext(int value)
  19. {
  20. _observer.OnNext(value);
  21. if (_disposeOn.TryGetValue(value, out var disconnect))
  22. disconnect.Dispose();
  23. }
  24. public void OnError(Exception exception)
  25. {
  26. _observer.OnError(exception);
  27. }
  28. public void OnCompleted()
  29. {
  30. _observer.OnCompleted();
  31. }
  32. public IDisposable Subscribe(IObserver<int> observer)
  33. {
  34. _subscribeCount++;
  35. _observer = observer;
  36. return Disposable.Create(() => { _disposed = true; });
  37. }
  38. private int _subscribeCount;
  39. private bool _disposed;
  40. public int SubscribeCount { get { return _subscribeCount; } }
  41. public bool Disposed { get { return _disposed; } }
  42. }
  43. }