@@ -6,7 +6,6 @@ package io.gitpod.jetbrains.remote
6
6
7
7
import com.intellij.codeWithMe.ClientId
8
8
import com.intellij.ide.BrowserUtil
9
- import com.intellij.openapi.application.ApplicationManager
10
9
import com.intellij.openapi.client.ClientSession
11
10
import com.intellij.openapi.client.ClientSessionsManager
12
11
import com.intellij.openapi.components.service
@@ -24,10 +23,7 @@ import io.netty.channel.ChannelHandlerContext
24
23
import io.netty.handler.codec.http.FullHttpRequest
25
24
import io.netty.handler.codec.http.QueryStringDecoder
26
25
import io.prometheus.client.exporter.common.TextFormat
27
- import kotlinx.coroutines.Dispatchers
28
- import kotlinx.coroutines.GlobalScope
29
- import kotlinx.coroutines.launch
30
- import kotlinx.coroutines.withContext
26
+ import kotlinx.coroutines.*
31
27
import org.jetbrains.ide.RestService
32
28
import org.jetbrains.io.response
33
29
import java.io.OutputStreamWriter
@@ -69,11 +65,7 @@ class GitpodCLIService : RestService() {
69
65
val file = parseFilePath(fileStr) ? : return " invalid file"
70
66
val shouldWait = getBooleanParameter(" wait" , urlDecoder)
71
67
return withClient(request, context) {
72
- GlobalScope .launch {
73
- withContext(Dispatchers .IO ) {
74
- cliHelperService.open(file, shouldWait)
75
- }
76
- }
68
+ cliHelperService.open(file, shouldWait)
77
69
}
78
70
}
79
71
if (operation == " preview" ) {
@@ -105,8 +97,8 @@ class GitpodCLIService : RestService() {
105
97
}
106
98
}
107
99
108
- private fun withClient (request : FullHttpRequest , context : ChannelHandlerContext , action : (project: Project ? ) -> Unit ): String? {
109
- ApplicationManager .getApplication().executeOnPooledThread {
100
+ private fun withClient (request : FullHttpRequest , context : ChannelHandlerContext , action : suspend (project: Project ? ) -> Unit ): String? {
101
+ GlobalScope .launch {
110
102
getClientSessionAndProjectAsync().let { (session, project) ->
111
103
ClientId .withClientId(session.clientId) {
112
104
action(project)
@@ -119,21 +111,21 @@ class GitpodCLIService : RestService() {
119
111
120
112
private data class ClientSessionAndProject (val session : ClientSession , val project : Project ? )
121
113
122
- private tailrec fun getClientSessionAndProjectAsync (): ClientSessionAndProject {
114
+ private suspend fun getClientSessionAndProjectAsync (): ClientSessionAndProject {
123
115
val project = getLastFocusedOrOpenedProject()
124
116
var session: ClientSession ? = null
125
- if (project != null ) {
126
- session = ClientSessionsManager .getProjectSessions(project, false ).firstOrNull()
127
- }
128
- if (session == null ) {
129
- session = ClientSessionsManager .getAppSessions(false ).firstOrNull()
130
- }
131
- return if (session != null ) {
132
- ClientSessionAndProject (session, project)
133
- } else {
134
- Thread .sleep(1000L )
135
- getClientSessionAndProjectAsync()
117
+ while (session == null ) {
118
+ if (project != null ) {
119
+ session = ClientSessionsManager .getProjectSessions(project, false ).firstOrNull()
120
+ }
121
+ if (session == null ) {
122
+ session = ClientSessionsManager .getAppSessions(false ).firstOrNull()
123
+ }
124
+ if (session == null ) {
125
+ delay(1000L )
126
+ }
136
127
}
128
+ return ClientSessionAndProject (session, project)
137
129
}
138
130
139
131
private fun parseFilePath (path : String ): Path ? {
0 commit comments