Skip to content

Commit e64baef

Browse files
committed
Use coroutines in ApolloCodegenService (prevents a crash when calling getExecutionSettings) (#6477)
(cherry picked from commit 1bd8425)
1 parent c4ddd9c commit e64baef

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/codegen/ApolloCodegenService.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ import com.intellij.openapi.project.Project
3333
import com.intellij.openapi.roots.ProjectRootManager
3434
import com.intellij.openapi.util.CheckedDisposable
3535
import com.intellij.openapi.vfs.VfsUtil
36+
import kotlinx.coroutines.CoroutineScope
37+
import kotlinx.coroutines.launch
3638
import org.gradle.tooling.CancellationTokenSource
3739
import org.gradle.tooling.GradleConnector
3840
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
3941
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
4042
import org.jetbrains.plugins.gradle.util.GradleConstants
4143
import java.io.File
42-
import java.util.concurrent.Executors
4344

4445
@Service(Service.Level.PROJECT)
4546
class ApolloCodegenService(
4647
private val project: Project,
48+
private val coroutineScope: CoroutineScope,
4749
) : Disposable {
4850
private var documentChangesDisposable: CheckedDisposable? = null
4951
private var fileEditorChangesDisposable: CheckedDisposable? = null
@@ -53,8 +55,6 @@ class ApolloCodegenService(
5355

5456
private var gradleCodegenCancellation: CancellationTokenSource? = null
5557

56-
private val gradleExecutorService = Executors.newSingleThreadExecutor()
57-
5858
init {
5959
logd("project=${project.name}")
6060
startOrStopCodegenObservers()
@@ -181,10 +181,9 @@ class ApolloCodegenService(
181181

182182
val modules = ModuleManager.getInstance(project).modules
183183
val rootProjectPath = project.getGradleRootPath() ?: return
184-
val executionSettings =
185-
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
186-
187-
gradleExecutorService.submit {
184+
coroutineScope.launch {
185+
val executionSettings =
186+
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
188187
val gradleExecutionHelper = GradleExecutionHelper()
189188
gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
190189
gradleCodegenCancellation = GradleConnector.newCancellationTokenSource()
@@ -257,6 +256,5 @@ class ApolloCodegenService(
257256
override fun dispose() {
258257
logd("project=${project.name}")
259258
stopContinuousGradleCodegen()
260-
gradleExecutorService.shutdown()
261259
}
262260
}

0 commit comments

Comments
 (0)