Skip to content

Commit

Permalink
简单调整 Session Continuous
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Mar 9, 2024
1 parent 8888e4d commit 37f7e92
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package love.forte.simbot.extension.continuous.session

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.channels.ClosedSendChannelException
Expand Down Expand Up @@ -103,8 +104,14 @@ private class SimpleContinuousSessionContext<T, R>(coroutineContext: CoroutineCo
private val parentJob = coroutineContext[Job]

override fun computeSession(key: Any, inSession: InSession<T, R>): SimpleSessionImpl<T, R> {
val job = Job(parentJob)
val channel = Channel<SessionData<T, R>>()
val job = SupervisorJob(parentJob)
val channel = Channel<SessionData<T, R>>(
capacity = Channel.RENDEZVOUS,
onBufferOverflow = BufferOverflow.SUSPEND,
onUndeliveredElement = { (value, c) ->
c.cancel(SessionPushOnFailureException("Undelivered value: $value"))
})

val session = SimpleSessionImpl(key, job, channel, subScope)

job.invokeOnCompletion {
Expand Down Expand Up @@ -232,7 +239,13 @@ private class SimpleSessionImpl<T, R>(

val (value, continuation) = receive()
val handle =
job.invokeOnCompletion { cause -> continuation.resumeWithException(SessionCompletedWithoutResumeException(cause)) }
job.invokeOnCompletion { cause ->
continuation.resumeWithException(
SessionCompletedWithoutResumeException(
cause
)
)
}

return createSimpleSessionContinuation(value, continuation, handle)
}
Expand Down

0 comments on commit 37f7e92

Please sign in to comment.