MySubject.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. internal class MySubject : ISubject<int>
  11. {
  12. private readonly Dictionary<int, IDisposable> _disposeOn = [];
  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. {
  23. disconnect.Dispose();
  24. }
  25. }
  26. public void OnError(Exception exception)
  27. {
  28. _observer.OnError(exception);
  29. }
  30. public void OnCompleted()
  31. {
  32. _observer.OnCompleted();
  33. }
  34. public IDisposable Subscribe(IObserver<int> observer)
  35. {
  36. _subscribeCount++;
  37. _observer = observer;
  38. return Disposable.Create(() => { _disposed = true; });
  39. }
  40. private int _subscribeCount;
  41. private bool _disposed;
  42. public int SubscribeCount { get { return _subscribeCount; } }
  43. public bool Disposed { get { return _disposed; } }
  44. }
  45. }