mirror of
https://github.com/2dust/v2rayN.git
synced 2025-10-15 21:09:13 +00:00
29 lines
671 B
C#
29 lines
671 B
C#
using System.Reactive;
|
|
using System.Reactive.Linq;
|
|
using System.Reactive.Subjects;
|
|
|
|
namespace ServiceLib.Events;
|
|
|
|
public sealed class EventChannel<T>
|
|
{
|
|
private readonly ISubject<T> _subject = Subject.Synchronize(new Subject<T>());
|
|
|
|
public IObservable<T> AsObservable()
|
|
{
|
|
return _subject.AsObservable();
|
|
}
|
|
|
|
public void Publish(T value)
|
|
{
|
|
_subject.OnNext(value);
|
|
}
|
|
|
|
public void Publish()
|
|
{
|
|
if (typeof(T) != typeof(Unit))
|
|
{
|
|
throw new InvalidOperationException("Publish() without value is only valid for EventChannel<Unit>.");
|
|
}
|
|
_subject.OnNext((T)(object)Unit.Default);
|
|
}
|
|
}
|