aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
blob: 7c734c57e6daaf986e94f3185ec2378d7ba3244c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package at.hannibal2.skyhanni.utils

import kotlin.time.Duration

class RecalculatingValue<T>(private val expireTime: Duration, val calculation: () -> T) {
    private var currentValue = calculation()
    private var lastAccessTime = SimpleTimeMark.farPast()

    fun getValue(): T {
        if (lastAccessTime.passedSince() > expireTime) {
            currentValue = calculation()
            lastAccessTime = SimpleTimeMark.now()
        }
        return currentValue
    }
}