TLDR; this is what worries me more, is there anyth...
# coroutines
d
TLDR; this is what worries me more, is there anything wrong here?
Copy code
override suspend fun observeCount() = coroutineScope {
    queries.count().asChannel().mapToOne( coroutineContext ).map { it.toInt() }
}
l
What is
mapToOne
?
d
Copy code
fun <T : Any> ReceiveChannel<Query<T>>.mapToOne( context: CoroutineContext ) =
    map( context ) { it.executeAsOne() }
l
What are you expecting exactly in this snippet, and what happens exactly at runtime instead?
d
I expect the ViewModel to receive the UiModel's from the Presenter, but it doesn't 😅
I see that the Channel created from the listener
offers
the Queries, but presenter never receive them
@louiscad you should take a look at the full snippet above for get a better idea 🙂 it's 90 lines of code but half are empty and very easy to understand 😋
l
There's a problem in invokeOnClose, you don't seem to pass a proper lambda. Also, the indentation doesn't follow the convention, I'm not sure why you didn't reformat at least to post here
The observe function of your presenter can't return until the channels are closed because the coroutines launched in the scope are not finished. You should not have it a suspending function but have it a regular function, extension for
CoroutineScope
instead.
d
I'm not getting it 🤔 the function on presenter correctly reaches its end 🤔 I will give a try, but I didn't really get it 😁 Why you saying the lambda on
invokeOnClose
is not right? The indentation stuff it's about AS beta that indentates wrongly, don't ask me why
It works 🙄
l
You can edit Kotlin code style, loading it from convention.
You mean you no longer have a problem with this code?
d
Yep, I did some edit for get rid of the other
coroutineScope {  }
In the Source
But I didn't get why it's working now ☹️ could you explain or give me so useful link?
l
As stated in the docs, the
coroutineScope { … }
function suspends until all coroutines launched inside it are completed or cancelled.
d
Now it's more clear, thanks 🍻