Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.javafx |
| 2 | |
| 3 | import javafx.animation.AnimationTimer |
| 4 | import javafx.animation.KeyFrame |
| 5 | import javafx.animation.Timeline |
| 6 | import javafx.application.Platform |
| 7 | import javafx.event.ActionEvent |
| 8 | import javafx.event.EventHandler |
| 9 | import javafx.util.Duration |
| 10 | import kotlinx.coroutines.experimental.* |
| 11 | import kotlinx.coroutines.experimental.javafx.JavaFx.delay |
| 12 | import java.util.concurrent.CopyOnWriteArrayList |
| 13 | import java.util.concurrent.TimeUnit |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 14 | import kotlin.coroutines.CoroutineContext |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 15 | |
| 16 | |
| 17 | /** |
| 18 | * Dispatches execution onto JavaFx application thread and provides native [delay] support. |
| 19 | */ |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 20 | object JavaFx : CoroutineDispatcher(), Yield, Delay { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 21 | private val pulseTimer by lazy { |
| 22 | PulseTimer().apply { start() } |
| 23 | } |
| 24 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 25 | override fun isDispatchNeeded(context: CoroutineContext): Boolean = !Platform.isFxApplicationThread() |
| 26 | override fun dispatch(context: CoroutineContext, block: Runnable) = Platform.runLater(block) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Suspends coroutine until next JavaFx pulse and returns time of the pulse on resumption. |
| 30 | * If the [Job] of the current coroutine is completed while this suspending function is waiting, this function |
| 31 | * immediately resumes with [CancellationException] . |
| 32 | */ |
| 33 | suspend fun awaitPulse(): Long = suspendCancellableCoroutine { cont -> |
| 34 | pulseTimer.onNext(cont) |
| 35 | } |
| 36 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 37 | override fun scheduleResume(continuation: CancellableContinuation<Unit>) { |
| 38 | Platform.runLater { continuation.resume(Unit) } |
| 39 | } |
| 40 | |
| 41 | override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 42 | val timeline = Timeline(KeyFrame(Duration.millis(unit.toMillis(time).toDouble()), |
| 43 | EventHandler<ActionEvent> { continuation.resume(Unit) })) |
| 44 | timeline.play() |
| 45 | continuation.onCompletion { timeline.stop() } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private class PulseTimer : AnimationTimer() { |
| 50 | val next = CopyOnWriteArrayList<CancellableContinuation<Long>>() |
| 51 | |
| 52 | override fun handle(now: Long) { |
| 53 | val cur = next.toTypedArray() |
| 54 | next.clear() |
| 55 | for (cont in cur) |
| 56 | cont.resume(now) |
| 57 | } |
| 58 | |
| 59 | fun onNext(cont: CancellableContinuation<Long>) { |
| 60 | next += cont |
| 61 | } |
| 62 | } |