跳转到主要内容
版本:v2.2.0-0.13.1

Kotlin 挂起函数转换编译器插件

Maven Central Gradle Plugin Portal

cover

概述

用于为挂起函数生成平台兼容函数的 Kotlin 编译器插件。

快速示例

JVM 平台

class Foo {
@JvmBlocking
@JvmAsync
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}

插件会自动生成:

class Foo {
@JvmSynthetic
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}

@Api4J
fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() }

@Api4J
fun waitAndGetAsync(): CompletableFuture<out String> = runInAsync { waitAndGet() }
}

JavaScript 平台

class Foo {
@JsPromise
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}

插件会自动生成:

class Foo {
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}

@Api4Js
fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() }
}

平台支持状态

平台起始版本
JVM0.1.0
JavaScript0.6.0
WasmJS (实验性)0.6.0
备注

JS 平台支持在版本 0.6.0 中添加。查看实现详情请参考 KT-53993PR #39