Skip to content

Commit

Permalink
fix: add isLoaded and lazy load
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Aug 16, 2024
1 parent f26b06f commit 6cfcbdc
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sdk/src/main/java/com/amplitude/experiment/storage/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ internal class LoadStoreCache<V>(
private val onLoad: (() -> Unit)? = null,
) {
private val cache: MutableMap<String, V> = mutableMapOf()
private var isLoaded = false

fun get(key: String): V? {
if (!isLoaded) load()
return cache[key]
}

fun getAll(): Map<String, V> {
if (!isLoaded) load()
return HashMap(cache)
}

Expand Down Expand Up @@ -62,6 +65,7 @@ internal class LoadStoreCache<V>(
clear()
putAll(values)
onLoad?.invoke()
isLoaded = true
}

fun store(values: MutableMap<String, V> = cache) {
Expand Down

0 comments on commit 6cfcbdc

Please sign in to comment.