上海虹口龙之梦项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 lines
2.0 KiB

using System;
namespace UniRx
{
public interface IConnectableObservable<T> : IObservable<T>
{
IDisposable Connect();
}
public static partial class Observable
{
class ConnectableObservable<T> : IConnectableObservable<T>
{
readonly IObservable<T> source;
readonly ISubject<T> subject;
readonly object gate = new object();
Connection connection;
public ConnectableObservable(IObservable<T> source, ISubject<T> subject)
{
this.source = source.AsObservable();
this.subject = subject;
}
public IDisposable Connect()
{
lock (gate)
{
// don't subscribe twice
if (connection == null)
{
var subscription = source.Subscribe(subject);
connection = new Connection(this, subscription);
}
return connection;
}
}
public IDisposable Subscribe(IObserver<T> observer)
{
return subject.Subscribe(observer);
}
class Connection : IDisposable
{
readonly ConnectableObservable<T> parent;
IDisposable subscription;
public Connection(ConnectableObservable<T> parent, IDisposable subscription)
{
this.parent = parent;
this.subscription = subscription;
}
public void Dispose()
{
lock (parent.gate)
{
if (subscription != null)
{
subscription.Dispose();
subscription = null;
parent.connection = null;
}
}
}
}
}
}
}