Hi, First of great work with the 0.5.0 release! I...
# kotlinx-rpc
a
Hi, First of great work with the 0.5.0 release! I'm migrating from 0.4.0 -> 0.5.0 and I've run into a issue. I've put the service in a flow to handle reconnection. But it's quite awkward having the service in a flow because of the streamScoped. I'm wondering has anyone else found a better way to handle streamScoped when trying to do flatmaps? Bellow is a simplified example,
Copy code
@Rpc
interface MyService : RemoteService {
    suspend fun serviceItemsFlow(): Flow<String>
}

@OptIn(ExperimentalCoroutinesApi::class)
fun SharedFlow<MyService?>.mapServiceFlow(): Flow<String> =
    flatMapLatest {
        channelFlow {
            streamScoped {
                (it?.serviceItemsFlow() ?: emptyFlow())
                    .collect {
                        send(it)
                    }
            }
        }
    }
What I'm asking is there a better way to handle this?
Landed on two versions, Not sure if
wrapInStreamScope
is safe, streamScoped can outlive clients & service.
Copy code
fun <S, R> Flow<S>.flatMapLatestStreamScoped(
    transform: suspend (value: S) -> Flow<R>
): Flow<R> = transformLatest {
        streamScoped {
            emitAll(transform(it))
        }
    }

fun <S> Flow<S>.wrapInStreamScope(): Flow<S> = channelFlow {
    streamScoped {
            collect {
                send(it)
            }
        }
    }
If wrapInStreamScope is safe/fine, the would be nice to have this in the library or if we could annotate with something like streamScopedToFlow