Skip to content

Commit 220c55e

Browse files
authored
Merge pull request #2540 from digma-ai/remove-delay-in-protocol-command
remove delay in protocol command
2 parents 5455de3 + c1b1c78 commit 220c55e

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolApi.kt

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import com.intellij.openapi.components.Service
44
import com.intellij.openapi.diagnostic.Logger
55
import com.intellij.openapi.project.Project
66
import kotlinx.coroutines.CoroutineScope
7+
import kotlinx.coroutines.TimeoutCancellationException
78
import kotlinx.coroutines.delay
89
import kotlinx.coroutines.launch
10+
import kotlinx.coroutines.withTimeout
911
import org.digma.intellij.plugin.common.DisposableAdaptor
1012
import org.digma.intellij.plugin.errorreporting.ErrorReporter
1113
import 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
}

src/main/kotlin/org/digma/intellij/plugin/ui/mainapp/MainAppMessageRouterHandler.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.digma.intellij.plugin.ui.mainapp
22

33
import com.fasterxml.jackson.databind.JsonNode
4+
import com.intellij.openapi.components.service
45
import com.intellij.openapi.project.Project
56
import org.cef.browser.CefBrowser
67
import org.digma.intellij.plugin.analytics.AnalyticsServiceException
78
import org.digma.intellij.plugin.log.Log
9+
import org.digma.intellij.plugin.protocol.DigmaProtocolApi
810
import org.digma.intellij.plugin.ui.assets.AssetsMessageRouterHandler
911
import org.digma.intellij.plugin.ui.errors.ErrorsMessageRouterHandler
1012
import org.digma.intellij.plugin.ui.highlights.HighlightsMessageRouterHandler
@@ -66,6 +68,7 @@ class MainAppMessageRouterHandler(project: Project) : BaseMessageRouterHandler(p
6668
} catch (e: AnalyticsServiceException) {
6769
Log.warnWithException(logger, e, "error getting backend info")
6870
}
71+
project.service<DigmaProtocolApi>().setMainAppInitialized()
6972
}
7073

7174
}

0 commit comments

Comments
 (0)