@@ -50,12 +50,16 @@ 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 com.intellij.util.concurrency.NonUrgentExecutor
53
54
import kotlinx.coroutines.CoroutineScope
54
55
import kotlinx.coroutines.Dispatchers
55
56
import kotlinx.coroutines.Job
57
+ import kotlinx.coroutines.asCoroutineDispatcher
56
58
import kotlinx.coroutines.cancelAndJoin
57
59
import kotlinx.coroutines.job
58
60
import kotlinx.coroutines.launch
61
+ import kotlinx.coroutines.sync.Mutex
62
+ import kotlinx.coroutines.sync.withLock
59
63
import org.jetbrains.plugins.gradle.util.GradleUtil
60
64
61
65
class MinecraftFacetDetector : ProjectActivity {
@@ -69,15 +73,13 @@ class MinecraftFacetDetector : ProjectActivity {
69
73
70
74
override suspend fun execute (project : Project ) {
71
75
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
- }
76
+ MinecraftModuleRootListener .doCheckUnderProgress(project, detectorService)
77
77
}
78
78
79
79
@Service(Service .Level .PROJECT )
80
80
private class FacetDetectorScopeProvider (val scope : CoroutineScope ) {
81
+ val dispatcher = NonUrgentExecutor .getInstance().asCoroutineDispatcher()
82
+ val lock = Mutex ()
81
83
var currentJob: Job ? = null
82
84
}
83
85
@@ -89,12 +91,18 @@ class MinecraftFacetDetector : ProjectActivity {
89
91
90
92
val project = event.source as ? Project ? : return
91
93
val detectorService = project.service<FacetDetectorScopeProvider >()
92
- detectorService.scope.launch {
93
- detectorService.currentJob?.cancelAndJoin()
94
- withBackgroundProgress(project, " Detecting Minecraft Frameworks" , cancellable = false ) {
94
+ detectorService.scope.launch(detectorService.dispatcher) {
95
+ doCheckUnderProgress(project, detectorService)
96
+ }
97
+ }
98
+
99
+ suspend fun doCheckUnderProgress (project : Project , detectorService : FacetDetectorScopeProvider ) {
100
+ withBackgroundProgress(project, " Detecting Minecraft Frameworks" , cancellable = false ) {
101
+ detectorService.lock.withLock {
102
+ detectorService.currentJob?.cancelAndJoin()
95
103
detectorService.currentJob = coroutineContext.job
96
- doCheck(project)
97
104
}
105
+ doCheck(project)
98
106
}
99
107
}
100
108
0 commit comments