Skip to content

Commit cce41b0

Browse files
committed
fix: workspaces were not properly loaded
- proper usage of coroutine's stateflow to update values - also fixed issues were state transition were not properly rendered
1 parent 09c99d1 commit cce41b0

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import kotlinx.coroutines.CoroutineScope
2323
import kotlinx.coroutines.delay
2424
import kotlinx.coroutines.flow.MutableStateFlow
2525
import kotlinx.coroutines.flow.StateFlow
26+
import kotlinx.coroutines.flow.update
2627
import kotlinx.coroutines.isActive
2728
import kotlinx.coroutines.launch
2829
import kotlinx.coroutines.withTimeout
@@ -41,16 +42,16 @@ class CoderRemoteEnvironment(
4142
private var agent: WorkspaceAgent,
4243
private var cs: CoroutineScope,
4344
) : RemoteProviderEnvironment("${workspace.name}.${agent.name}") {
44-
private var status = WorkspaceAndAgentStatus.from(workspace, agent)
45+
private var wsRawStatus = WorkspaceAndAgentStatus.from(workspace, agent)
4546

4647
private val ui: ToolboxUi = serviceLocator.getService(ToolboxUi::class.java)
4748
private val i18n = serviceLocator.getService(LocalizableStringFactory::class.java)
4849

4950
override var name: String = "${workspace.name}.${agent.name}"
50-
override val state: StateFlow<RemoteEnvironmentState>
51-
get() = TODO("Not yet implemented")
52-
override val description: StateFlow<EnvironmentDescription>
53-
get() = TODO("Not yet implemented")
51+
override val state: MutableStateFlow<RemoteEnvironmentState> =
52+
MutableStateFlow(wsRawStatus.toRemoteEnvironmentState(serviceLocator))
53+
override val description: MutableStateFlow<EnvironmentDescription> =
54+
MutableStateFlow(EnvironmentDescription.General(i18n.pnotr(workspace.templateName)))
5455

5556
override val actionsList: StateFlow<List<ActionDescription>> = MutableStateFlow(
5657
listOf(
@@ -76,12 +77,12 @@ class CoderRemoteEnvironment(
7677
}
7778
}
7879
},
79-
Action(i18n.ptrl("Start"), enabled = { status.canStart() }) {
80+
Action(i18n.ptrl("Start"), enabled = { wsRawStatus.canStart() }) {
8081
val build = client.startWorkspace(workspace)
8182
workspace = workspace.copy(latestBuild = build)
8283
update(workspace, agent)
8384
},
84-
Action(i18n.ptrl("Stop"), enabled = { status.canStop() }) {
85+
Action(i18n.ptrl("Stop"), enabled = { wsRawStatus.canStop() }) {
8586
val build = client.stopWorkspace(workspace)
8687
workspace = workspace.copy(latestBuild = build)
8788
update(workspace, agent)
@@ -99,11 +100,11 @@ class CoderRemoteEnvironment(
99100
fun update(workspace: Workspace, agent: WorkspaceAgent) {
100101
this.workspace = workspace
101102
this.agent = agent
102-
val newStatus = WorkspaceAndAgentStatus.from(workspace, agent)
103-
if (newStatus != status) {
104-
status = newStatus
105-
val state = status.toRemoteEnvironmentState(serviceLocator)
106-
// listenerSet.forEach { it.consume(state) }
103+
wsRawStatus = WorkspaceAndAgentStatus.from(workspace, agent)
104+
cs.launch {
105+
state.update {
106+
wsRawStatus.toRemoteEnvironmentState(serviceLocator)
107+
}
107108
}
108109
}
109110

@@ -139,7 +140,7 @@ class CoderRemoteEnvironment(
139140
cs.launch {
140141
// TODO info and cancel pop-ups only appear on the main page where all environments are listed.
141142
// However, #showSnackbar works on other pages. Until JetBrains fixes this issue we are going to use the snackbar
142-
val shouldDelete = if (status.canStop()) {
143+
val shouldDelete = if (wsRawStatus.canStop()) {
143144
ui.showOkCancelPopup(
144145
i18n.ptrl("Delete running workspace?"),
145146
i18n.ptrl("Workspace will be closed and all the information in this workspace will be lost, including all files, unsaved changes and historical."),
@@ -161,7 +162,7 @@ class CoderRemoteEnvironment(
161162
withTimeout(5.minutes) {
162163
var workspaceStillExists = true
163164
while (cs.isActive && workspaceStillExists) {
164-
if (status == WorkspaceAndAgentStatus.DELETING || status == WorkspaceAndAgentStatus.DELETED) {
165+
if (wsRawStatus == WorkspaceAndAgentStatus.DELETING || wsRawStatus == WorkspaceAndAgentStatus.DELETED) {
165166
workspaceStillExists = false
166167
serviceLocator.getService(EnvironmentUiPageManager::class.java)
167168
.showPluginEnvironmentsPage()

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import kotlinx.coroutines.Job
3535
import kotlinx.coroutines.delay
3636
import kotlinx.coroutines.flow.MutableStateFlow
3737
import kotlinx.coroutines.flow.StateFlow
38+
import kotlinx.coroutines.flow.update
3839
import kotlinx.coroutines.isActive
3940
import kotlinx.coroutines.launch
4041
import okhttp3.OkHttpClient
@@ -82,7 +83,7 @@ class CoderRemoteProvider(
8283
private var firstRun = true
8384

8485
override val environments: MutableStateFlow<LoadableState<List<RemoteProviderEnvironment>>> = MutableStateFlow(
85-
LoadableState.Loading
86+
LoadableState.Value(emptyList())
8687
)
8788

8889
/**
@@ -134,7 +135,9 @@ class CoderRemoteProvider(
134135
cli.configSsh(newEnvironments.map { it.name }.toSet())
135136
}
136137

137-
environments.value = LoadableState.Value(resolvedEnvironments.toList())
138+
environments.update {
139+
LoadableState.Value(resolvedEnvironments.toList())
140+
}
138141

139142
lastEnvironments = resolvedEnvironments
140143
} catch (_: CancellationException) {

0 commit comments

Comments
 (0)