Skip to content

Commit dc5cf81

Browse files
authored
Merge pull request #894 from simple-robot/streamable-api
增加模块与新的公共类型 Streamable, 用来简化部分针对 Sequence 类型的转化操作
2 parents a04aa48 + f08d80e commit dc5cf81

File tree

22 files changed

+552
-26
lines changed

22 files changed

+552
-26
lines changed

.github/workflows/publish-v4-release.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ env:
88
JAVA_VERSION: 21
99
JAVA_DISTRIBUTION: zulu
1010
GRADLE_VERSION: 8.5
11+
NODE_VERSION: 22
1112
IS_CI: true
1213
GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }}
1314
GPG_SECRET_KEY: ${{ secrets.GPG_SECRET_KEY }}
@@ -30,6 +31,10 @@ jobs:
3031
distribution: ${{ env.JAVA_DISTRIBUTION }}
3132
java-version: ${{ env.JAVA_VERSION }}
3233
cache: 'gradle'
34+
35+
- uses: actions/setup-node@v4
36+
with:
37+
node-version: ${{ env.NODE_VERSION }}
3338
- name: Run All Tests pre release
3439
uses: gradle/actions/setup-gradle@v3
3540
with:
@@ -56,7 +61,9 @@ jobs:
5661
distribution: ${{ env.JAVA_DISTRIBUTION }}
5762
java-version: ${{ env.JAVA_VERSION }}
5863
cache: 'gradle'
59-
64+
- uses: actions/setup-node@v4
65+
with:
66+
node-version: ${{ env.NODE_VERSION }}
6067
# setup Gradle
6168
- name: Gradle Publish Release
6269
uses: gradle/actions/setup-gradle@v3

.github/workflows/publish-v4-snapshot.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ env:
2121
JAVA_VERSION: 21
2222
JAVA_DISTRIBUTION: zulu
2323
GRADLE_VERSION: 8.5
24+
NODE_VERSION: 22
2425
IS_CI: true
2526
GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }}
2627
GPG_SECRET_KEY: ${{ secrets.GPG_SECRET_KEY }}
@@ -49,7 +50,7 @@ jobs:
4950

5051
- uses: actions/setup-node@v4
5152
with:
52-
node-version: 20
53+
node-version: ${{ env.NODE_VERSION }}
5354

5455
- name: Setup Gradle
5556
uses: gradle/actions/setup-gradle@v3
@@ -79,10 +80,9 @@ jobs:
7980
java-version: ${{ env.JAVA_VERSION }}
8081
cache: 'gradle'
8182

82-
# setup Node
8383
- uses: actions/setup-node@v4
8484
with:
85-
node-version: 20
85+
node-version: ${{ env.NODE_VERSION }}
8686

8787
- name: Publish snapshots
8888
uses: gradle/actions/setup-gradle@v3

.github/workflows/test-v4-branch.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ env:
99
JAVA_VERSION: 21
1010
JAVA_DISTRIBUTION: zulu
1111
GRADLE_VERSION: 8.5
12+
NODE_VERSION: 22
1213
IS_CI: true
1314
GRADLE_OPTS: "-Dfile.encoding=UTF-8"
1415

@@ -30,6 +31,9 @@ jobs:
3031
distribution: ${{ env.JAVA_DISTRIBUTION }}
3132
java-version: ${{ env.JAVA_VERSION }}
3233
cache: 'gradle'
34+
- uses: actions/setup-node@v4
35+
with:
36+
node-version: ${{ env.NODE_VERSION }}
3337

3438
- name: Run V4 All Tests
3539
uses: gradle/gradle-build-action@v3

build.gradle.kts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,13 @@ idea {
171171
}
172172
}
173173

174-
174+
// https://kotlinlang.org/docs/js-project-setup.html#node-js
175+
rootProject.plugins.withType<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin> {
176+
rootProject.the<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension>().apply {
177+
// CI 中配置环境,不再单独下载
178+
if (isCi) {
179+
download = false
180+
}
181+
}
182+
// "true" for default behavior
183+
}

buildSrc/src/main/kotlin/JsConfig.kt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
*/
2323

2424
import org.gradle.api.Project
25-
import org.gradle.kotlin.dsl.the
26-
import org.gradle.kotlin.dsl.withType
2725
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
2826
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinWasmJsTargetDsl
29-
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
3027

3128

3229
inline fun KotlinJsTargetDsl.configJs(
@@ -77,7 +74,8 @@ inline fun KotlinWasmJsTargetDsl.configWasmJs(
7774
block: () -> Unit = {}
7875
) {
7976
if (nodeJs) {
80-
nodejs()
77+
nodejs {
78+
}
8179
}
8280
// if (nodeJs && isLinux) {
8381
// // win in candy node `21.0.0-v8-canary202309143a48826a08` is not supported
@@ -100,18 +98,18 @@ inline fun KotlinWasmJsTargetDsl.configWasmJs(
10098
}
10199

102100
inline fun Project.configWasmJsTest(block: () -> Unit = {}) {
103-
if (false) {
104-
// see https://youtrack.jetbrains.com/issue/KT-63014/Running-tests-with-wasmJs-in-1.9.20-requires-Chrome-Canary#focus=Comments-27-8321383.0-0
105-
rootProject.the<NodeJsRootExtension>().apply {
106-
// nodeVersion = "21.0.0-v8-canary202309143a48826a08"
107-
version = "21.0.0-v8-canary202309143a48826a08"
108-
downloadBaseUrl = "https://nodejs.org/download/v8-canary"
109-
}
110-
111-
tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask>().configureEach {
112-
args.add("--ignore-engines")
113-
}
114-
}
101+
// if (false) {
102+
// // see https://youtrack.jetbrains.com/issue/KT-63014/Running-tests-with-wasmJs-in-1.9.20-requires-Chrome-Canary#focus=Comments-27-8321383.0-0
103+
// rootProject.the<NodeJsRootExtension>().apply {
104+
// // nodeVersion = "21.0.0-v8-canary202309143a48826a08"
105+
// version = "21.0.0-v8-canary202309143a48826a08"
106+
// downloadBaseUrl = "https://nodejs.org/download/v8-canary"
107+
// }
108+
//
109+
// tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask>().configureEach {
110+
// args.add("--ignore-engines")
111+
// }
112+
// }
115113

116114
block()
117115
}

buildSrc/src/main/kotlin/P.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ sealed class P(override val group: String) : ProjectDetail() {
5252
5353
*/
5454
companion object {
55-
const val VERSION = "4.3.1"
56-
const val NEXT_VERSION = "4.3.2"
55+
const val VERSION = "4.4.0"
56+
const val NEXT_VERSION = "4.4.0"
5757
const val SNAPSHOT_VERSION = "$VERSION-SNAPSHOT"
5858
const val NEXT_SNAPSHOT_VERSION = "$NEXT_VERSION-SNAPSHOT"
5959

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ include(":simbot-gradles:simbot-gradle-suspendtransforms")
4444

4545
include(":simbot-commons:simbot-common-annotations")
4646
include(":simbot-commons:simbot-common-collection")
47+
include(":simbot-commons:simbot-common-streamable")
4748
include(":simbot-commons:simbot-common-atomic")
4849
include(":simbot-commons:simbot-common-apidefinition")
4950
include(":simbot-commons:simbot-common-core")

simbot-api/api/simbot-api.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ public abstract interface class love/forte/simbot/bot/BotManager : love/forte/si
371371
public fun all (Llove/forte/simbot/common/id/ID;)Lkotlin/sequences/Sequence;
372372
public fun allToList ()Ljava/util/List;
373373
public fun allToList (Llove/forte/simbot/common/id/ID;)Ljava/util/List;
374+
public fun allToStreamable ()Llove/forte/simbot/common/streamable/Streamable;
375+
public fun allToStreamable (Llove/forte/simbot/common/id/ID;)Llove/forte/simbot/common/streamable/Streamable;
374376
public fun asFuture ()Ljava/util/concurrent/CompletableFuture;
375377
public abstract fun cancel (Ljava/lang/Throwable;)V
376378
public static synthetic fun cancel$default (Llove/forte/simbot/bot/BotManager;Ljava/lang/Throwable;ILjava/lang/Object;)V
@@ -393,6 +395,7 @@ public final class love/forte/simbot/bot/BotManagerUtil {
393395

394396
public abstract interface class love/forte/simbot/bot/BotManagers : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
395397
public fun allBots ()Lkotlin/sequences/Sequence;
398+
public fun allBotsToStreamable ()Llove/forte/simbot/common/streamable/Streamable;
396399
public fun firstBot ()Llove/forte/simbot/bot/Bot;
397400
public fun firstBot (Llove/forte/simbot/common/id/ID;)Llove/forte/simbot/bot/Bot;
398401
}
@@ -1206,6 +1209,7 @@ public abstract interface class love/forte/simbot/event/EventListener {
12061209

12071210
public abstract interface class love/forte/simbot/event/EventListenerContainer {
12081211
public abstract fun getListeners ()Lkotlin/sequences/Sequence;
1212+
public fun listenersToStreamable ()Llove/forte/simbot/common/streamable/Streamable;
12091213
}
12101214

12111215
public abstract interface class love/forte/simbot/event/EventListenerContext {

simbot-api/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ kotlin {
7575
implementation(project(":simbot-commons:simbot-common-annotations"))
7676
implementation(project(":simbot-logger"))
7777

78+
api(project(":simbot-commons:simbot-common-streamable"))
7879
api(project(":simbot-commons:simbot-common-suspend-runner"))
7980
api(project(":simbot-commons:simbot-common-core"))
8081
api(project(":simbot-commons:simbot-common-collection"))

simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/BotManager.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import love.forte.simbot.ability.CompletionAware
2727
import love.forte.simbot.ability.LifecycleAware
2828
import love.forte.simbot.common.function.MergeableFactory
2929
import love.forte.simbot.common.id.ID
30+
import love.forte.simbot.common.streamable.Streamable
31+
import love.forte.simbot.common.streamable.Streamable.Companion.asStreamable
3032
import love.forte.simbot.plugin.PluginFactory
3133
import love.forte.simbot.suspendrunner.ST
3234

@@ -57,6 +59,24 @@ public interface BotManager : AutoConfigurableBotPlugin, LifecycleAware, Complet
5759
*/
5860
public fun all(id: ID): Sequence<Bot> = all().filter { bot -> bot.id == id }
5961

62+
/**
63+
* 得到所有的 [Bot] 并转化为 [Streamable]。
64+
*
65+
* @since 4.4.0
66+
* @see all
67+
*/
68+
public fun allToStreamable(): Streamable<Bot> =
69+
all().asStreamable()
70+
71+
/**
72+
* 得到所有 `id` 符合条件的 [Bot] 并转化为 [Streamable]。
73+
*
74+
* @since 4.4.0
75+
* @see all
76+
*/
77+
public fun allToStreamable(id: ID): Streamable<Bot> =
78+
all(id).asStreamable()
79+
6080
/**
6181
* 将 [all] 收集为 [List] 并返回。
6282
*

simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/BotManagers.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ package love.forte.simbot.bot
2828

2929
import love.forte.simbot.common.collection.toImmutable
3030
import love.forte.simbot.common.id.ID
31+
import love.forte.simbot.common.streamable.Streamable
32+
import love.forte.simbot.common.streamable.Streamable.Companion.asStreamable
3133
import kotlin.jvm.JvmMultifileClass
3234
import kotlin.jvm.JvmName
3335

@@ -41,6 +43,13 @@ public interface BotManagers : Collection<BotManager> {
4143
*/
4244
public fun allBots(): Sequence<Bot> = asSequence().flatMap { it.all() }
4345

46+
/**
47+
* 获取 [allBots] 的流转化器。
48+
*
49+
* @since 4.4.0
50+
*/
51+
public fun allBotsToStreamable(): Streamable<Bot> = allBots().asStreamable()
52+
4453
/**
4554
* 尝试获取第一个 [BotManager] 中的第一个 [Bot]。
4655
*

simbot-api/src/commonMain/kotlin/love/forte/simbot/event/EventListenerContainer.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Project https://github.com/simple-robot/simpler-robot
55
66
*
7-
* This file is part of the Simple Robot Library.
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
88
*
99
* This program is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU Lesser General Public License as published by
@@ -23,6 +23,9 @@
2323

2424
package love.forte.simbot.event
2525

26+
import love.forte.simbot.common.streamable.Streamable
27+
import love.forte.simbot.common.streamable.Streamable.Companion.asStreamable
28+
2629

2730
/**
2831
* 事件监听器的“容器”接口,提供用于获取其中的所有 [EventListener] 的API。
@@ -37,5 +40,11 @@ public interface EventListenerContainer {
3740
*/
3841
public val listeners: Sequence<EventListener>
3942

40-
43+
/**
44+
* 获取到 [listeners] 并转化为 [Streamable]
45+
*
46+
* @see listeners
47+
*/
48+
public fun listenersToStreamable(): Streamable<EventListener> =
49+
listeners.asStreamable()
4150
}

simbot-api/src/jvmMain/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
requires simbot.logger;
3232
requires static org.jetbrains.annotations;
3333
requires static simbot.common.annotations;
34+
requires simbot.common.streamable;
3435
requires simbot.common.suspendrunner;
3536
requires simbot.common.core;
3637
requires simbot.common.collection;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public final class love/forte/simbot/common/streamable/Streamable : java/lang/Iterable, kotlin/jvm/internal/markers/KMappedMarker {
2+
public static final field Companion Llove/forte/simbot/common/streamable/Streamable$Companion;
3+
public synthetic fun <init> (Lkotlin/sequences/Sequence;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
4+
public final fun asSequence ()Lkotlin/sequences/Sequence;
5+
public final fun asStream ()Ljava/util/stream/Stream;
6+
public final fun collectTo (Ljava/util/Collection;)Ljava/util/Collection;
7+
public final fun collectToList ()Ljava/util/List;
8+
public fun iterator ()Ljava/util/Iterator;
9+
public static final fun of (Lkotlin/sequences/Sequence;)Llove/forte/simbot/common/streamable/Streamable;
10+
}
11+
12+
public final class love/forte/simbot/common/streamable/Streamable$Companion {
13+
public final fun of (Lkotlin/sequences/Sequence;)Llove/forte/simbot/common/streamable/Streamable;
14+
}
15+

0 commit comments

Comments
 (0)