Kotlin 挂起函数转换编译器插件
概述
用于为挂起函数生成平台兼容函数的 Kotlin 编译器插件。
快速示例
JVM 平台
class Foo {
@JvmBlocking
@JvmAsync
@JvmReactive
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() }
@Api4J
fun waitAndGetReactive(): Publisher<String> = runInReactive { waitAndGet() }
}
@JvmReactive 返回 Reactive Streams Publisher;当挂起函数返回 null 时空完成。
需要显式启用 addJvmReactive(),并由用户 JVM classpath 自行添加 reactive 依赖。
详见 默认转换器。
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() }
}
平台支持状态
| 平台 | 起始版本 |
|---|---|
| JVM | 0.1.0 |
| JavaScript | 0.6.0 |
| WasmJS (实验性) | 0.6.0 |