Skip to content

Commit

Permalink
v4.0.0-dev13
Browse files Browse the repository at this point in the history
桥接函数的 Reserve API 部分位置同样调整 baseName; 调整 Spring starter 中部分命名、以及部分修复
  • Loading branch information
ForteScarlet committed Jan 21, 2024
1 parent bb3db1a commit 6f1925d
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 38 deletions.
Empty file added .changelog/v4.0.0-dev13.md
Empty file.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# v4.0.0-dev13
> [!warning]
> 这是一个尚在开发中的**预览版**,它可能不稳定,可能会频繁变更,且没有可用性保证。

> Release & Pull Notes: [v4.0.0-dev13](https://github.com/simple-robot/simpler-robot/releases/tag/v4.0.0-dev13)

# v4.0.0-dev12
> [!warning]
> 这是一个尚在开发中的**预览版**,它可能不稳定,可能会频繁变更,且没有可用性保证。
Expand Down
6 changes: 3 additions & 3 deletions simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public interface GuildRelation {
*
* 如果实现者不支持也可能始终得到 `null`。
*/
@ST(blockingBaseName = "getGuild", blockingSuffix = "", asyncBaseName = "getGuild")
@ST(blockingBaseName = "getGuild", blockingSuffix = "", asyncBaseName = "getGuild", reserveBaseName = "getGuild")
public suspend fun guild(id: ID): Guild?

/**
Expand Down Expand Up @@ -214,7 +214,7 @@ public interface GroupRelation {
*
* 如果实现者不支持也可能始终得到 `null`。
*/
@ST(blockingBaseName = "getGroup", blockingSuffix = "", asyncBaseName = "getGroup")
@ST(blockingBaseName = "getGroup", blockingSuffix = "", asyncBaseName = "getGroup", reserveBaseName = "getGroup")
public suspend fun group(id: ID): ChatGroup?

/**
Expand Down Expand Up @@ -247,7 +247,7 @@ public interface ContactRelation {
*
* 如果实现者不支持也可能始终得到 `null`。
*/
@ST(blockingBaseName = "getContact", blockingSuffix = "", asyncBaseName = "getContact")
@ST(blockingBaseName = "getContact", blockingSuffix = "", asyncBaseName = "getContact", reserveBaseName = "getContact")
public suspend fun contact(id: ID): Contact?

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,24 @@ internal class ProviderComponentFactory<COM : Component, CONF : Any>(
* 添加一个用于获取 [ComponentFactoryProvider] 的函数。
* 这是用于兼容在非 `JVM` 平台下没有 `ServiceLoader` 的方案,
* 在 `JVM` 中应直接使用 `ServiceLoader` 加载 SPI 的方式
* 但是如果强行使用 [addProvider] 添加结果,
* 但是如果强行使用 [addComponentFactoryProvider] 添加结果,
* [loadComponentProviders] 也还是会得到这些结果的。
*/
public fun addProvider(providerCreator: () -> ComponentFactoryProvider<*>) {
public fun addComponentFactoryProvider(providerCreator: () -> ComponentFactoryProvider<*>) {
Services.addProvider<ComponentFactoryProvider<*>>(providerCreator)
}

/**
* 清理所有通过 [addProvider] 添加的 provider 构建器。
* 清理所有通过 [addComponentFactoryProvider] 添加的 provider 构建器。
*/
public fun clearProviders() {
public fun clearComponentFactoryProviders() {
Services.clearProviders<ComponentFactoryProvider<*>>()
}

/**
* 尝试自动加载环境中可获取的所有 [ComponentFactoryProvider] 实例。
* 在 `JVM` 平台下通过 `ServiceLoader` 加载 [ComponentFactoryProvider] 并得到结果,
* 而在其他平台则会得到预先从 [addProvider] 中添加的所有函数构建出来的结果。
* 而在其他平台则会得到预先从 [addComponentFactoryProvider] 中添加的所有函数构建出来的结果。
*
*/
public expect fun loadComponentProviders(): Sequence<ComponentFactoryProvider<*>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public interface Guild : Organization {
* 根据ID获取一个指定的频道。
* 如果找不到则会得到 `null`。
*/
@ST(blockingBaseName = "getChannel", blockingSuffix = "", asyncBaseName = "getChannel")
@ST(blockingBaseName = "getChannel", blockingSuffix = "", asyncBaseName = "getChannel", reserveBaseName = "getChannel")
public suspend fun channel(id: ID): Channel?

/**
Expand All @@ -104,7 +104,7 @@ public interface Guild : Organization {
* 根据ID获取一个指定的聊天频道。
* 如果找不到则会得到 `null`。
*/
@ST(blockingBaseName = "getChatChannel", blockingSuffix = "", asyncBaseName = "getChatChannel")
@ST(blockingBaseName = "getChatChannel", blockingSuffix = "", asyncBaseName = "getChatChannel", reserveBaseName = "getChatChannel")
public suspend fun chatChannel(id: ID): ChatChannel?

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public interface Organization : Actor {
*
* @throws Exception 可能产生任何异常
*/
@ST(blockingBaseName = "getMember", blockingSuffix = "", asyncBaseName = "getMember")
@ST(blockingBaseName = "getMember", blockingSuffix = "", asyncBaseName = "getMember", reserveBaseName = "getMember")
public suspend fun member(id: ID): Member?

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package love.forte.simbot.plugin
import love.forte.simbot.common.function.ConfigurerFunction
import love.forte.simbot.common.function.invokeWith
import love.forte.simbot.common.services.Services
import love.forte.simbot.component.addProvider
import love.forte.simbot.component.addComponentFactoryProvider
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

Expand Down Expand Up @@ -93,24 +93,24 @@ public interface PluginFactoryConfigurerProvider<CONF : Any> {
* 添加一个用于获取 [PluginFactoryProvider] 的函数。
* 这是用于兼容在非 `JVM` 平台下没有 `ServiceLoader` 的方案,
* 在 `JVM` 中应直接使用 `ServiceLoader` 加载 SPI 的方式,
* 但是如果使用 [addProvider] 强行添加结果,[loadPluginProviders]
* 但是如果使用 [addComponentFactoryProvider] 强行添加结果,[loadPluginProviders]
* 也还是会得到这些结果的。
*/
public fun addProvider(providerCreator: () -> PluginFactoryProvider<*>) {
public fun addPluginFactoryProvider(providerCreator: () -> PluginFactoryProvider<*>) {
Services.addProvider<PluginFactoryProvider<*>>(providerCreator)
}

/**
* 清理所有通过 [addProvider] 添加的 provider 构建器。
* 清理所有通过 [addComponentFactoryProvider] 添加的 provider 构建器。
*/
public fun clearProviders() {
public fun clearPluginFactoryProviders() {
Services.clearProviders<PluginFactoryProvider<*>>()
}

/**
* 尝试自动加载环境中可获取的所有 [PluginFactoryProvider] 实例。
* 在 `JVM` 平台下通过 `ServiceLoader` 加载 [PluginFactoryProvider] 并得到结果,
* 而在其他平台则会得到预先从 [addProvider] 中添加的所有函数构建出来的结果。
* 而在其他平台则会得到预先从 [addComponentFactoryProvider] 中添加的所有函数构建出来的结果。
*/
public expect fun loadPluginProviders(): Sequence<PluginFactoryProvider<*>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ class ComponentFactoryProviderTests {

@Test
fun addAndGet() {
addProvider { FacP() }
addProvider { FacP() }
addProvider { FacP() }
addComponentFactoryProvider { FacP() }
addComponentFactoryProvider { FacP() }
addComponentFactoryProvider { FacP() }

val list = loadComponentProviders().toList()
assertEquals(3, list.size)

clearProviders()
clearComponentFactoryProviders()
}

@Test
Expand All @@ -93,25 +93,25 @@ class ComponentFactoryProviderTests {
withContext(Dispatchers.Default) {
launch {
repeat(100) {
addProvider { FacP() }
addComponentFactoryProvider { FacP() }
}
}
launch {
repeat(100) {
addProvider { FacP() }
addComponentFactoryProvider { FacP() }
}
}
launch {
repeat(100) {
addProvider { FacP() }
addComponentFactoryProvider { FacP() }
}
}
}
}

val list = loadComponentProviders().toList()
assertEquals(300, list.size)
clearProviders()
clearComponentFactoryProviders()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package love.forte.simbot.component
import love.forte.simbot.common.services.Services

/**
* 获取通过 [addProvider] 添加的内容的副本序列。
* 获取通过 [addComponentFactoryProvider] 添加的内容的副本序列。
*/
public actual fun loadComponentProviders(): Sequence<ComponentFactoryProvider<*>> =
Services.loadProviders<ComponentFactoryProvider<*>>().map { it() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
package love.forte.simbot.plugin

import love.forte.simbot.common.services.Services
import love.forte.simbot.component.addProvider
import love.forte.simbot.component.addComponentFactoryProvider

/**
* 加载所有通过 [addProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。
* 加载所有通过 [addComponentFactoryProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。
*/
public actual fun loadPluginProviders(): Sequence<PluginFactoryProvider<*>> =
Services.loadProviders<PluginFactoryProvider<*>>().map { it() }
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package love.forte.simbot.component
import love.forte.simbot.common.services.Services

/**
* 获取通过 [addProvider] 添加的内容的副本序列。
* 获取通过 [addComponentFactoryProvider] 添加的内容的副本序列。
*/
public actual fun loadComponentProviders(): Sequence<ComponentFactoryProvider<*>> =
Services.loadProviders<ComponentFactoryProvider<*>>().map { it() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
package love.forte.simbot.plugin

import love.forte.simbot.common.services.Services
import love.forte.simbot.component.addProvider
import love.forte.simbot.component.addComponentFactoryProvider

/**
* 加载所有通过 [addProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。
* 加载所有通过 [addComponentFactoryProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。
*/
public actual fun loadPluginProviders(): Sequence<PluginFactoryProvider<*>> =
Services.loadProviders<PluginFactoryProvider<*>>().map { it() }
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal actual fun <T : Any> loadProvidersInternal(type: KClass<T>): Sequence<(
return synchronized(lock) {
globalProviderCreators[type]?.toList()
?.asSequence()
?.map { provider -> { type.cast(provider) } }
?.map { provider -> { type.cast(provider()) } }
?: emptySequence()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,48 @@ import love.forte.simbot.spring.configuration.config.DefaultSimbotApplicationCon
import love.forte.simbot.spring.configuration.listener.SimbotEventListenerFunctionProcessor
import org.springframework.context.annotation.Import


/**
* 启用 simbot 的各项配置。
*
* 将其标记在你的启动类或某个配置类上。
*
* Kotlin
*
* ```kotlin
* @EnableSimbot
* @SpringBootApplication
* open class MyApplication {
* // ...
* }
* ```
*
* ```kotlin
* @EnableSimbot
* @Configuration
* open class MyConfig {
* // ...
* }
* ```
*
* Java
*
* ```java
* @EnableSimbot
* @SpringBootApplication
* public class MyApplication {
* // ...
* }
* ```
*
* ```java
* @EnableSimbot
* @Configuration
* public class MyConfig {
* // ...
* }
* ```
*
*/
@Target(AnnotationTarget.CLASS)
@Import(
SimbotSpringPropertiesConfiguration::class,
Expand All @@ -40,7 +81,6 @@ import org.springframework.context.annotation.Import
DefaultSimbotDispatcherProcessorConfiguration::class,
DefaultSimbotComponentInstallProcessorConfiguration::class,
DefaultSimbotPluginInstallProcessorConfiguration::class,
DefaultSimbotSpringApplicationLauncherFactoryConfiguration::class,
DefaultSimbotEventDispatcherProcessorConfiguration::class,
DefaultBinderManagerProvidersConfiguration::class,
// listeners directly
Expand All @@ -56,6 +96,5 @@ import org.springframework.context.annotation.Import
// post listeners
SimbotEventListenerFunctionProcessor::class,
SimbotApplicationRunner::class

)
public annotation class EnableSimbot
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import love.forte.simbot.spring.common.BotAutoStartOnFailureException
import love.forte.simbot.spring.common.BotConfigResourceLoadOnFailureException
import love.forte.simbot.spring.common.MismatchConfigurableBotManagerException
import love.forte.simbot.spring.common.application.*
import love.forte.simbot.spring.configuration.SimbotEventDispatcherProcessor
import love.forte.simbot.spring.configuration.listener.SimbotEventListenerResolver
import love.forte.simbot.suspendrunner.runInNoScopeBlocking
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -63,13 +64,15 @@ public open class DefaultSimbotSpringApplicationProcessorConfiguration {
properties: SpringApplicationConfigurationProperties,
@Autowired(required = false) eventListenerResolvers: List<SimbotEventListenerResolver>? = null,
@Autowired(required = false) preConfigurer: List<SimbotApplicationPreConfigurer>? = null,
@Autowired(required = false) postConfigurer: List<SimbotApplicationPostConfigurer>? = null
@Autowired(required = false) postConfigurer: List<SimbotApplicationPostConfigurer>? = null,
@Autowired(required = false) eventDispatcherProcessor: SimbotEventDispatcherProcessor,
): DefaultSimbotApplicationProcessor =
DefaultSimbotApplicationProcessor(
properties = properties,
eventListenerResolvers = eventListenerResolvers ?: emptyList(),
preConfigurer = preConfigurer ?: emptyList(),
postConfigurer = postConfigurer ?: emptyList(),
eventDispatcherProcessor
)

public companion object {
Expand All @@ -91,7 +94,8 @@ public class DefaultSimbotApplicationProcessor(
private val properties: SpringApplicationConfigurationProperties,
private val eventListenerResolvers: List<SimbotEventListenerResolver>,
private val preConfigurer: List<SimbotApplicationPreConfigurer>,
private val postConfigurer: List<SimbotApplicationPostConfigurer>
private val postConfigurer: List<SimbotApplicationPostConfigurer>,
private val eventDispatcherProcessor: SimbotEventDispatcherProcessor,
) : SimbotApplicationProcessor {
override fun process(application: SpringApplication) {
preConfigurer.forEach {
Expand All @@ -114,8 +118,8 @@ public class DefaultSimbotApplicationProcessor(


private fun process0(application: SpringApplication) {
eventDispatcherProcessor.process(application.eventDispatcher)
loadBots(application)

}

private fun loadBots(application: SpringApplication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import kotlin.reflect.KFunction

/**
* 用于通过 [Application] 注册 [EventListener] 的函数接口。
* 主要是用于通过 [SimbotEventListenerFunctionProcessor] 生成。
*/
public fun interface SimbotEventListenerResolver {
public fun resolve(application: Application)
Expand Down

0 comments on commit 6f1925d

Please sign in to comment.