@@ -4,8 +4,10 @@ import com.intellij.openapi.components.Service
44import com.intellij.openapi.diagnostic.Logger
55import com.intellij.openapi.project.Project
66import kotlinx.coroutines.CoroutineScope
7+ import kotlinx.coroutines.TimeoutCancellationException
78import kotlinx.coroutines.delay
89import kotlinx.coroutines.launch
10+ import kotlinx.coroutines.withTimeout
911import org.digma.intellij.plugin.common.DisposableAdaptor
1012import org.digma.intellij.plugin.errorreporting.ErrorReporter
1113import org.digma.intellij.plugin.log.Log
@@ -23,14 +25,21 @@ class DigmaProtocolApi(val cs: CoroutineScope) : DisposableAdaptor {
2325
2426 private val logger: Logger = Logger .getInstance(this ::class .java)
2527
28+ private var mainAppInitialized = false
29+
30+ fun setMainAppInitialized () {
31+ mainAppInitialized = true
32+ Log .log(logger::trace, " main app initialized , thread={}" , Thread .currentThread().name)
33+ }
34+
35+
2636 /* *
2737 * return null on success.
2838 * error message on failure
2939 */
3040 fun performAction (project : Project , parameters : Map <String , String >, waitForJcef : Boolean ): String? {
3141 try {
3242
33-
3443 val action = getActionFromParameters(parameters) ? : return " DigmaProtocolCommand no action in request"
3544
3645 Log .log(logger::trace, " perform action {}, thread {}" , action, Thread .currentThread().name)
@@ -75,8 +84,9 @@ class DigmaProtocolApi(val cs: CoroutineScope) : DisposableAdaptor {
7584 }
7685
7786 cs.launch {
87+
7888 if (waitForJcef) {
79- delay( 5000 )
89+ waitForJcef( )
8090 }
8191
8292 val scope = SpanScope (codeObjectId)
@@ -90,13 +100,32 @@ class DigmaProtocolApi(val cs: CoroutineScope) : DisposableAdaptor {
90100
91101 private fun showAssetTab (project : Project , action : String , waitForJcef : Boolean ): String? {
92102 cs.launch {
103+
93104 if (waitForJcef) {
94- delay( 5000 )
105+ waitForJcef( )
95106 }
107+
96108 project.messageBus.syncPublisher(ProtocolCommandEvent .PROTOCOL_COMMAND_TOPIC ).protocolCommand(action)
97109 }
98110 return null
99111 }
100112
101113
114+ private suspend fun waitForJcef () {
115+
116+ try {
117+ withTimeout(5000 ) {
118+ while (! mainAppInitialized) {
119+ delay(100 )
120+ }
121+ }
122+
123+ } catch (e: TimeoutCancellationException ) {
124+ // ignore
125+ }
126+
127+ // wait another second , it seems to be necessary to let jcef completely initialize
128+ delay(1000 )
129+ }
130+
102131}
0 commit comments