ribesg
04/23/2019, 8:59 AMsuspend fun <T> withMinimumDuration(duration: Long, block: suspend CoroutineScope.() -> T) =
coroutineScope {
val delay = async { delay(duration) }
val task = async { block() }
delay.join()
task.await()
}gildor
04/23/2019, 9:02 AMgildor
04/23/2019, 9:02 AMribesg
04/23/2019, 9:03 AMgildor
04/23/2019, 9:03 AMgildor
04/23/2019, 9:03 AMgildor
04/23/2019, 9:03 AMribesg
04/23/2019, 9:04 AMgildor
04/23/2019, 9:04 AMasync for delay, should be the sameribesg
04/23/2019, 9:05 AMCoroutineScope like launch and async but I don’t know. I didn’t even know coroutineScope, it was a quickfix to a warning. I’m not sure I want that. Maybe I want the delay even if there’s an error.ribesg
04/23/2019, 9:06 AMgildor
04/23/2019, 9:16 AMgildor
04/23/2019, 9:17 AMribesg
04/23/2019, 9:17 AMsuspend fun <T> withMinimumDuration(scope: CoroutineScope, duration: Long, block: suspend () -> T): T {
val delay = scope.async { delay(duration) }
try {
val result = block()
delay.join()
return result
} catch (e: Throwable) {
delay.join()
throw e
}
}gildor
04/23/2019, 9:18 AMgildor
04/23/2019, 9:19 AMribesg
04/23/2019, 9:20 AMduration, even if there is an error when calling blockribesg
04/23/2019, 9:21 AMcoroutineScope with a try-catch inside maybe?gildor
04/23/2019, 9:21 AMribesg
04/23/2019, 9:22 AMribesg
04/23/2019, 9:22 AMsuspend fun <T> withMinimumDuration(duration: Long, block: suspend () -> T) =
coroutineScope {
val delay = async { delay(duration) }
try {
block()
} finally {
delay.join()
}
}gildor
04/23/2019, 9:23 AMblock() will return faster than delay?ribesg
04/23/2019, 9:25 AMduration is 1000 and block is delay(500). I want the call to withMinimumDuration to last 1000msgildor
04/23/2019, 9:25 AMval task = async { block() } // start block
delay(duration) // minimal delay
task.await()ribesg
04/23/2019, 9:25 AMblock is throw Error()gildor
04/23/2019, 9:25 AMribesg
04/23/2019, 9:26 AMgildor
04/23/2019, 9:26 AM500 than withMinimumDuration will returngildor
04/23/2019, 9:26 AMribesg
04/23/2019, 9:26 AMgildor
04/23/2019, 9:27 AMgildor
04/23/2019, 9:27 AMgildor
04/23/2019, 9:27 AMribesg
04/23/2019, 9:27 AMgildor
04/23/2019, 9:28 AMgildor
04/23/2019, 9:28 AMribesg
04/23/2019, 9:28 AMribesg
04/23/2019, 9:29 AMgildor
04/23/2019, 9:40 AMawait()ribesg
04/23/2019, 9:42 AMribesg
04/23/2019, 9:44 AMsuspend fun <T> withMinimumDuration(duration: Long, block: suspend CoroutineScope.() -> T) =
supervisorScope {
val task = async(block = block)
delay(duration)
task.await()
}gildor
04/23/2019, 9:48 AM