GroupedObservable.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.Reactive.Disposables;
  5. using System.Reactive.Subjects;
  6. namespace System.Reactive.Linq
  7. {
  8. internal sealed class GroupedObservable<TKey, TElement> : ObservableBase<TElement>, IGroupedObservable<TKey, TElement>
  9. {
  10. private readonly IObservable<TElement> _subject;
  11. private readonly RefCountDisposable _refCount;
  12. public GroupedObservable(TKey key, ISubject<TElement> subject, RefCountDisposable refCount)
  13. {
  14. Key = key;
  15. _subject = subject;
  16. _refCount = refCount;
  17. }
  18. public GroupedObservable(TKey key, ISubject<TElement> subject)
  19. {
  20. Key = key;
  21. _subject = subject;
  22. }
  23. public TKey Key { get; }
  24. protected override IDisposable SubscribeCore(IObserver<TElement> observer)
  25. {
  26. if (_refCount != null)
  27. {
  28. //
  29. // [OK] Use of unsafe Subscribe: called on a known subject implementation.
  30. //
  31. var release = _refCount.GetDisposable();
  32. var subscription = _subject.Subscribe/*Unsafe*/(observer);
  33. return StableCompositeDisposable.Create(release, subscription);
  34. }
  35. else
  36. {
  37. //
  38. // [OK] Use of unsafe Subscribe: called on a known subject implementation.
  39. //
  40. return _subject.Subscribe/*Unsafe*/(observer);
  41. }
  42. }
  43. }
  44. }