@@ -50,12 +50,10 @@ import com.intellij.openapi.startup.ProjectActivity
50
50
import com.intellij.openapi.util.Key
51
51
import com.intellij.platform.ide.progress.withBackgroundProgress
52
52
import com.intellij.platform.util.progress.forEachWithProgress
53
- import kotlinx.coroutines.CoroutineScope
54
- import kotlinx.coroutines.Dispatchers
55
- import kotlinx.coroutines.Job
56
- import kotlinx.coroutines.cancelAndJoin
57
- import kotlinx.coroutines.job
58
- import kotlinx.coroutines.launch
53
+ import com.intellij.util.concurrency.NonUrgentExecutor
54
+ import kotlinx.coroutines.*
55
+ import kotlinx.coroutines.sync.Mutex
56
+ import kotlinx.coroutines.sync.withLock
59
57
import org.jetbrains.plugins.gradle.util.GradleUtil
60
58
61
59
class MinecraftFacetDetector : ProjectActivity {
@@ -69,15 +67,13 @@ class MinecraftFacetDetector : ProjectActivity {
69
67
70
68
override suspend fun execute (project : Project ) {
71
69
val detectorService = project.service<FacetDetectorScopeProvider >()
72
- detectorService.currentJob?.cancelAndJoin()
73
- withBackgroundProgress(project, " Detecting Minecraft Frameworks" , cancellable = false ) {
74
- detectorService.currentJob = coroutineContext.job
75
- MinecraftModuleRootListener .doCheck(project)
76
- }
70
+ MinecraftModuleRootListener .doCheckUnderProgress(project, detectorService)
77
71
}
78
72
79
73
@Service(Service .Level .PROJECT )
80
74
private class FacetDetectorScopeProvider (val scope : CoroutineScope ) {
75
+ val dispatcher = NonUrgentExecutor .getInstance().asCoroutineDispatcher()
76
+ val lock = Mutex ()
81
77
var currentJob: Job ? = null
82
78
}
83
79
@@ -89,12 +85,18 @@ class MinecraftFacetDetector : ProjectActivity {
89
85
90
86
val project = event.source as ? Project ? : return
91
87
val detectorService = project.service<FacetDetectorScopeProvider >()
92
- detectorService.scope.launch {
93
- detectorService.currentJob?.cancelAndJoin()
94
- withBackgroundProgress(project, " Detecting Minecraft Frameworks" , cancellable = false ) {
88
+ detectorService.scope.launch(detectorService.dispatcher) {
89
+ doCheckUnderProgress(project, detectorService)
90
+ }
91
+ }
92
+
93
+ suspend fun doCheckUnderProgress (project : Project , detectorService : FacetDetectorScopeProvider ) {
94
+ withBackgroundProgress(project, " Detecting Minecraft Frameworks" , cancellable = false ) {
95
+ detectorService.lock.withLock {
96
+ detectorService.currentJob?.cancelAndJoin()
95
97
detectorService.currentJob = coroutineContext.job
96
- doCheck(project)
97
98
}
99
+ doCheck(project)
98
100
}
99
101
}
100
102
0 commit comments