aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
blob: 76f0c0c940055c175da095618c5847e2ceeb5d9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
    }
}