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
The once
FlattenStrategy.
#516
Conversation
Sources/Flatten.swift
Outdated
@@ -164,6 +181,9 @@ extension Signal where Value: SignalProducerConvertible, Error == NoError, Value | |||
|
|||
case .race: | |||
return self.race() | |||
|
|||
case .once: | |||
return self.take(first: 1).race() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the description you explain it's equivalent to flatMap(.latest)
, but internally you are using race
. Which one is true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there's only one signal, all the strategies are functionally equivalent. The only difference is the performance characteristics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not have a case for performance here yet, just the room for optimization.
Since there's only one signal, all the strategies are functionally equivalent.
race
propagates interrupted
from the inner stream to the flattened stream. The other strategies do not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, so the strategy is more or less relevant. race
gives the closest semantic to then
(which is essentially flatMapCompleted
).
I'm pretty ambivalent about this addition. 😕 |
I'm a fan of this addition, the app I most use ReactiveSwift with has a lot of cases that'd benefit from this readability. |
@mdiep I think now it has a case of performance, despite not practically relevant, in the sense that the take-once constraint enables simpler implementation. 😛 |
Maybe it's time to reconsider adding a |
I am not quite convinced to have it as a dedicated type, since there is barely a compile time advantage AFAICT. From the static enforcement PoV, To facilitate it, we would also have to replicate the entire API of |
I guess this alone could be the primary driver though, since that's also how the division between |
Yeah, I think there are benefits to documentation/reasoning. Many common tasks could be represented as a observable of one value. It would be easy to have APIs to convert between them as well. The biggest issue is coming up with a good name. |
Related: #423
This is a convenience that has been using in our codebase for a while, implemented as
take(first: 1).race()
. There are a lot of circumstances that we need only one value from a continuous producer (e.g. property).For example, we can fold this:
into:
While it doesn't add substantial functionality, it offers a sensible choice for flattening single-value stream of streams, that also reduces verbosity, from an API consumer POV.
Checklist