Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applications 增加部分Java友好API #535

Merged
merged 4 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ changelog:
- 'for-release'
- 'ignore-in-release'
categories:
- title: '⚠️ 不兼容变更'
- title: '⚠️ 不兼容'
labels:
- '不兼容'
- title: '🔧 Bug Fix'
- title: '🔧 修复'
labels:
- 'bug'
- '缺陷'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ package love.forte.simbot.application

import love.forte.plugin.suspendtrans.annotation.JvmAsync
import love.forte.plugin.suspendtrans.annotation.JvmBlocking
import love.forte.simbot.Api4J
import love.forte.simbot.utils.runInNoScopeBlocking
import java.util.function.BiConsumer
import java.util.function.Consumer


/**
Expand Down Expand Up @@ -61,6 +65,27 @@ public suspend fun <Config : ApplicationConfiguration, Builder : ApplicationBuil
return factory.create(configurator, builder)
}

/**
* 构建并启用一个 [Application].
*
* [createSimbotApplication] 的非挂起兼容API, 并使用Java友好的函数接口代替Kotlin函数类型。
*
* @see createSimbotApplication
*/
@JvmOverloads
@JvmName("createSimbotApplication")
public fun <Config : ApplicationConfiguration, Builder : ApplicationBuilder<A>, A : Application> createSimbotApplication4J(
factory: ApplicationFactory<Config, Builder, A>,
configurator: Consumer<Config>? = null,
builder: BiConsumer<Builder, Config>? = null,
): A {
return runInNoScopeBlocking {
createSimbotApplication(factory, { configurator?.accept(this) }) { config ->
builder?.accept(this, config)
}
}
}


/**
* 构建一个 [ApplicationLauncher].
Expand Down Expand Up @@ -95,6 +120,7 @@ public suspend fun <Config : ApplicationConfiguration, Builder : ApplicationBuil
* ```
*
*/
@JvmSynthetic
public fun <Config : ApplicationConfiguration, Builder : ApplicationBuilder<A>, A : Application> simbotApplication(
factory: ApplicationFactory<Config, Builder, A>,
configurator: Config.() -> Unit = {},
Expand All @@ -104,6 +130,29 @@ public fun <Config : ApplicationConfiguration, Builder : ApplicationBuilder<A>,
}


/**
* 构建一个 [ApplicationLauncher].
*
* [simbotApplication] 的非挂起兼容API, 并使用Java友好的函数接口代替Kotlin函数类型。
*
* @see simbotApplication
*
*/
@JvmOverloads
@JvmName("simbotApplication")
public fun <Config : ApplicationConfiguration, Builder : ApplicationBuilder<A>, A : Application> simbotApplication4J(
factory: ApplicationFactory<Config, Builder, A>,
configurator: Consumer<Config>? = null,
builder: BiConsumer<Builder, Config>? = null,
): ApplicationLauncher<A> {
return applicationLauncher {
factory.create({ configurator?.accept(this) }) { config ->
builder?.accept(this, config)
}
}
}


/**
* 通过 [ApplicationDslBuilder] 来使用DSL风格或链式调用风格构建目标 [Application][A].
*
Expand Down Expand Up @@ -206,7 +255,7 @@ public annotation class ApplicationDslBuilderDsl
* ```
*/
public interface ApplicationDslBuilder<Config : ApplicationConfiguration, Builder : ApplicationBuilder<A>, A : Application> {

/**
* 提供配置函数。
*
Expand All @@ -220,8 +269,8 @@ public interface ApplicationDslBuilder<Config : ApplicationConfiguration, Builde
*
*/
@ApplicationDslBuilderDsl
public fun config(config: Config.() -> Unit): ApplicationDslBuilder<Config, Builder, A>
public fun config(config: ConfigFunction<Config>): ApplicationDslBuilder<Config, Builder, A>

/**
* 提供构建函数。
*
Expand All @@ -232,16 +281,38 @@ public interface ApplicationDslBuilder<Config : ApplicationConfiguration, Builde
* ```
*/
@ApplicationDslBuilderDsl
@JvmSynthetic
public fun build(builder: suspend Builder.(Config) -> Unit): ApplicationDslBuilder<Config, Builder, A>



/**
* 提供构建函数。
*
* ```java
* builder.build((builder0, config) -> {});
* ```
*/
@Api4J
public fun build(builder: BiConsumer<Builder, Config>): ApplicationDslBuilder<Config, Builder, A> {
return build { c ->
builder.accept(this, c)
}
}


/**
* 根据配置的函数构建目标 [Application][A].
*/
@JvmBlocking
@JvmAsync
public suspend fun create(): A



/**
* 用于 [build] 兼容 Kotlin 和 Java.
*/
public fun interface ConfigFunction<T> {
public operator fun T.invoke()
}
}


Expand All @@ -250,24 +321,24 @@ private class ApplicationDslBuilderImpl<Config : ApplicationConfiguration, Build
) : ApplicationDslBuilder<Config, Builder, A> {
private val configs = mutableListOf<Config.() -> Unit>()
private val builders = mutableListOf<suspend Builder.(Config) -> Unit>()
override fun config(config: Config.() -> Unit): ApplicationDslBuilder<Config, Builder, A> =

override fun config(config: ApplicationDslBuilder.ConfigFunction<Config>): ApplicationDslBuilder<Config, Builder, A> =
also {
configs.add(config)
configs.add { config.apply { invoke() } }
}

override fun build(builder: suspend Builder.(Config) -> Unit): ApplicationDslBuilder<Config, Builder, A> =
also {
builders.add(builder)
}

override suspend fun create(): A = createSimbotApplication(
factory = factory,
{ configs.forEach { conf -> this.conf() } }
) { c ->
builders.forEach { builder -> this.builder(c) }
}

}


Expand Down

This file was deleted.