以下withContext
的实现对扩展为block
的{{1}}进行调用是否有效?
coroutineContext
种类类似于suspend fun <T, R> withContext(context: CoroutineContext, receiver: T, block: suspend T.() -> R): R {
val newCoroutineContext = coroutineContext + context
return suspendCoroutine { cont ->
block.startCoroutine(receiver, Continuation(newCoroutineContext) { cont.resumeWith(it) })
}
}
中的withContext
,但是在较低的级别上没有Job API,因为我无法在标准库中找到它。
由于我对拦截不感兴趣,我应该进一步使用kotlinx.coroutines
吗?我不了解suspendCoroutineUninterceptedOrReturn
中使用的SafeContinuation
中的机械要点。那么通过suspendCoroutine
对于withContext
的有效实现是什么?
有关更多情况,我正在研究一个“构建器”,以从一个暂停块中实现suspendCoroutineUninterceptedOrReturn
(请参见下面的AnAction
),并且我想实现一个包装器,而不是确保{ {1}},并向fun actionBuilder
注入自定义event.project !=null
。
我不想使用ProjectElement
的原因是在此级别上,构建器仅用于避免coroutineContext
和withContext
之间的代码重复(其中{{1} }仅用于标记进入update
的部分)。因此,我想仅在需要时(例如后台工作)才开始使用Job API,只需在顶部实现actionPerformed
。
P.S。到目前为止,代码似乎可以正常工作,并且ActionScope<T>.execute
发生了错误,因为actionPerformed
创建了根作业,但是由于某种原因而不是取消(不确定此时是否有问题)。 / p>
executeScope