mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 23:00:31 +05:30
51 lines
1.3 KiB
Kotlin
51 lines
1.3 KiB
Kotlin
package com.github.libretube
|
|
|
|
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
|
|
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()
|
|
}
|
|
|
|
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() |