ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.05k stars 395 forks source link

Repeating a zipped interval has unexpected results #433

Open djleach-belcan opened 6 years ago

djleach-belcan commented 6 years ago

I'm trying to solve a problem similar to the one described here: https://stackoverflow.com/questions/41225446/rxjs5-emit-array-items-over-time-and-repeat-forever

I want to iterate over an array of values and propagate each value at specific intervals until a condition is met (using take_until). The examples below just repeat 3 times for testing. I had hoped I could do something described in the third answer to that question but the results are not what I expected.

This code:

auto interval = Rx::observable<>::interval(100ms);
auto data = Rx::observable<>::from(10, 20);
interval
    | Rx::zip([](auto&& interval, auto&& data)
        {
            logger->info("Interval: {}, Data: {}", interval, data);
            return data;
        }, data)
    | Rx::repeat(3)
    | Rx::subscribe<int>([](auto&&){});

Produces this output:

[time: 15:08:46.930] Interval: 1, Data: 10
[time: 15:08:47.034] Interval: 2, Data: 20
[time: 15:08:47.034] Interval: 1, Data: 10
[time: 15:08:47.034] Interval: 2, Data: 20
[time: 15:08:47.034] Interval: 1, Data: 10
[time: 15:08:47.034] Interval: 2, Data: 20

And replacing from with iterate and an array produces this output, which is even weirder:

[time: 15:11:13.064] Interval: 1, Data: 10
[time: 15:11:13.168] Interval: 2, Data: 20
[time: 15:11:13.168] Interval: 1, Data: 10
[time: 15:11:13.168] Interval: 2, Data: 20
[time: 15:11:13.268] Interval: 1, Data: 10
[time: 15:11:13.268] Interval: 2, Data: 20

Neither of these does what I expect with regard to timing. I had expected each of those values to be emitted 100ms apart but that is not the case. How can I best achieve what I'm trying to do? And is there documentation somewhere for interval that would explain this behavior? This seems to be related to another issue.

kirkshoop commented 6 years ago

Yes. This is the same bug. I think that a workaround would be to wrap the interval in defer

Sent with GitHawk