Skip to content

with_latest_from itself does not emit current value #374

@maiermic

Description

@maiermic

If you call with_latest_from with the same observable it is called on, the observable passed to with_latest_from does not emit the same value as the observable it has been called on:

#include <iostream>
#include "rxcpp/rx.hpp"

int main()
{
    rxcpp::rxsub::subject<int> sub;
    auto subscriber = sub.get_subscriber();
    auto source = sub.get_observable();
    source
        .with_latest_from(source)
        .subscribe([](auto p) {
            // I expect this to be called once,
            // but it is not.
            std::cout << '[' << std::get<0>(p) << ", " << std::get<1>(p) << "]\n";
        });
    subscriber.on_next(1);
    subscriber.on_completed();
}

Expected:

[1, 1]

Got: No output.


If you emit multiple values, the observable passed to with_latest_from still returns the old value (not the same):

#include <iostream>
#include "rxcpp/rx.hpp"

int main()
{
    rxcpp::rxsub::subject<int> sub;
    auto subscriber = sub.get_subscriber();
    auto source = sub.get_observable();
    source
        .with_latest_from(source)
        .subscribe([](auto p) {
            std::cout << '[' << std::get<0>(p) << ", " << std::get<1>(p) << "]\n";
        });
    subscriber.on_next(1);
    subscriber.on_next(2);
    subscriber.on_next(3);
    subscriber.on_completed();
}

Expected:

[1, 1]
[2, 2]
[3, 3]

Got:

[2, 1]
[3, 2]

It is the same behaviour if the observable passed depends on the observable with_latest_from is called on:

#include <iostream>
#include "rxcpp/rx.hpp"

int main()
{
    rxcpp::rxsub::subject<int> sub;
    auto subscriber = sub.get_subscriber();
    auto source = sub.get_observable();
    auto source_that_depends_on_source = source.filter([](auto x) { return x > 0; });
    source
        .with_latest_from(source_that_depends_on_source)
        .subscribe([](auto p) {
            std::cout << '[' << std::get<0>(p) << ", " << std::get<1>(p) << "]\n";
        });
    subscriber.on_next(1);
    subscriber.on_next(2);
    subscriber.on_next(3);
    subscriber.on_completed();
}

Expected:

[1, 1]
[2, 2]
[3, 3]

Got:

[2, 1]
[3, 2]

Note: I used something simple for source_that_depends_on_source because something more complex would be hard to comprehend.


PS: RxJS behaves like expected (run example):

var subject = new Rx.Subject();

subject
    .withLatestFrom(subject)
    .subscribe(p => console.log(p));

subject.onNext(1);
subject.onNext(2);
subject.onNext(3);
subject.onCompleted();

Output:

[1, 1]
[2, 2]
[3, 3]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions