Make sure pending metadata is reset and is thread safe
Summary: We have gotton concurrent modification crashes from this Reviewed By: lblasa Differential Revision: D42343224 fbshipit-source-id: 9cf4046da63d40cbe6632c3ae24d95abd21081ba
This commit is contained in:
committed by
Facebook GitHub Bot
parent
18b6ce6f24
commit
412d10b280
@@ -59,7 +59,7 @@ class UIDebuggerFlipperPlugin(
|
||||
MetadataUpdateEvent.name,
|
||||
Json.encodeToString(
|
||||
MetadataUpdateEvent.serializer(),
|
||||
MetadataUpdateEvent(MetadataRegister.getPendingMetadata())))
|
||||
MetadataUpdateEvent(MetadataRegister.extractPendingMetadata())))
|
||||
|
||||
context.treeObserverManager.start()
|
||||
}
|
||||
|
||||
@@ -23,9 +23,11 @@ object MetadataRegister {
|
||||
const val TYPE_LAYOUT = "layout"
|
||||
const val TYPE_DOCUMENTATION = "documentation"
|
||||
|
||||
private val lock = "lock"
|
||||
|
||||
private var generator: MetadataId = 0
|
||||
private val register: MutableMap<String, Metadata> = mutableMapOf()
|
||||
private val pending: MutableSet<String> = mutableSetOf()
|
||||
private val pendingKeys: MutableSet<String> = mutableSetOf()
|
||||
|
||||
private fun key(namespace: String, name: String): String = "${namespace}_$name"
|
||||
|
||||
@@ -41,38 +43,40 @@ object MetadataRegister {
|
||||
return m.id
|
||||
}
|
||||
|
||||
synchronized(lock) {
|
||||
val identifier = ++generator
|
||||
val metadata = Metadata(identifier, type, namespace, name, mutable, possibleValues)
|
||||
|
||||
register[key] = metadata
|
||||
pending.add(key)
|
||||
pendingKeys.add(key)
|
||||
|
||||
return identifier
|
||||
}
|
||||
}
|
||||
|
||||
fun get(namespace: String, name: String): Metadata? {
|
||||
val key = key(namespace, name)
|
||||
return register[key]
|
||||
}
|
||||
|
||||
fun getMetadata(): Map<MetadataId, Metadata> {
|
||||
val metadata: MutableMap<MetadataId, Metadata> = mutableMapOf()
|
||||
register.forEach { entry -> metadata[entry.value.id] = entry.value }
|
||||
|
||||
return metadata
|
||||
}
|
||||
|
||||
fun getPendingMetadata(): Map<MetadataId, Metadata> {
|
||||
/** gets all pending metadata to be sent and resets the pending list */
|
||||
fun extractPendingMetadata(): Map<MetadataId, Metadata> {
|
||||
synchronized(lock) {
|
||||
val pendingMetadata: MutableMap<MetadataId, Metadata> = mutableMapOf()
|
||||
pending.forEach { key ->
|
||||
|
||||
pendingKeys.forEach { key ->
|
||||
register[key]?.let { metadata -> pendingMetadata[metadata.id] = metadata }
|
||||
}
|
||||
pendingKeys.clear()
|
||||
|
||||
return pendingMetadata
|
||||
}
|
||||
}
|
||||
|
||||
fun reset() {
|
||||
pending.clear()
|
||||
register.forEach { entry -> pending.add(entry.key) }
|
||||
synchronized(lock) {
|
||||
pendingKeys.clear()
|
||||
register.forEach { entry -> pendingKeys.add(entry.key) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ class TreeObserverManager(val context: Context) {
|
||||
}
|
||||
|
||||
private fun sendMetadata() {
|
||||
val metadata = MetadataRegister.getPendingMetadata()
|
||||
val metadata = MetadataRegister.extractPendingMetadata()
|
||||
if (metadata.size > 0) {
|
||||
context.connectionRef.connection?.send(
|
||||
MetadataUpdateEvent.name,
|
||||
|
||||
Reference in New Issue
Block a user