Skip to content

Commit

Permalink
fix: thread-safe Identify object (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy authored Jun 3, 2023
1 parent d10bce6 commit 60120f2
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions core/src/main/java/com/amplitude/core/events/Identify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ enum class IdentifyOperation(val operationType: String) {
REMOVE("\$remove")
}

open class Identify() {
open class Identify {

private val propertySet: MutableSet<String> = mutableSetOf()
val properties = mutableMapOf<String, Any?>()

private val _properties = mutableMapOf<String, Any?>()
val properties: MutableMap<String, Any?>
@Synchronized get() {
val clonedProperties = _properties.toMutableMap()
for ((key, value) in clonedProperties) {
if (value is Map<*, *>) {
clonedProperties[key] = value.toMutableMap()
}
}
return clonedProperties
}

fun set(property: String, value: Boolean): Identify {
setUserProperty(IdentifyOperation.SET, property, value)
Expand Down Expand Up @@ -540,13 +551,13 @@ open class Identify() {
return this
}

fun clearAll(): Identify {
properties.clear()
properties.put(IdentifyOperation.CLEAR_ALL.operationType, UNSET_VALUE)
@Synchronized fun clearAll(): Identify {
_properties.clear()
_properties.put(IdentifyOperation.CLEAR_ALL.operationType, UNSET_VALUE)
return this
}

private fun setUserProperty(operation: IdentifyOperation, property: String, value: Any?) {
@Synchronized private fun setUserProperty(operation: IdentifyOperation, property: String, value: Any?) {
if (property.isEmpty()) {
ConsoleLogger.logger.warn("Attempting to perform operation ${operation.operationType} with a null or empty string property, ignoring")
return
Expand All @@ -556,7 +567,7 @@ open class Identify() {
return
}
// check that clearAll wasn't already used in this Identify
if (properties.containsKey(IdentifyOperation.CLEAR_ALL.operationType)) {
if (_properties.containsKey(IdentifyOperation.CLEAR_ALL.operationType)) {
ConsoleLogger.logger.warn("This Identify already contains a \$clearAll operation, ignoring operation %s")
return
}
Expand All @@ -565,10 +576,10 @@ open class Identify() {
ConsoleLogger.logger.warn("Already used property $property in previous operation, ignoring operation ${operation.operationType}")
return
}
if (!properties.containsKey(operation.operationType)) {
properties[operation.operationType] = mutableMapOf<String, Any>()
if (!_properties.containsKey(operation.operationType)) {
_properties[operation.operationType] = mutableMapOf<String, Any>()
}
(properties[operation.operationType] as MutableMap<String, Any>)[property] = value
(_properties[operation.operationType] as MutableMap<String, Any>)[property] = value
propertySet.add(property)
}

Expand Down

0 comments on commit 60120f2

Please sign in to comment.