All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blr19c.falowp.bot.system.cache.OnlyReadOnceReference.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC2
Show newest version
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
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy