Skip to content

Commit 8afc1a0

Browse files
committed
feat: use dynamic polling interval for device authentication
- Implement `getPollingIntervalMs` to retrieve the polling interval from the `DevicePrompt` state. - Update `startPolling` to use the dynamic interval instead of a fixed constant. - Replace the hardcoded `POLL_INTERVAL_MS` (15s) with a `DEFAULT_POLL_INTERVAL_SEC` (5s) as a fallback. - Ensure the polling delay is converted correctly from seconds to milliseconds.
1 parent 4c10aaf commit 8afc1a0

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

feature/auth/presentation/src/commonMain/kotlin/zed/rainxch/auth/presentation/AuthenticationViewModel.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,24 @@ class AuthenticationViewModel(
218218

219219
private fun startPolling(deviceCode: String) {
220220
pollingJob?.cancel()
221+
val intervalMs = getPollingIntervalMs()
221222
pollingJob =
222223
viewModelScope.launch {
223224
while (isActive) {
224-
delay(POLL_INTERVAL_MS)
225+
delay(intervalMs)
225226
doPoll(deviceCode)
226227
}
227228
}
228229
}
229230

231+
private fun getPollingIntervalMs(): Long {
232+
val loginState = _state.value.loginState
233+
val intervalSec =
234+
(loginState as? AuthLoginState.DevicePrompt)?.start?.intervalSec
235+
?: DEFAULT_POLL_INTERVAL_SEC
236+
return (intervalSec * 1000).toLong()
237+
}
238+
230239
private fun pollOnce(deviceCode: String) {
231240
viewModelScope.launch {
232241
doPoll(deviceCode)
@@ -414,7 +423,7 @@ class AuthenticationViewModel(
414423
private const val KEY_INTERVAL_SEC = "auth_interval_sec"
415424
private const val KEY_EXPIRES_IN_SEC = "auth_expires_in_sec"
416425
private const val KEY_START_TIME_MILLIS = "auth_start_time_millis"
417-
private const val POLL_INTERVAL_MS = 15_000L
426+
private const val DEFAULT_POLL_INTERVAL_SEC = 5
418427

419428
private val SAVED_STATE_KEYS =
420429
listOf(

0 commit comments

Comments
 (0)