-
Notifications
You must be signed in to change notification settings - Fork 114
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
Transforming a sequence into a signal of sequence #262
Comments
Or maybe we just need a |
There is an initializer of Signal that takes a sequence of signals and flattens them into one signal. That would give you Signal(flattening: sequenceOfSignals, strategy: .concat)
.reduce([]) { $0 + [$1] } This will work correctly only if each of the signals from the sequence of signals emits just one event (participant) and then completes. You could do Using |
Thanks for your response ! Yes, I found that yesterday, while looking through the code, and tried to use it. However I couldn't manage to make the function definition work with the Error protocol. But I think I got it working. So I combined it with your suggestion of using
So I can now use it inside the Unfortunately, it only works every now and then, but mostly fails : the API calls get discarded (cancelled request) before they get a chance to be collected. So I need to figure this out... |
Context
I am finding, more often than not, that I am in need to transform a sequence (dictionary or array) into a signal of sequence.
This could happen either from an existing signal being flatMap'd to a sequence or simply an existing sequence from the imperative world that I need to transform (say via an API call on each element of the sequence) into a signal of sequence.
Taking the example of the chat room in AbsurdGitter. Let's imagine for a second that the Room entity holds a reference to the participants in the room, so something like :
A
roomSignal
can get me the currently selected room from which I can retrieve the active participants, as such :But if I want to transform the resulting signal into a signal of array of Users (retrieved from an API call to a third party backend), so that I can display the list of users in the room, then I always hit a snag :
So I am now left with a sequence (dictionary) in the flatMapLatest that will be transformed into an array of signals, when, what I want is a signal of sequence.
So :
Note : using a combination of
.flattenElements()
and.collect()
certainly makes things a lot easier. However, as mentioned in issue #209 , that will not work unless I prematurely finish theroomSignal
-- of course, I might actually be doing something really stupid (wouldn't put that past me!) and then using.flattenElements()
and.collect()
would be the way to go !Solution
If we extend Sequence to add a
toSignal()
method, with a transform function, that could potentially make things a lot smoother :I am just not sure that this is the right approach. Is there a better way to solve this problem ?
The text was updated successfully, but these errors were encountered: