Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental.swing |
| 18 | |
| 19 | import kotlinx.coroutines.experimental.CancellableContinuation |
| 20 | import kotlinx.coroutines.experimental.CoroutineDispatcher |
| 21 | import kotlinx.coroutines.experimental.Delay |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 22 | import kotlinx.coroutines.experimental.DisposableHandle |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 23 | import kotlinx.coroutines.experimental.swing.Swing.delay |
| 24 | import java.awt.event.ActionListener |
| 25 | import java.util.concurrent.TimeUnit |
| 26 | import javax.swing.SwingUtilities |
| 27 | import javax.swing.Timer |
Roman Elizarov | ea4a51b | 2017-01-31 12:01:25 +0300 | [diff] [blame] | 28 | import kotlin.coroutines.experimental.CoroutineContext |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Dispatches execution onto Swing event dispatching thread and provides native [delay] support. |
| 32 | */ |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 33 | object Swing : CoroutineDispatcher(), Delay { |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 34 | override fun dispatch(context: CoroutineContext, block: Runnable) = SwingUtilities.invokeLater(block) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 35 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 36 | override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) { |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 37 | val timer = schedule(time, unit, ActionListener { |
Roman Elizarov | a198bad | 2017-02-10 13:30:38 +0300 | [diff] [blame] | 38 | with(continuation) { resumeUndispatched(Unit) } |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 39 | }) |
Vsevolod Tolstopyatov | 80a2947 | 2018-04-17 16:02:02 +0300 | [diff] [blame] | 40 | continuation.invokeOnCancellation { timer.stop() } |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | override fun invokeOnTimeout(time: Long, unit: TimeUnit, block: Runnable): DisposableHandle { |
| 44 | val timer = schedule(time, unit, ActionListener { |
| 45 | block.run() |
| 46 | }) |
| 47 | return object : DisposableHandle { |
| 48 | override fun dispose() { |
| 49 | timer.stop() |
| 50 | } |
Roman Elizarov | a198bad | 2017-02-10 13:30:38 +0300 | [diff] [blame] | 51 | } |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private fun schedule(time: Long, unit: TimeUnit, action: ActionListener): Timer = |
| 55 | Timer(unit.toMillis(time).coerceAtMost(Int.MAX_VALUE.toLong()).toInt(), action).apply { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 56 | isRepeats = false |
| 57 | start() |
| 58 | } |
Roman Elizarov | a198bad | 2017-02-10 13:30:38 +0300 | [diff] [blame] | 59 | |
| 60 | override fun toString() = "Swing" |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 61 | } |