Skip to content

Commit 26d2eb8

Browse files
committed
single cancelable refresh task
1 parent ba2d732 commit 26d2eb8

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Coder-Desktop/Coder-Desktop/State.swift

+14-5
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,28 @@ class AppState: ObservableObject {
155155
}
156156
}
157157

158+
private var refreshTask: Task<String?, Never>?
158159
public func refreshDeploymentConfig() async {
159-
if hasSession {
160-
let res = try? await retry(floor: .milliseconds(100), ceil: .seconds(10)) {
161-
let config = try await client!.agentConnectionInfoGeneric()
162-
return config.hostname_suffix
160+
// Client is non-nil if there's a sesssion
161+
if hasSession, let client {
162+
refreshTask?.cancel()
163+
164+
refreshTask = Task {
165+
let res = try? await retry(floor: .milliseconds(100), ceil: .seconds(10)) {
166+
let config = try await client.agentConnectionInfoGeneric()
167+
return config.hostname_suffix
168+
}
169+
return res
163170
}
164-
hostnameSuffix = res ?? Self.defaultHostnameSuffix
171+
172+
self.hostnameSuffix = await refreshTask?.value ?? Self.defaultHostnameSuffix
165173
}
166174
}
167175

168176
public func clearSession() {
169177
hasSession = false
170178
sessionToken = nil
179+
refreshTask?.cancel()
171180
client = nil
172181
reconfigure()
173182
}

0 commit comments

Comments
 (0)