Repository navigation
Expand file tree
/
Copy pathSubjectWithRefCount.cs
More file actions
77 lines (69 loc) · 2.37 KB
/
Copy pathSubjectWithRefCount.cs
File metadata and controls
77 lines (69 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Reactive.Disposables;
using System.Reactive.Subjects;
using System.Threading;
namespace DynamicData.Experimental
{
/// <summary>
/// A subject with a count of the number of subscribers
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SubjectWithRefCount<T> : ISubjectWithRefCount<T>
{
private readonly ISubject<T> _subject;
private int _refCount = 0;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Object"/> class.
/// </summary>
public SubjectWithRefCount(ISubject<T> subject = null)
{
_subject = subject ?? new Subject<T>();
}
/// <summary>
/// Provides the observer with new data.
/// </summary>
/// <param name="value">The current notification information.</param>
public void OnNext(T value)
{
_subject.OnNext(value);
}
/// <summary>
/// Notifies the observer that the provider has experienced an error condition.
/// </summary>
/// <param name="error">An object that provides additional information about the error.</param>
public void OnError(Exception error)
{
_subject.OnError(error);
}
/// <summary>
/// Notifies the observer that the provider has finished sending push-based notifications.
/// </summary>
public void OnCompleted()
{
_subject.OnCompleted();
}
/// <summary>
/// Notifies the provider that an observer is to receive notifications.
/// </summary>
/// <returns>
/// The observer's interface that enables resources to be disposed.
/// </returns>
/// <param name="observer">The object that is to receive notifications.</param>
public IDisposable Subscribe(IObserver<T> observer)
{
Interlocked.Increment(ref _refCount);
var subscriber = _subject.Subscribe(observer);
return Disposable.Create(() =>
{
subscriber.Dispose();
Interlocked.Decrement(ref _refCount);
});
}
/// <summary>number of subscribers
/// </summary>
/// <value>
/// The ref count.
/// </value>
public int RefCount => _refCount;
}
}