4

我的目标可能最容易用大理石图来解释。我有两个 Observable,xs 和 ys。我想返回一个名为 rs 的 Observable。

xs --x---x---x---x-------x---x---x-
      \           \       \
ys ----y-----------y---y---y-------
       |           |       |
rs ----x-----------x-------x-------
       y           y       y

所以,我需要类似于 CombineLatest 的东西,除了它应该只在 xs 后跟 ys 时触发。该模式之外的其他 xs 或 ys 不应触发输出,应丢弃。

CombineLatest、Zip 或 And/Then/When 不做我需要的事情,我找不到任何方法来指定更复杂的连接结构。

4

3 回答 3

3

对于此类问题,提供测试场景非常有用。你已经给出了你想要的一个很棒的小大理石图,然后将它转换为测试用例真的很容易。然后其他论坛读者可以获取测试代码,实施他们的想法并知道他们是否满足您的要求。

[Test]
public void Should_only_get_latest_value_from_Y_but_not_while_x_produes()
{
    //            11111111112222222222333
    //   12345678901234567890123456789012
    //xs --x---x---x---x-------x---x---x-
    //      \           \       \
    //ys ----y-----------y---y---y-------
    //       |           |       |
    //rs ----x-----------x-------x-------
    //       y           y       y


    var testScheduler = new TestScheduler();
    //            11111111112222222222333
    //   12345678901234567890123456789012
    //xs --x---x---x---x-------x---x---x-
    var xs = testScheduler.CreateColdObservable(
        new Recorded<Notification<char>>(3, Notification.CreateOnNext('1')),
        new Recorded<Notification<char>>(7, Notification.CreateOnNext('2')),
        new Recorded<Notification<char>>(10, Notification.CreateOnNext('3')),
        new Recorded<Notification<char>>(15, Notification.CreateOnNext('4')),
        new Recorded<Notification<char>>(23, Notification.CreateOnNext('5')),
        new Recorded<Notification<char>>(27, Notification.CreateOnNext('6')),
        new Recorded<Notification<char>>(31, Notification.CreateOnNext('7')));

    //            11111111112222222222333
    //   12345678901234567890123456789012
    //ys ----y-----------y---y---y-------
    var ys = testScheduler.CreateColdObservable(
        new Recorded<Notification<char>>(5, Notification.CreateOnNext('A')),
        new Recorded<Notification<char>>(17, Notification.CreateOnNext('B')),
        new Recorded<Notification<char>>(21, Notification.CreateOnNext('C')),
        new Recorded<Notification<char>>(25, Notification.CreateOnNext('D')));


    //Expected :
    //Tick  x   y
    //5     1   A
    //17    4   B
    //25    5   D
    var expected = new[]
    {
        new Recorded<Notification<Tuple<char, char>>>(5, Notification.CreateOnNext(Tuple.Create('1', 'A'))),
        new Recorded<Notification<Tuple<char, char>>>(17, Notification.CreateOnNext(Tuple.Create('4', 'B'))),
        new Recorded<Notification<Tuple<char, char>>>(25, Notification.CreateOnNext(Tuple.Create('5', 'D')))
    };

    var observer = testScheduler.CreateObserver<Tuple<char, char>>();

    //Passes HOT, fails Cold. Doesn't meet the requirements due to Timeout anyway.
    //xs.Select(x => ys.Take(1)
    //                    .Timeout(TimeSpan.FromSeconds(0.5),
    //                            Observable.Empty<char>())
    //                    .Select(y => Tuple.Create(x, y))
    //    )
    //    .Switch()
    //    .Subscribe(observer);

    //Passes HOT. Passes Cold
    xs.Join(ys,
            _ => xs.Merge(ys),
            _ => Observable.Empty<Unit>(),
            Tuple.Create)
        .Subscribe(observer);

    testScheduler.Start();

    //You may want to Console.WriteLine out the two collections to validate for yourself.

    CollectionAssert.AreEqual(expected, observer.Messages);
}

PS 顺便说一句,Merge 很好地使用了答案。

于 2012-11-14T17:43:34.393 回答
3

I ended up using Join.

var rs = xs.Join(ys, 
                 _ => xs.Merge(ys),
                 _ => Observable.Empty<Unit>(),
                 Tuple.Create);

This is a good article that explains how Join works: http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/04/04/rx-join.aspx

于 2012-11-11T20:08:58.027 回答
0

To state the diagram another way, each x needs to take the first y without an additional x before the y. Those first requirement suggests Take(1). At that point, you will have an IObservable<IObservable<...>>. The second requirement and the type generated by the first two points to Switch. Putting it all together, you should be able to match the diagram with:

var rs = xs.Select(x => ys.Take(1)
                          .Select(y => Tuple.Create(x, y))
                  )
           .Switch()
于 2012-11-10T05:54:54.790 回答