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