From e90be8b93d7d04cb4a3c50f02fd051f7a6987fd5 Mon Sep 17 00:00:00 2001
From: Him188 <Him188@mamoe.net>
Date: Wed, 18 Dec 2019 10:35:23 +0800
Subject: [PATCH] Enhance SuspendLazy

---
 .../net.mamoe.mirai/utils/SuspendLazy.kt      | 24 +++++++------------
 1 file changed, 9 insertions(+), 15 deletions(-)

diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/SuspendLazy.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/SuspendLazy.kt
index cbc9b2f2c..8fab59f2f 100644
--- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/SuspendLazy.kt
+++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/SuspendLazy.kt
@@ -2,16 +2,18 @@
 
 package net.mamoe.mirai.utils
 
-import kotlinx.coroutines.CompletableDeferred
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Deferred
 import kotlinx.coroutines.async
 import net.mamoe.mirai.contact.QQ
+import kotlin.coroutines.CoroutineContext
+import kotlin.coroutines.EmptyCoroutineContext
 
 /**
- * 创建一个在当前 [CoroutineScope] 下执行的 [SuspendLazy]
+ * 创建一个在当前 [CoroutineScope] 下执行初始化的 [SuspendLazy]
  */
-fun <R> CoroutineScope.SuspendLazy(initializer: suspend () -> R): Lazy<Deferred<R>> = SuspendLazy(this, initializer)
+fun <R> CoroutineScope.SuspendLazy(context: CoroutineContext = EmptyCoroutineContext, initializer: suspend () -> R): Lazy<Deferred<R>> =
+    SuspendLazy(this, context, initializer)
 
 /**
  * 挂起初始化的 [lazy], 是属性不能 `suspend` 的替代品.
@@ -27,20 +29,12 @@ fun <R> CoroutineScope.SuspendLazy(initializer: suspend () -> R): Lazy<Deferred<
  * @sample QQ.profile
  */
 @PublishedApi
-internal class SuspendLazy<R>(scope: CoroutineScope, initializer: suspend () -> R) : Lazy<Deferred<R>> {
-    private val valueUpdater: Deferred<R> by lazy { scope.async { initializer() } }
+internal class SuspendLazy<R>(scope: CoroutineScope, coroutineContext: CoroutineContext = EmptyCoroutineContext, initializer: suspend () -> R) :
+    Lazy<Deferred<R>> {
+    private val valueUpdater: Deferred<R> by lazy { scope.async(context = coroutineContext) { initializer() } }
 
-    @Suppress("EXPERIMENTAL_API_USAGE")
     override val value: Deferred<R>
-        get() = if (isInitialized()) {
-            CompletableDeferred(valueUpdater.getCompleted())
-        } else {
-            CompletableDeferred<R>().apply {
-                valueUpdater.invokeOnCompletion {
-                    this.complete(valueUpdater.getCompleted())
-                }
-            }
-        }
+        get() = valueUpdater
 
     override fun isInitialized(): Boolean = valueUpdater.isCompleted
 }
\ No newline at end of file