From 8e6c68b8b6f84a8cc5e09517f96690caa89056e5 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 17 Jan 2020 16:24:11 +0800 Subject: [PATCH] Fix weakRef --- .../kotlin/net.mamoe.mirai/utils/WeakRef.kt | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/WeakRef.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/WeakRef.kt index d6949e624..4f56ec816 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/WeakRef.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/WeakRef.kt @@ -11,19 +11,34 @@ import kotlin.reflect.KProperty inline class UnsafeWeakRef(private val weakRef: WeakRef) { fun get(): T = weakRef.get() ?: error("WeakRef is released") fun clear() = weakRef.clear() +} - /** - * Provides delegate value. - * - * ```kotlin - * val bot: Bot by param.unsafeWeakRef() - * ``` - */ - operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): ReadOnlyProperty { - return object : ReadOnlyProperty { - override fun getValue(thisRef: Any?, property: KProperty<*>): T { - return get() - } +/** + * Provides delegate value. + * + * ```kotlin + * val bot: Bot by param.unsafeWeakRef() + * ``` + */ +operator fun UnsafeWeakRef.provideDelegate(thisRef: Any?, property: KProperty<*>): ReadOnlyProperty { + return object : ReadOnlyProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): T { + return get() + } + } +} + +/** + * Provides delegate value. + * + * ```kotlin + * val bot: Bot by param.unsafeWeakRef() + * ``` + */ +operator fun WeakRef.provideDelegate(thisRef: Any?, property: KProperty<*>): ReadOnlyProperty { + return object : ReadOnlyProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): T? { + return get() } } }