@@ -9,6 +9,8 @@ import com.coder.toolbox.sdk.v2.models.WorkspaceAgent
9
9
import com.coder.toolbox.util.withPath
10
10
import com.coder.toolbox.views.Action
11
11
import com.coder.toolbox.views.EnvironmentView
12
+ import com.jetbrains.toolbox.api.remoteDev.AfterDisconnectHook
13
+ import com.jetbrains.toolbox.api.remoteDev.BeforeConnectionHook
12
14
import com.jetbrains.toolbox.api.remoteDev.DeleteEnvironmentConfirmationParams
13
15
import com.jetbrains.toolbox.api.remoteDev.EnvironmentVisibilityState
14
16
import com.jetbrains.toolbox.api.remoteDev.RemoteProviderEnvironment
@@ -35,7 +37,7 @@ class CoderRemoteEnvironment(
35
37
private val client : CoderRestClient ,
36
38
private var workspace : Workspace ,
37
39
private var agent : WorkspaceAgent ,
38
- ) : RemoteProviderEnvironment(" ${workspace.name} .${agent.name} " ) {
40
+ ) : RemoteProviderEnvironment(" ${workspace.name} .${agent.name} " ), BeforeConnectionHook, AfterDisconnectHook {
39
41
private var wsRawStatus = WorkspaceAndAgentStatus .from(workspace, agent)
40
42
41
43
override var name: String = " ${workspace.name} .${agent.name} "
@@ -109,6 +111,21 @@ class CoderRemoteEnvironment(
109
111
return actions
110
112
}
111
113
114
+ override fun getBeforeConnectionHooks (): List <BeforeConnectionHook > = listOf (this )
115
+
116
+ override fun getAfterDisconnectHooks (): List <AfterDisconnectHook > = listOf (this )
117
+
118
+ override fun beforeConnection () {
119
+ context.logger.info(" Connecting to $id ..." )
120
+ this .isConnected = true
121
+ }
122
+
123
+ override fun afterDisconnect () {
124
+ this .connectionRequest.update { false }
125
+ this .isConnected = false
126
+ context.logger.info(" Disconnected from $id " )
127
+ }
128
+
112
129
/* *
113
130
* Update the workspace/agent status to the listeners, if it has changed.
114
131
*/
@@ -140,7 +157,8 @@ class CoderRemoteEnvironment(
140
157
agent
141
158
)
142
159
143
- override val connectionRequest: MutableStateFlow <Boolean >? = MutableStateFlow (false )
160
+ private var isConnected = false
161
+ override val connectionRequest: MutableStateFlow <Boolean > = MutableStateFlow (false )
144
162
145
163
/* *
146
164
* Does nothing. In theory, we could do something like start the workspace
@@ -149,10 +167,9 @@ class CoderRemoteEnvironment(
149
167
* to be much value.
150
168
*/
151
169
override fun setVisible (visibilityState : EnvironmentVisibilityState ) {
152
- if (wsRawStatus.ready() && visibilityState.contentsVisible == true && visibilityState.isBackendConnected == false ) {
153
- context.logger.info(" Connecting to $id ..." )
170
+ if (wsRawStatus.ready() && visibilityState.contentsVisible == true && isConnected == false ) {
154
171
context.cs.launch {
155
- connectionRequest? .update {
172
+ connectionRequest.update {
156
173
true
157
174
}
158
175
}
0 commit comments