RxCPP的行为与Rx.Net不同

时间:2017-08-13 15:48:16

标签: c++ rxcpp

我正在使用RxCPP并且很难理解它的行为。

这是两个程序,一个在Rx.Net中,另一个在RxCPP中。 他们假设输出相同的印刷品,但他们没有 程序从鼠标流中获取点,并计算点之间的增量流。
鼠标是点流的流,每个笔划 -  从下到上是一个流。鼠标一个接一个地给出这样的流。

在这些试验中,预期产量为:
Delta no 0是:0,0
Delta no 1是:5,0
Delta no 2是:0,5
Delta no 3是:2,3
这是Rx.Net输出的内容。
Rx.Cpp仅输出第一行: Delta no 0是:0,0

有什么想法吗?

Rx.Cpp示例:

  #include <rx.hpp>
  namespace rx = rxcpp;
  namespace rxsub = rxcpp::subjects;
  using rxob = rx::observable<>;

    struct Point
    {
        Point(int x, int y) : x(x), y(y) {}

        int x = 0, y = 0;
        Point operator-() const { return {-x, -y}; }
        Point operator+(const Point& other) const { return Point{x + other.x, y + other.y}; }
        Point operator-(const Point& other) const { return operator+(-other); }
    };

    std::ostream& operator<<(std::ostream& o, const Point& p)
    {
        return o << "(" << p.x << ", " << p.y << ")";
    }

    void TestRxCPP()
    {
      using RxPoint = rx::observable<Point>;
      using Strokes = rx::observable<RxPoint>;
      using StrokesSubject = rxsub::subject<RxPoint>;

      StrokesSubject mouseSource;
      auto strokes = mouseSource.get_observable();

      auto deltaVectors = [](Strokes strokes) {
        auto deltas = strokes.flat_map([=](RxPoint stroke) {
            auto points = stroke;
            // create stream of delta vectors from start point
            auto firstPoint = points.take(1);
            auto delta =
                points.combine_latest([](Point v0, Point v1) { return v0 - v1; }, firstPoint);
            return delta;
        });

        return deltas;
      };

      auto delta = deltaVectors(strokes);
      int n = 0;
      delta.subscribe(
        [&](const Point& d) { std::cout << "Delta no. " << n++ << " is: " << d << std::endl; });

      auto testMouse = rxob::from(Point{3 + 0, 4 + 0}, Point{3 + 5, 4 + 0}, Point{3 + 0, 4 + 5}, Point{3 + 2, 4 + 3});
      mouseSource.get_subscriber().on_next(testMouse);
    }

Rx.Net示例:

    void RxNET()
    {
        var strokesS = new Subject<IObservable<Point>>();

        Func<IObservable<IObservable<Point>>, IObservable<Point>> 
        deltaVectors = strokes =>
        {
            var deltas = strokes.SelectMany(stroke =>
            {
                var points = stroke;
                // create stream of delta vectors from start point
                var firstPoint = points.Take(1);
                var deltaP =
                    points.CombineLatest(firstPoint, (v0, v1) => new Point(v0.X - v1.X, v0.Y - v1.Y));
                return deltaP;
            });

            return deltas;
        };

        var delta = deltaVectors(strokesS);
        var n = 0;
        delta.Subscribe(d => { Console.WriteLine($"Delta no {n++} is: {d}\n"); });

        var testMouse = new List<Point>
        {
            new Point(3 + 0, 4 + 0),
            new Point(3 + 5, 4 + 0),
            new Point(3 + 0, 4 + 5),
            new Point(3 + 2, 4 + 3)
        }.ToObservable();
        strokesS.OnNext(testMouse);
    }

1 个答案:

答案 0 :(得分:1)

Thanks to @Kirk Shoop at the rxcpp github :-)
这是一种HOTVCOLD行为。

笔划是COLD并且正在共享,并且只使用一个线程。 points.combine_latest(..., firstPoint)表示在订阅firstPoint之前发送所有积分。因此只发出最后一个delta。

如果您撤消combine_latest

,则可以使用COLD和HOT来源
auto delta =
    firstPoint.combine_latest([](Point v0, Point v1) { return v1 - v0; }, points);