com.blr19c.falowp.bot.system.cache.OnlyReadOnceReference.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of falowp-bot-system Show documentation
Show all versions of falowp-bot-system Show documentation
FalowpBot system infrastructure
package com.blr19c.falowp.bot.system.cache
import kotlinx.coroutines.runBlocking
import kotlin.reflect.KProperty
/**
* 仅能读取一次的数据
*/
class OnlyReadOnceReference(
private val block: suspend () -> T,
) {
private var firstCall = true
@Synchronized
operator fun getValue(thisRef: Any, property: KProperty<*>): T? {
return if (firstCall) {
firstCall = false
runBlocking { block.invoke() }
} else {
null
}
}
}