Files
blitz-kt/src/main/kotlin/blitz/DelegateProperties.kt
SuperCraftAlex 6f80ab30a3 clean up
2024-03-09 18:34:25 +01:00

21 lines
569 B
Kotlin

package blitz
fun <T, O: Any> caching(tiedGet: Provider<T>, calc: (T) -> O) = object : Lazy<O> {
private var lastTiedV = tiedGet()
private var lastV: O? = null
override val value: O get() {
val nTied = tiedGet()
if (lastTiedV != nTied) {
lastTiedV = nTied
lastV = calc(nTied)
return lastV!!
}
if (lastV == null)
lastV = calc(nTied)
return lastV!!
}
override fun isInitialized(): Boolean =
lastTiedV == tiedGet() && lastV != null
}