Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.swing |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.CancellableContinuation |
| 8 | import kotlinx.coroutines.experimental.CoroutineDispatcher |
| 9 | import kotlinx.coroutines.experimental.Delay |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 10 | import kotlinx.coroutines.experimental.DisposableHandle |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 11 | import kotlinx.coroutines.experimental.swing.Swing.delay |
| 12 | import java.awt.event.ActionListener |
| 13 | import java.util.concurrent.TimeUnit |
| 14 | import javax.swing.SwingUtilities |
| 15 | import javax.swing.Timer |
Roman Elizarov | ea4a51b | 2017-01-31 12:01:25 +0300 | [diff] [blame] | 16 | import kotlin.coroutines.experimental.CoroutineContext |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Dispatches execution onto Swing event dispatching thread and provides native [delay] support. |
| 20 | */ |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 21 | object Swing : CoroutineDispatcher(), Delay { |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 22 | override fun dispatch(context: CoroutineContext, block: Runnable) = SwingUtilities.invokeLater(block) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 23 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 24 | override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) { |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 25 | val timer = schedule(time, unit, ActionListener { |
Roman Elizarov | a198bad | 2017-02-10 13:30:38 +0300 | [diff] [blame] | 26 | with(continuation) { resumeUndispatched(Unit) } |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 27 | }) |
Vsevolod Tolstopyatov | 80a2947 | 2018-04-17 16:02:02 +0300 | [diff] [blame] | 28 | continuation.invokeOnCancellation { timer.stop() } |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | override fun invokeOnTimeout(time: Long, unit: TimeUnit, block: Runnable): DisposableHandle { |
| 32 | val timer = schedule(time, unit, ActionListener { |
| 33 | block.run() |
| 34 | }) |
| 35 | return object : DisposableHandle { |
| 36 | override fun dispose() { |
| 37 | timer.stop() |
| 38 | } |
Roman Elizarov | a198bad | 2017-02-10 13:30:38 +0300 | [diff] [blame] | 39 | } |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | private fun schedule(time: Long, unit: TimeUnit, action: ActionListener): Timer = |
| 43 | Timer(unit.toMillis(time).coerceAtMost(Int.MAX_VALUE.toLong()).toInt(), action).apply { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 44 | isRepeats = false |
| 45 | start() |
| 46 | } |
Roman Elizarov | a198bad | 2017-02-10 13:30:38 +0300 | [diff] [blame] | 47 | |
| 48 | override fun toString() = "Swing" |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 49 | } |