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

Should flatMap restart the inner observable if it's re-emitted? #55

Closed
raquo opened this issue Dec 28, 2020 · 3 comments
Closed

Should flatMap restart the inner observable if it's re-emitted? #55

raquo opened this issue Dec 28, 2020 · 3 comments
Labels

Comments

@raquo
Copy link
Owner

raquo commented Dec 28, 2020

Consider the following code for both SwitchStreamStrategy and SwitchSignalStrategy:

parentObservable.flatMap(parentEvent => makeChildObservable(parentEvent))

if parentObservable emits 1 then 2, and makeChildObservable(parentEvent) returns the exact same childObservable both times, that childObservable will be stopped and then started again when the parent emits 2.

I'm inclined to change the behaviour so that childObservable is not restarted in this case, so that it just continues running, but I'm not sure. Some streams have side effects when they start (such as the new AjaxEventStream), so while this should be a rare issue, it's not just a minor performance concern, but a potentially breaking behaviour change.

My thinking is that if the reference to childObservable is exactly the same in both cases, the user likely intended to cache it for some reason, probably to avoid recreating / restarting it.

The main question is, what do users expect to happen in this case? Do you expect nothing to happen when switching from childObservable to the exact same childObservable (same reference, not just ==), or do you expect it to be stopped and immediately started again? Do you have code that would be affected by this?

I could make this configurable if there is demand for that, but I still need to figure out which default would make the most sense.

@raquo raquo added the design label Dec 28, 2020
@yurique
Copy link
Contributor

yurique commented Dec 28, 2020

If the returned childObservable is exactly the same every time — that would look awkward to me.

What would happen in the following case, for example?

parentObservable1.flatMap(parentEvent => makeChildObservable(parentEvent))
parentObservable2.flatMap(parentEvent => makeChildObservable(parentEvent))

I've never done this, so I don't really case about this particular case :), but I suppose changing the behavior to not restart it will make it make somewhat more sense. Unless someone has a really good use case for the opposite.

@raquo
Copy link
Owner Author

raquo commented Dec 28, 2020

To clarify, when I said "stopped" and "restarted", I meant assuming there are no other observers of childObservable. That's the only case when removing an internal observer from childObservable and then adding it back results in it being restarted.

That's actually a very good point. childObservable will only be restarted if it has no other listeners (such as parentObservable2.flatMap(...) if that's the case). To me this is further evidence that users shouldn't rely on the flatMap's current behaviour of restarting childObservable as I described, as whether it happens depends on stuff outside of this flatMap, which is weird.

@raquo
Copy link
Owner Author

raquo commented Jan 5, 2021

Fixed for v0.12.0

Discovered another questionable edge case while fixing this. Documented here for now. I'm not sure what the right behaviour should be, and this is sort of related to #43 so I'm not gonna address it yet.

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

No branches or pull requests

2 participants