Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeating a zipped interval has unexpected results #433

Open
djleach-belcan opened this issue Feb 19, 2018 · 1 comment
Open

Repeating a zipped interval has unexpected results #433

djleach-belcan opened this issue Feb 19, 2018 · 1 comment

Comments

@djleach-belcan
Copy link

djleach-belcan commented Feb 19, 2018

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
Copy link
Member

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

Sent with GitHawk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants