Skip to content

Commit

Permalink
Merge pull request #835 from simple-robot/def-job-with-app
Browse files Browse the repository at this point in the history
Application的coroutineContext应当始终有一个Job
  • Loading branch information
ForteScarlet committed Jun 3, 2024
2 parents c653c46 + 539774e commit 5b81696
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package love.forte.simbot.spring.common.application

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import love.forte.simbot.annotations.InternalSimbotAPI
import love.forte.simbot.application.*
Expand Down Expand Up @@ -65,11 +67,16 @@ public open class SpringApplicationBuilder : AbstractApplicationBuilder() {
* Build [SpringApplicationConfiguration]
*/
@InternalSimbotAPI
public open fun build(): SpringApplicationConfiguration =
SpringApplicationConfigurationImpl(
coroutineContext,
public open fun build(): SpringApplicationConfiguration {
val context = coroutineContext
val job = SupervisorJob(context[Job])

return SpringApplicationConfigurationImpl(
context.minusKey(Job) + job,
applicationConfigurationProperties
)
}


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,13 @@ private class SimpleApplicationFactoryConfigurer(
* 通过 [Simple] 构建 [SimpleApplication] 时使用的构建器。
*/
public class SimpleApplicationBuilder : AbstractApplicationBuilder() {
internal fun build(): SimpleApplicationConfiguration = SimpleApplicationConfigurationImpl(coroutineContext)
internal fun build(): SimpleApplicationConfiguration {
val context = coroutineContext
val job = SupervisorJob(context[Job])

// 至少有个 Job
return SimpleApplicationConfigurationImpl(context.minusKey(Job) + job)
}
}

private class SimpleApplicationConfigurationImpl(override val coroutineContext: CoroutineContext) :
Expand Down

0 comments on commit 5b81696

Please sign in to comment.