Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose isInitialized, try/catch store initialization #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/com/statsig/androidsdk/Statsig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@ object Statsig {
return result ?: StatsigOverrides(mutableMapOf(), mutableMapOf())
}

/**
* @return true if the SDK is initialized (usable), though the most up
* to date values will not be fetched from the network until async initialization
* is complete
*/
@JvmStatic
fun isInitialized(): Boolean {
return client.isInitialized()
}

private fun enforceInitialized(functionName: String) {
client.enforceInitialized(functionName)
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/statsig/androidsdk/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,31 @@ internal class Store (private val sharedPrefs: SharedPreferences, user: StatsigU
currentUserCacheKey = user.getCacheKey()

if (cachedResponse != null) {
val type = object : TypeToken<MutableMap<String, Cache>>() {}.type
try {
Statsig.errorBoundary.capture({
val type = object : TypeToken<MutableMap<String, Cache>>() {}.type
tore-statsig marked this conversation as resolved.
Show resolved Hide resolved
cacheById = gson.fromJson(cachedResponse, type) ?: cacheById
} catch (_: Exception) {
}, {
StatsigUtil.removeFromSharedPrefs(sharedPrefs, CACHE_BY_USER_KEY)
}
})
}

stickyDeviceExperiments = mutableMapOf()
if (cachedDeviceValues != null) {
val type = object : TypeToken<MutableMap<String, APIDynamicConfig>>() {}.type
try {
Statsig.errorBoundary.capture({
val type = object : TypeToken<MutableMap<String, APIDynamicConfig>>() {}.type
stickyDeviceExperiments = gson.fromJson(cachedDeviceValues, type) ?: stickyDeviceExperiments
} catch (_: Exception) {
}, {
StatsigUtil.removeFromSharedPrefs(sharedPrefs, STICKY_DEVICE_EXPERIMENTS_KEY)
}
})
}

localOverrides = StatsigOverrides(mutableMapOf(), mutableMapOf())
if (cachedLocalOverrides != null) {
try {
Statsig.errorBoundary.capture({
localOverrides = gson.fromJson(cachedLocalOverrides, StatsigOverrides::class.java)
} catch (_: Exception) {
}, {
StatsigUtil.removeFromSharedPrefs(sharedPrefs, LOCAL_OVERRIDES_KEY)
}
})
}
reason = EvaluationReason.Uninitialized
currentCache = loadCacheForCurrentUser()
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/statsig/androidsdk/StatsigCacheTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class StatsigCacheTest {

testSharedPrefs.edit().putString("Statsig.CACHE_BY_USER", gson.toJson(cacheById))

assertFalse(Statsig.isInitialized())
TestUtil.startStatsigAndDontWait(app, user, StatsigOptions())
assertTrue(Statsig.isInitialized())
client = Statsig.client
assertTrue(client.checkGate("always_on"))

Expand Down