@@ -9,6 +9,8 @@ import com.coder.toolbox.sdk.v2.models.WorkspaceAgent
99import com.coder.toolbox.util.withPath
1010import com.coder.toolbox.views.Action
1111import com.coder.toolbox.views.EnvironmentView
12+ import com.jetbrains.toolbox.api.remoteDev.AfterDisconnectHook
13+ import com.jetbrains.toolbox.api.remoteDev.BeforeConnectionHook
1214import com.jetbrains.toolbox.api.remoteDev.DeleteEnvironmentConfirmationParams
1315import com.jetbrains.toolbox.api.remoteDev.EnvironmentVisibilityState
1416import com.jetbrains.toolbox.api.remoteDev.RemoteProviderEnvironment
@@ -35,7 +37,7 @@ class CoderRemoteEnvironment(
3537 private val client : CoderRestClient ,
3638 private var workspace : Workspace ,
3739 private var agent : WorkspaceAgent ,
38- ) : RemoteProviderEnvironment(" ${workspace.name} .${agent.name} " ) {
40+ ) : RemoteProviderEnvironment(" ${workspace.name} .${agent.name} " ), BeforeConnectionHook, AfterDisconnectHook {
3941 private var wsRawStatus = WorkspaceAndAgentStatus .from(workspace, agent)
4042
4143 override var name: String = " ${workspace.name} .${agent.name} "
@@ -109,6 +111,21 @@ class CoderRemoteEnvironment(
109111 return actions
110112 }
111113
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+
112129 /* *
113130 * Update the workspace/agent status to the listeners, if it has changed.
114131 */
@@ -140,7 +157,8 @@ class CoderRemoteEnvironment(
140157 agent
141158 )
142159
143- override val connectionRequest: MutableStateFlow <Boolean >? = MutableStateFlow (false )
160+ private var isConnected = false
161+ override val connectionRequest: MutableStateFlow <Boolean > = MutableStateFlow (false )
144162
145163 /* *
146164 * Does nothing. In theory, we could do something like start the workspace
@@ -149,10 +167,9 @@ class CoderRemoteEnvironment(
149167 * to be much value.
150168 */
151169 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 ) {
154171 context.cs.launch {
155- connectionRequest? .update {
172+ connectionRequest.update {
156173 true
157174 }
158175 }
0 commit comments