IConnectableObservable.Connect 关于多次通话的合同

Contract for IConnectableObservable.Connect with regards to multiple calls

当 IConnectableObservable.Connect 被多次调用时,合同应该是什么?

处理返回的 IDisposable 时,应该发布 OnCompleted 还是应该断开连接并允许第二次调用 Connect?

如果在处理第一次之前第二次调用 Connect,它应该:

我正在尝试实现 IConnectableObservable,实现它的人的文档非常简单。

如果你看看 source

你第一个问题的答案(写作时):

When the IDisposable returned is disposed of should OnCompleted be published or should it just disconnect and allow Connect to be called a second time?

只是断开连接

If Connect is called a second time before the first is disposed of

它应该:Return 相同的 IDisposable,可能有来自不同代码区域的意外处置

为了后代,有趣的代码部分是:

        public void Dispose()
        {
            lock (_parent._gate)
            {
                if (_subscription != null)
                {
                    _subscription.Dispose();
                    _subscription = null;

                    _parent._connection = null;
                }
            }
        }

    public IDisposable Connect()
    {
        lock (_gate)
        {
            if (_connection == null)
            {
                var subscription = _source.SubscribeSafe(_subject);
                _connection = new Connection(this, subscription);
            }

            return _connection;
        }
    }

正如您在上面看到的那样,有一个连接被释放并连接到一个锁块中以防止并发修改。