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

performStream vs performStreamLatest #95

Open
stevekrouse opened this issue Dec 12, 2018 · 4 comments
Open

performStream vs performStreamLatest #95

stevekrouse opened this issue Dec 12, 2018 · 4 comments

Comments

@stevekrouse
Copy link
Contributor

I don't get the difference between them as explained in the documentation. Can you maybe give an example use case for each that highlight the differences?

@limemloh
Copy link
Member

Sure 😄 I will do my best.

There are 2 variations of performStream in Hareactive, which is performStreamLatest and performStreamOrdered. All of them takes a Stream of IOs and return a stream of IO-results.
When an IO is occurring in the given stream, all of them behave the same and run the IO straight away, the difference is how the resulting stream behaves. Since the IOs can be asynchronous the result might not be available in the same order as the IO stream (Might not even be available at all).

Now performStream returns a stream which has IO-results occurring in the same order as the result become available and not necessarily in the same order as the IO stream.
This is fine for stuff where the resulting order does not matter, such as sending data to a server.

performStreamOrdered on the other hand keep track of the order of the IOs and ensures that the results stream follow the same order. Meaning that even if a result is available it will only appear in the resulting stream when the result of every IO before it have a available result.
This might be useful if you are using scan on the resulting stream and you are accumulating something where the order is important.

performStreamLatest also keeps track of the order of IOs, but instead of restraining the IO-results like performStreamOrdered it occurs with the result of the latest IO available and ignores the results of every IO before it if their result is not available.
You might use performStreamLatest for when you are implementing an input with suggestions, while the user types the input fetches some suggestions from the server. Here you only care about the latest response from the server. If you were to use performStream you might show a suggestion from something the user typed earlier and if you were to use performStreamOrdered one of the earlier fetches might block for the most recent result.

I hope this was somewhat helpful otherwise let me know and I will try to come up with more use cases.
😃

@paldepind
Copy link
Member

As a supplement to the above, here is a concrete (but contrived) example that illustrates the difference.

Let's say we have a stream like this one:

[(1, ioA), (2, ioB), (3, ioC)]

At time 1 the stream has an occurrence with the value ioA, at time 2 it has an occurrence with the value ioB, and at time 3 it has an occurrence with the value ioC. Furthermore, let's say ioA takes 1.2 time units to execute, that ioB takes 3 time units, and that ioC takes 1 time unit.

If we apply performStream to the stream we get

[(2.2, "a"), (4, "c"), (5, "b")

The function executes each IO and delivers the result as soon as it is available.

If we apply performStreamLatest to the stream we get

[(2.2, "a"), (4, "b")]

The function executes each IO but discards "old" results. Hence when ioB finishes its result is discarded since a more recent IO has already occurred.

If we apply performStreamOrdered to the stream we get

[(2.2, "a"), (5, "b"), (5, "c")

The function delivers each result in the same order that the IOs appeared. Hence when ioC is finished its result is delayed until the result from ioB is also available.

@stevekrouse
Copy link
Contributor Author

This is very helpful! Want me to submit a pull req to add some of this to the documentation?

I get it now. Was there a typo in your explanation @paldepind? performStreamLatest should throw away the b, not the c, right?

I made a picture to help see it:

image

@paldepind
Copy link
Member

Was there a typo in your explanation @paldepind? performStreamLatest should throw away the b, not the c, right?

Yes!

Want me to submit a pull req to add some of this to the documentation?

That would be wonderful.

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

No branches or pull requests

3 participants