Skip to content

Commit

Permalink
v4.0.0-dev7
Browse files Browse the repository at this point in the history
增加 process 事件注册扩展
  • Loading branch information
ForteScarlet committed Jan 18, 2024
1 parent 9cd254c commit 50337d7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
Empty file added .changelog/v4.0.0-dev7.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-dev7
> [!warning]
> 这是一个尚在开发中的**预览版**,它可能不稳定,可能会频繁变更,且没有可用性保证。

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

# v4.0.0-dev6
> [!warning]
> 这是一个尚在开发中的**预览版**,它可能不稳定,可能会频繁变更,且没有可用性保证。
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/P.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ sealed class P(override val group: String) : ProjectDetail() {
val versionWithoutSnapshot: Version

init {
val mainVersion = version(4, 0, 0) - version("dev6")
val mainVersion = version(4, 0, 0) - version("dev7")

fun initVersionWithoutSnapshot(status: Version?): Version = if (status == null) {
mainVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public interface EventListenerRegistrar {
* @param E The type of event to register the listener for.
* @param propertiesConsumer An optional function that consumes the configuration properties of the listener registration.
* The function should have the signature `ConfigurerFunction<EventListenerRegistrationProperties>`.
* @param defaultResult The default result function that will be invoked if the event is not of type `E`.
* @param typeMismatchResult The default result function that will be invoked if the event is not of type `E`.
* The function should have the signature `EventContext.() -> EventResult`.
* By default, it returns an invalid result.
* @param listenerFunction The listener function that will be invoked when the event is fired.
Expand All @@ -80,14 +80,60 @@ public interface EventListenerRegistrar {
*/
public inline fun <reified E : Event> EventListenerRegistrar.listen(
propertiesConsumer: ConfigurerFunction<EventListenerRegistrationProperties>? = null,
crossinline defaultResult: EventListenerContext.() -> EventResult = { invalid() },
crossinline typeMismatchResult: EventListenerContext.() -> EventResult = { invalid() },
crossinline listenerFunction: suspend EventListenerContext.(E) -> EventResult,
) {
register(
propertiesConsumer = propertiesConsumer,
listener = {
val event = this.event
if (event is E) listenerFunction(this, event) else defaultResult(this)
if (event is E) listenerFunction(this, event) else typeMismatchResult(this)
})
}

/**
* 是 [listen] 或 [EventListenerRegistrar.register] 的进一步简写形式,
* 注册一个事件处理器。
* 通过 [process] 注册的事件处理器函数不会要求你返回 [EventResult],
* 取而代之的是始终返回默认的 [defaultResult],默认为 [EventResult.empty]。
*
*/
public inline fun EventListenerRegistrar.process(
crossinline defaultResult: () -> EventResult = { EventResult.empty() },
propertiesConsumer: ConfigurerFunction<EventListenerRegistrationProperties>? = null,
crossinline listenerFunction: suspend EventListenerContext.() -> Unit,
) {
register(
propertiesConsumer = propertiesConsumer,
listener = {
listenerFunction()
defaultResult()
})
}

/**
* 是 [listen] 或 [EventListenerRegistrar.register] 的进一步简写形式,
* 注册一个处理特定类型 [E] 的事件处理器。
* 通过 [process] 注册的事件处理器函数不会要求你返回 [EventResult],
* 取而代之的是始终返回默认的 [defaultResult],默认为 [EventResult.empty]。
*
* @param defaultResult 事件处理后的默认返回值
* @param typeMismatchResult 事件类型与 [E] 不匹配时的默认返回值
*/
public inline fun <reified E : Event> EventListenerRegistrar.process(
crossinline defaultResult: () -> EventResult = { EventResult.empty() },
propertiesConsumer: ConfigurerFunction<EventListenerRegistrationProperties>? = null,
crossinline typeMismatchResult: EventListenerContext.() -> EventResult = { invalid() },
crossinline listenerFunction: suspend EventListenerContext.(E) -> Unit,
) {
register(
propertiesConsumer = propertiesConsumer,
listener = {
val event = this.event
if (event is E) {
listenerFunction(event)
defaultResult()
} else typeMismatchResult()
})
}

Expand Down

0 comments on commit 50337d7

Please sign in to comment.