Skip to content

Commit a461bdb

Browse files
authored
impl: auto-connect to toolbox backend (#24)
The auto connect happens only when: - workspace is running - the user clicks on one of the environments in the main page - there isn't already an established connection
1 parent 2a9f5e1 commit a461bdb

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/main/kotlin/com/coder/toolbox/CoderRemoteEnvironment.kt

+12-16
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,27 @@ class CoderRemoteEnvironment(
110110
override suspend
111111
fun getContentsView(): EnvironmentContentsView = EnvironmentView(client.url, workspace, agent)
112112

113+
override val connectionRequest: MutableStateFlow<Boolean>? = MutableStateFlow(false)
114+
113115
/**
114116
* Does nothing. In theory, we could do something like start the workspace
115117
* when you click into the workspace, but you would still need to press
116118
* "connect" anyway before the content is populated so there does not seem
117119
* to be much value.
118120
*/
119-
override fun setVisible(visibilityState: EnvironmentVisibilityState) {}
120-
121-
/**
122-
* Immediately send the state to the listener and store for updates.
123-
*/
124-
// override fun addStateListener(consumer: EnvironmentStateConsumer): Boolean {
125-
// // TODO@JB: It would be ideal if we could have the workspace state and
126-
// // the connected state listed separately, since right now the
127-
// // connected state can mask the workspace state.
128-
// // TODO@JB: You can still press connect if the environment is
129-
// // unreachable. Is that expected?
130-
// consumer.consume(status.toRemoteEnvironmentState(serviceLocator))
131-
// return super.addStateListener(consumer)
132-
// }
121+
override fun setVisible(visibilityState: EnvironmentVisibilityState) {
122+
if (wsRawStatus.ready() && visibilityState.contentsVisible == true && visibilityState.isBackendConnected == false) {
123+
context.logger.info("Connecting to $id...")
124+
context.cs.launch {
125+
connectionRequest?.update {
126+
true
127+
}
128+
}
129+
}
130+
}
133131

134132
override fun onDelete() {
135133
context.cs.launch {
136-
// TODO info and cancel pop-ups only appear on the main page where all environments are listed.
137-
// However, #showSnackbar works on other pages. Until JetBrains fixes this issue we are going to use the snackbar
138134
val shouldDelete = if (wsRawStatus.canStop()) {
139135
context.ui.showOkCancelPopup(
140136
context.i18n.ptrl("Delete running workspace?"),

0 commit comments

Comments
 (0)