Roman Elizarov | 4747f3a | 2017-12-27 13:15:11 +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 | 4747f3a | 2017-12-27 13:15:11 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental |
| 6 | |
| 7 | import org.w3c.dom.Window |
| 8 | |
| 9 | /** |
Roman Elizarov | 1867f6d | 2018-01-13 11:20:32 +0300 | [diff] [blame] | 10 | * Converts an instance of [Window] to an implementation of [CoroutineDispatcher]. |
| 11 | */ |
| 12 | public fun Window.asCoroutineDispatcher(): CoroutineDispatcher = |
| 13 | @Suppress("UnsafeCastFromDynamic") |
| 14 | asDynamic().coroutineDispatcher ?: WindowDispatcher(this).also { |
| 15 | asDynamic().coroutineDispatcher = it |
| 16 | } |
| 17 | |
| 18 | /** |
Roman Elizarov | 4747f3a | 2017-12-27 13:15:11 +0300 | [diff] [blame] | 19 | * Suspends coroutine until next JS animation frame and returns frame time on resumption. |
| 20 | * The time is consistent with [window.performance.now()][org.w3c.performance.Performance.now]. |
| 21 | * This function is cancellable. If the [Job] of the current coroutine is completed while this suspending |
| 22 | * function is waiting, this function immediately resumes with [CancellationException]. |
| 23 | */ |
| 24 | public suspend fun Window.awaitAnimationFrame(): Double = suspendCancellableCoroutine { cont -> |
Roman Elizarov | 1867f6d | 2018-01-13 11:20:32 +0300 | [diff] [blame] | 25 | asWindowAnimationQueue().enqueue(cont) |
Roman Elizarov | 4747f3a | 2017-12-27 13:15:11 +0300 | [diff] [blame] | 26 | } |
Roman Elizarov | 1867f6d | 2018-01-13 11:20:32 +0300 | [diff] [blame] | 27 | |
| 28 | private fun Window.asWindowAnimationQueue(): WindowAnimationQueue = |
| 29 | @Suppress("UnsafeCastFromDynamic") |
| 30 | asDynamic().coroutineAnimationQueue ?: WindowAnimationQueue(this).also { |
| 31 | asDynamic().coroutineAnimationQueue = it |
| 32 | } |
| 33 | |
| 34 | private class WindowAnimationQueue(private val window: Window) { |
| 35 | private val dispatcher = window.asCoroutineDispatcher() |
| 36 | private var scheduled = false |
| 37 | private var current = Queue<CancellableContinuation<Double>>() |
| 38 | private var next = Queue<CancellableContinuation<Double>>() |
| 39 | private var timestamp = 0.0 |
| 40 | |
| 41 | fun enqueue(cont: CancellableContinuation<Double>) { |
| 42 | next.add(cont) |
| 43 | if (!scheduled) { |
| 44 | scheduled = true |
| 45 | window.requestAnimationFrame { ts -> |
| 46 | timestamp = ts |
| 47 | val prev = current |
| 48 | current = next |
| 49 | next = prev |
| 50 | scheduled = false |
| 51 | process() |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fun process() { |
| 57 | while(true) { |
| 58 | val element = current.poll() ?: return |
| 59 | with(element) { dispatcher.resumeUndispatched(timestamp) } |
| 60 | } |
| 61 | } |
| 62 | } |