Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.intellij.openapi.util.CheckedDisposable
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.psi.PsiDocumentManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.gradle.tooling.CancellationTokenSource
import org.gradle.tooling.GradleConnector
Expand All @@ -54,6 +55,7 @@ class ApolloCodegenService(
private var dirtyGqlDocument: Document? = null

private var gradleCodegenCancellation: CancellationTokenSource? = null
private var apolloCompilerCodegenJob: Job? = null

init {
logd("project=${project.name}")
Expand Down Expand Up @@ -165,7 +167,9 @@ class ApolloCodegenService(

if (apolloKotlinService?.hasCompilerOptions == true) {
// We can use the built-in Apollo compiler
ApolloCompilerHelper(project).generateSources(apolloKotlinService)
apolloCompilerCodegenJob = coroutineScope.launch {
ApolloCompilerHelper(project).generateSources(apolloKotlinService)
}
} else {
// Fall back to the Gradle codegen task
startGradleCodegen()
Expand Down Expand Up @@ -231,6 +235,8 @@ class ApolloCodegenService(

private fun stopCodegen() {
logd()
apolloCompilerCodegenJob?.cancel()
apolloCompilerCodegenJob = null
gradleCodegenCancellation?.cancel()
gradleCodegenCancellation = null
}
Expand All @@ -257,7 +263,9 @@ class ApolloCodegenService(
private fun startCodegen() {
if (project.apolloKotlinProjectModelService.getApolloKotlinServices().any { it.hasCompilerOptions }) {
logd("Using Apollo compiler for codegen")
ApolloCompilerHelper(project).generateAllSources()
apolloCompilerCodegenJob = coroutineScope.launch {
ApolloCompilerHelper(project).generateAllSources()
Comment on lines +266 to +267
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generateAllSources() is 100% blocking code IIRC. There is no need for a coroutine, a plain Executor should be enough. Also worth noting that you may end up with multiple compilation happening in parallel (not safe) if you start/stop/start/stop quickly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guide recommends using coroutines with the provided scope to do background work nowadays, also saying that blocking code is OK. But the doc (+ situation with different versions of IDEs) is kind of messy and I'm actually not sure about whether job.cancel() actually works here. I'll test a bit more.

}
} else {
logd("Using Gradle codegen task")
startGradleCodegen()
Expand Down
Loading