فهرست منبع

[core] CoroutineOnDemandValueScope: fix emit when state is ProducerReady

Him188 2 سال پیش
والد
کامیت
a1d8d744d0
1فایلهای تغییر یافته به همراه14 افزوده شده و 6 حذف شده
  1. 14 6
      mirai-core/src/commonMain/kotlin/network/auth/CoroutineOnDemandValueScope.kt

+ 14 - 6
mirai-core/src/commonMain/kotlin/network/auth/CoroutineOnDemandValueScope.kt

@@ -73,6 +73,10 @@ internal class CoroutineOnDemandValueScope<T, V>(
                         }
                     }
 
+                    is ProducerState.ProducerReady -> {
+                        setStateProducing(state)
+                    }
+
                     else -> throw IllegalProducerStateException(state)
                 }
             }
@@ -96,6 +100,14 @@ internal class CoroutineOnDemandValueScope<T, V>(
         }
     }
 
+    private fun setStateProducing(state: ProducerState.ProducerReady<T, V>) {
+        val deferred = CompletableDeferred<V>(coroutineScope.coroutineContext.job)
+        if (!compareAndSetState(state, ProducerState.Producing(state.producer, deferred))) {
+            deferred.cancel() // avoid leak
+        }
+        // loop again
+    }
+
     private fun finishImpl(exception: Throwable?) {
         state.loop { state ->
             when (state) {
@@ -173,11 +185,7 @@ internal class CoroutineOnDemandValueScope<T, V>(
                 }
 
                 is ProducerState.ProducerReady -> {
-                    val deferred = CompletableDeferred<V>(coroutineScope.coroutineContext.job)
-                    if (!compareAndSetState(state, ProducerState.Producing(state.producer, deferred))) {
-                        deferred.cancel() // avoid leak
-                    }
-                    // loop again
+                    setStateProducing(state)
                 }
 
                 is ProducerState.Producing -> return true // ok
@@ -201,4 +209,4 @@ internal class CoroutineOnDemandValueScope<T, V>(
     override fun finish() {
         finishImpl(null)
     }
-}
+}