mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-23 18:30:13 +08:00
Fix weakRef
This commit is contained in:
parent
aebaf605a7
commit
8e6c68b8b6
@ -11,19 +11,34 @@ import kotlin.reflect.KProperty
|
|||||||
inline class UnsafeWeakRef<T>(private val weakRef: WeakRef<T>) {
|
inline class UnsafeWeakRef<T>(private val weakRef: WeakRef<T>) {
|
||||||
fun get(): T = weakRef.get() ?: error("WeakRef is released")
|
fun get(): T = weakRef.get() ?: error("WeakRef is released")
|
||||||
fun clear() = weakRef.clear()
|
fun clear() = weakRef.clear()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides delegate value.
|
* Provides delegate value.
|
||||||
*
|
*
|
||||||
* ```kotlin
|
* ```kotlin
|
||||||
* val bot: Bot by param.unsafeWeakRef()
|
* val bot: Bot by param.unsafeWeakRef()
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): ReadOnlyProperty<Any?, T> {
|
operator fun <T> UnsafeWeakRef<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): ReadOnlyProperty<Any?, T> {
|
||||||
return object : ReadOnlyProperty<Any?, T> {
|
return object : ReadOnlyProperty<Any?, T> {
|
||||||
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
|
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
|
||||||
return get()
|
return get()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides delegate value.
|
||||||
|
*
|
||||||
|
* ```kotlin
|
||||||
|
* val bot: Bot by param.unsafeWeakRef()
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
operator fun <T> WeakRef<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): ReadOnlyProperty<Any?, T?> {
|
||||||
|
return object : ReadOnlyProperty<Any?, T?> {
|
||||||
|
override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
|
||||||
|
return get()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user