Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental |
| 2 | |
| 3 | import java.util.concurrent.TimeUnit |
| 4 | import kotlin.coroutines.ContinuationInterceptor |
| 5 | |
| 6 | /** |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 7 | * This dispatcher _feature_ is implemented by [CoroutineDispatcher] implementations that natively support |
| 8 | * non-blocking [delay] function. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 9 | */ |
| 10 | public interface Delay { |
| 11 | /** |
| 12 | * Delays coroutine for a given time without blocking a thread and resumes it after a specified time. |
| 13 | * This suspending function is cancellable. |
| 14 | * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| 15 | * immediately resumes with [CancellationException]. |
| 16 | */ |
| 17 | suspend fun delay(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) { |
| 18 | require(time >= 0) { "Delay time $time cannot be negative" } |
| 19 | if (time <= 0) return // don't delay |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 20 | return suspendCancellableCoroutine { scheduleResumeAfterDelay(time, unit, it) } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | /** |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 24 | * Schedules resume of a specified [continuation] after a specified delay [time]. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 25 | */ |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 26 | fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Delays coroutine for a given time without blocking a thread and resumes it after a specified time. |
| 31 | * This suspending function is cancellable. |
| 32 | * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| 33 | * immediately resumes with [CancellationException]. |
| 34 | * |
| 35 | * This function delegates to [Delay] implementation of the context [CoroutineDispatcher] if possible, |
| 36 | * otherwise it resumes using a built-in single-threaded scheduled executor service. |
| 37 | */ |
| 38 | suspend fun delay(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) { |
| 39 | require(time >= 0) { "Delay time $time cannot be negative" } |
| 40 | if (time <= 0) return // don't delay |
| 41 | return suspendCancellableCoroutine sc@ { cont: CancellableContinuation<Unit> -> |
| 42 | (cont.context[ContinuationInterceptor] as? Delay)?.apply { |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 43 | scheduleResumeAfterDelay(time, unit, cont) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 44 | return@sc |
| 45 | } |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 46 | scheduledExecutor.scheduleResumeAfterDelay(time, unit, cont) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 47 | } |
| 48 | } |