Skip to content

Commit

Permalink
update user with values (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinlili-statsig authored Apr 28, 2024
1 parent ce565ce commit eb3270a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/statsig/androidsdk/Statsig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ object Statsig {
* @throws IllegalStateException if the SDK has not been initialized
*/
@JvmStatic
fun updateUserAsync(user: StatsigUser?, callback: IStatsigCallback? = null) {
client.updateUserAsync(user, callback)
fun updateUserAsync(user: StatsigUser?, callback: IStatsigCallback? = null, values: Map<String, Any>? = null) {
client.updateUserAsync(user, callback, values)
}

/**
Expand All @@ -288,8 +288,8 @@ object Statsig {
* @throws IllegalStateException if the SDK has not been initialized
*/
@JvmSynthetic // Hide this from Java files
suspend fun updateUser(user: StatsigUser?) {
client.updateUser(user)
suspend fun updateUser(user: StatsigUser?, values: Map<String, Any>? = null) {
client.updateUser(user, values)
}

/**
Expand Down
39 changes: 27 additions & 12 deletions src/main/java/com/statsig/androidsdk/StatsigClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,23 @@ class StatsigClient() : LifecycleEventListener {
* Log Events will be dropped
* @throws IllegalStateException if the SDK has not been initialized
*/
fun updateUserAsync(user: StatsigUser?, callback: IStatsigCallback? = null) {
fun updateUserAsync(user: StatsigUser?, callback: IStatsigCallback? = null, values: Map<String, Any>? = null) {
val functionName = "updateUserAsync"
enforceInitialized(functionName)
errorBoundary.capture({
updateUserCache(user)
statsigScope.launch {
updateUserImpl()
withContext(dispatcherProvider.main) {
try {
callback?.onStatsigUpdateUser()
} catch (e: Exception) {
throw ExternalException(e.message)
if (values != null) {
updateUserWithValuesImpl(user, values)
callback?.onStatsigUpdateUser()
} else {
updateUserCache(user)
statsigScope.launch {
updateUserImpl()
withContext(dispatcherProvider.main) {
try {
callback?.onStatsigUpdateUser()
} catch (e: Exception) {
throw ExternalException(e.message)
}
}
}
}
Expand All @@ -417,11 +422,15 @@ class StatsigClient() : LifecycleEventListener {
* @param user the updated user
* @throws IllegalStateException if the SDK has not been initialized
*/
suspend fun updateUser(user: StatsigUser?) {
suspend fun updateUser(user: StatsigUser?, values: Map<String, Any>? = null) {
enforceInitialized("updateUser")
errorBoundary.captureAsync {
updateUserCache(user)
updateUserImpl()
if (values != null) {
updateUserWithValuesImpl(user, values)
} else {
updateUserCache(user)
updateUserImpl()
}
options.userObjectValidator?.let { it(this.user) }
}
}
Expand Down Expand Up @@ -766,6 +775,12 @@ class StatsigClient() : LifecycleEventListener {
})
}

private fun updateUserWithValuesImpl(user: StatsigUser?, values: Map<String, Any>) {
val normalizedUser = normalizeUser(user)
this.user = normalizedUser
this.store.bootstrap(values, this.user)
}

private suspend fun updateUserImpl() {
withContext(dispatcherProvider.io) {
errorBoundary.captureAsync(
Expand Down

0 comments on commit eb3270a

Please sign in to comment.