Skip to content

Commit 25e2e27

Browse files
committed
emit flow events from the same thread
Small improvement where we get rid of emitting environment and ssh connection trigger events from new coroutines. StateFlow in Kotlin is a hot, conflated flow that keeps only the most recent value. In other words we can immediately update the value without needing to launch a new coroutine, and we won't block the current thread.
1 parent e537c6a commit 25e2e27

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,8 @@ class CoderRemoteEnvironment(
276276

277277
private fun updateStatus(status: WorkspaceAndAgentStatus) {
278278
environmentStatus = status
279-
context.cs.launch(CoroutineName("Workspace Status Updater")) {
280-
state.update {
281-
environmentStatus.toRemoteEnvironmentState(context)
282-
}
279+
state.update {
280+
environmentStatus.toRemoteEnvironmentState(context)
283281
}
284282
context.logger.debug("Overall status for workspace $id is $environmentStatus. Workspace status: ${workspace.latestBuild.status}, agent status: ${agent.status}, agent lifecycle state: ${agent.lifecycleState}, login before ready: ${agent.loginBeforeReady}")
285283
}
@@ -312,10 +310,8 @@ class CoderRemoteEnvironment(
312310
*/
313311
fun startSshConnection(): Boolean {
314312
if (environmentStatus.ready() && !isConnected.value) {
315-
context.cs.launch(CoroutineName("SSH Connection Trigger")) {
316-
connectionRequest.update {
317-
true
318-
}
313+
connectionRequest.update {
314+
true
319315
}
320316
return true
321317
}

0 commit comments

Comments
 (0)