[英]Kotlin multi platform flow block convert to custom wrapper flow use for ios
I have a kotlin multi platform project which contains apollo graphql api我有一个包含 apollo graphql api 的 kotlin 多平台项目
in this project i have BaseRepository Class and in this class there is a method to execute query or mutations在这个项目中,我有 BaseRepository 类,在这个类中,有一种方法可以执行查询或突变
suspend fun <D : Query.Data> executeQuery(query: Query<D>): ApolloResponse<D> {
val response = getApolloClient().query(query).execute()
checkOperation(response)
return response
}
suspend fun <D : Mutation.Data> executeMutation(mutation: Mutation<D>): ApolloResponse<D> {
val response = getApolloClient().mutation(mutation).execute()
checkOperation(response)
return response
}
For example i want to use this method in Some repository like this例如,我想在这样的存储库中使用此方法
class HelpRepository : BaseRepository() {
fun test(request: AddFeedBackRequest) = flow {
val feedBackType = if (request.type == AddFeedBackType.Bug) {
FeedbackType.BUG
} else {
FeedbackType.FEEDBACK
}
val input = AddFeedbackInput(request.note, Optional.presentIfNotNull(feedBackType))
emit(true)
val mutation = AddFeedbackMutation(input)
val response = executeMutation(mutation)
emit(false)
}
}
when i add the flow scope i shouldn't be had to convert this method to a suspend function当我添加流范围时,我不应该将此方法转换为挂起函数
i dont want to use suspend function because of ios application.由于 ios 应用程序,我不想使用挂起功能。 When i use suspend function its convert "Kotlinx_coroutines_coreFlowCollector" in xcode
当我使用挂起函数时,它在 xcode 中转换“Kotlinx_coroutines_coreFlowCollector”
so i found a wrapper function like this所以我找到了这样的包装函数
fun <T> Flow<T>.asCommonFlow(): CommonFlow<T> = CommonFlow(this)
class CommonFlow<T>(private val origin: Flow<T>) : Flow<T> by origin {
fun listen(block: (T) -> Unit): Closeable {
val job = Job()
onEach {
block(it)
}.launchIn(CoroutineScope(Dispatchers.Main + job))
return object : Closeable {
override fun close() {
job.cancel()
}
}
}
}
when i use this wrapper with single variable it works exactly what i want in xcode.当我将此包装器与单个变量一起使用时,它的工作原理正是我在 xcode 中想要的。
but in functions i couldn't find a proper way to do this但在功能中我找不到合适的方法来做到这一点
i need a wrapper like我需要一个像
= commonFlow {
}
instead of this
= flow {
}
to use this method as a commonFlow wrapper将此方法用作 commonFlow 包装器
Can you help me ?你能帮助我吗 ?
We have pretty much the same thing in one of our projects.在我们的一个项目中,我们有几乎相同的东西。 We have a extension function that converts the regular flow to a "common" flow so it can be used in both Android and iOS.
我们有一个扩展功能,可以将常规流转换为“通用”流,因此它可以在 Android 和 iOS 中使用。
You can created flow like always, and wrap it at the end.您可以像往常一样创建流程,并在最后包装它。
fun <T> Flow<T>.wrap(): CommonFlow<T> = CommonFlow(this)
class HelpRepository : BaseRepository() {
fun test(request: AddFeedBackRequest) = flow {
val feedBackType = if (request.type == AddFeedBackType.Bug) {
FeedbackType.BUG
} else {
FeedbackType.FEEDBACK
}
val input = AddFeedbackInput(request.note, Optional.presentIfNotNull(feedBackType))
emit(true)
val mutation = AddFeedbackMutation(input)
val response = executeMutation(mutation)
emit(false)
}
}.wrap()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.