Skip to content

Commit 4747f3a

Browse files
committed
MPP: Window.awaitAnimationFrame extension
1 parent a919827 commit 4747f3a

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
17+
package org.w3c.performance
18+
19+
public abstract class Performance {
20+
abstract fun now(): Double
21+
}

js/js-stub/src/main/kotlin/Window.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
17+
package org.w3c.dom
18+
19+
public abstract class Window
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
17+
package kotlinx.coroutines.experimental
18+
19+
import org.w3c.dom.Window
20+
21+
/**
22+
* Suspends coroutine until next JS animation frame and returns frame time on resumption.
23+
* The time is consistent with [window.performance.now()][org.w3c.performance.Performance.now].
24+
* This function is cancellable. If the [Job] of the current coroutine is completed while this suspending
25+
* function is waiting, this function immediately resumes with [CancellationException].
26+
*/
27+
public suspend fun Window.awaitAnimationFrame(): Double = suspendCancellableCoroutine { cont ->
28+
val handle = requestAnimationFrame { timestamp ->
29+
with(cont) { DefaultDispatcher.resumeUndispatched(timestamp) }
30+
}
31+
cont.invokeOnCompletion {
32+
cancelAnimationFrame(handle)
33+
}
34+
}

0 commit comments

Comments
 (0)