LibreTube/app/src/main/java/com/github/libretube/api/ResettableLazy.kt
2022-08-14 09:55:28 +02:00

55 lines
1.3 KiB
Kotlin

package com.github.libretube.util
import java.util.*
import kotlin.reflect.KProperty
class ResettableLazyManager {
// we synchronize to make sure the timing of a reset() call and new inits do not collide
private val managedDelegates = LinkedList<Resettable>()
fun register(managed: Resettable) {
synchronized(managedDelegates) {
managedDelegates.add(managed)
}
}
fun reset() {
synchronized(managedDelegates) {
managedDelegates.forEach { it.reset() }
managedDelegates.clear()
}
}
}
interface Resettable {
fun reset()
}
class ResettableLazy<PROPTYPE>(val manager: ResettableLazyManager, val init: () -> PROPTYPE) :
Resettable {
@Volatile
var lazyHolder = makeInitBlock()
operator fun getValue(thisRef: Any?, property: KProperty<*>): PROPTYPE {
return lazyHolder.value
}
override fun reset() {
lazyHolder = makeInitBlock()
}
private fun makeInitBlock(): Lazy<PROPTYPE> {
return lazy {
manager.register(this)
init()
}
}
}
fun <PROPTYPE> resettableLazy(manager: ResettableLazyManager, init: () -> PROPTYPE):
ResettableLazy<PROPTYPE> {
return ResettableLazy(manager, init)
}
fun resettableManager(): ResettableLazyManager = ResettableLazyManager()