CombineLatest 是否保留了可观察量的顺序?

Does CombineLatest conserve the order of the observables?

我对以下重载感兴趣:

public static IObservable<IList<TSource>> CombineLatest<TSource>(this params IObservable<TSource>[] sources);
public static IObservable<IList<TSource>> CombineLatest<TSource>(this IEnumerable<IObservable<TSource>> sources);

结果列表中的元素顺序是否保证与输入中的顺序相同?

例如,在下面的代码中,list[0] 将始终包含来自 a 的元素,而 list[1] 将始终包含来自 b?

的元素
IObservable<int> a = ...;
IObservable<int> b = ...;
var list = await Observable.CombineLatest(a, b).FirstAsync();

文档指出:

a list with the latest source elements

和:

observable sequence containing lists of the latest elements of the sources

但并没有真正提到任何关于订单的事情。

CombineLatest 当有一个元素被推送到所有流时首先触发。

之后,每当将新元素推送到任何流时都会触发它。

所以如果你 "combine" 两个流,结果列表中总是有两个元素,据我所知,保证顺序与你在 CombineLatest参数。

您可以使用弹珠图可视化 Rx 运算符。 HERECombineLatest.

顺序守恒。

当您查看 source code of RX 时,一切都归结为 System.Reactive.Linq.CombineLatest<TSource, TResult> class。

您可以在那里发现为每个输入可观察对象创建了一个索引观察器(其中索引是输入中的顺序):

for (int i = 0; i < N; i++)
{
    var j = i;

    var d = new SingleAssignmentDisposable();
    _subscriptions[j] = d;

    var o = new O(this, j);
    d.Disposable = srcs[j].SubscribeSafe(o);
}

生成的元素如下:

private void OnNext(int index, TSource value)
{
    lock (_gate)
    {
        _values[index] = value;
        _hasValue[index] = true;

        if (_hasValueAll || (_hasValueAll = _hasValue.All(Stubs<bool>.I)))
        {
                /* snip */
                res = _parent._resultSelector(new ReadOnlyCollection<TSource>(_values));
                /* snip */

            _observer.OnNext(res);
        }
        /* snip */
    }
}

我感兴趣的重载 _resultSelector 只是一个 Enumerable.ToList()。所以输出列表中的顺序将与输入中的顺序相同。